mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 09:15:41 +08:00
feat: Reader Skill 支持版本自检与自助更新
微信机器人回复剥离引用编号、列表改用空行分隔,失败通知补失败时刻 问问微信关键词检索在索引落后时按单会话直读原始记录兜底 关闭窗口支持记住选择并在设置中修改 退群监控刷新列表
This commit is contained in:
1 parent
fd7d5016a8
commit
eaa25cd29e
33 files changed
+1401
-154
No files matched your search
@@ -1,9 +1,12 @@
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs'
|
||||
import { createHash } from 'crypto'
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs'
|
||||
import { tmpdir } from 'os'
|
||||
import { dirname, join, resolve } from 'path'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import {
|
||||
getSkillCandidates,
|
||||
parseSkillFrontmatter,
|
||||
readSkillDocument,
|
||||
resolveSkillResourceStatus
|
||||
} from '../../src/main/services/skill-resource-service'
|
||||
|
||||
@@ -25,9 +28,18 @@ function environment(root: string, packaged: boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
function writeSkill(filePath: string): void {
|
||||
function skillBody(version?: string): string {
|
||||
const versionLine = version ? `version: ${version}\n` : ''
|
||||
return `---\nname: tracememo-reader\n${versionLine}description: fixture\n---\n\n# TraceMemo Reader\n\n正文内容。\n`
|
||||
}
|
||||
|
||||
function writeSkill(filePath: string, version?: string): void {
|
||||
mkdirSync(dirname(filePath), { recursive: true })
|
||||
writeFileSync(filePath, '# TraceMemo Reader\n', 'utf8')
|
||||
writeFileSync(filePath, skillBody(version), 'utf8')
|
||||
}
|
||||
|
||||
function sha256(content: string): string {
|
||||
return createHash('sha256').update(content, 'utf8').digest('hex')
|
||||
}
|
||||
|
||||
describe('Reader Skill resource resolution', () => {
|
||||
@@ -39,44 +51,28 @@ describe('Reader Skill resource resolution', () => {
|
||||
const root = fixtureRoot()
|
||||
const runtime = environment(root, false)
|
||||
const skillPath = join(runtime.cwd, 'docs', 'skill', 'tracememo-reader', 'SKILL.md')
|
||||
writeSkill(skillPath)
|
||||
writeSkill(skillPath, '1.3.0')
|
||||
|
||||
expect(resolveSkillResourceStatus(runtime)).toMatchObject({
|
||||
available: true,
|
||||
source: 'development',
|
||||
version: 'v1.2',
|
||||
name: 'tracememo-reader',
|
||||
version: '1.3.0',
|
||||
filePath: skillPath,
|
||||
directoryPath: dirname(skillPath)
|
||||
})
|
||||
})
|
||||
|
||||
it('resolves the Reader Skill from this checkout', () => {
|
||||
const workspace = resolve(__dirname, '../..')
|
||||
const status = resolveSkillResourceStatus({
|
||||
appPath: workspace,
|
||||
cwd: workspace,
|
||||
resourcesPath: join(workspace, 'node_modules', 'electron', 'resources'),
|
||||
execPath: join(workspace, 'node_modules', 'electron', 'electron.exe'),
|
||||
packaged: false
|
||||
})
|
||||
|
||||
expect(status).toMatchObject({
|
||||
available: true,
|
||||
source: 'development',
|
||||
version: 'v1.2',
|
||||
filePath: join(workspace, 'docs', 'skill', 'tracememo-reader', 'SKILL.md')
|
||||
})
|
||||
})
|
||||
|
||||
it('uses the extraResources Skill directory in a packaged runtime', () => {
|
||||
const root = fixtureRoot()
|
||||
const runtime = environment(root, true)
|
||||
const skillPath = join(runtime.resourcesPath, 'skill', 'tracememo-reader', 'SKILL.md')
|
||||
writeSkill(skillPath)
|
||||
writeSkill(skillPath, '1.3.0')
|
||||
|
||||
expect(resolveSkillResourceStatus(runtime)).toMatchObject({
|
||||
available: true,
|
||||
source: 'bundled',
|
||||
version: '1.3.0',
|
||||
filePath: skillPath
|
||||
})
|
||||
})
|
||||
@@ -104,4 +100,116 @@ describe('Reader Skill resource resolution', () => {
|
||||
expect(status).toMatchObject({ available: false, source: 'development' })
|
||||
for (const candidate of candidates) expect(status.error).toContain(candidate.path)
|
||||
})
|
||||
|
||||
it('omits the version when the frontmatter does not declare one', () => {
|
||||
const root = fixtureRoot()
|
||||
const runtime = environment(root, false)
|
||||
const skillPath = join(runtime.cwd, 'docs', 'skill', 'tracememo-reader', 'SKILL.md')
|
||||
writeSkill(skillPath)
|
||||
|
||||
const status = resolveSkillResourceStatus(runtime)
|
||||
expect(status.available).toBe(true)
|
||||
expect(status.version).toBeUndefined()
|
||||
})
|
||||
|
||||
// /health 是外部 Agent 的自检入口:环境探测本身出错时只能降级,不能抛出。
|
||||
it('degrades to unavailable instead of throwing when the environment is broken', () => {
|
||||
const status = resolveSkillResourceStatus({
|
||||
appPath: undefined as unknown as string,
|
||||
cwd: undefined as unknown as string,
|
||||
resourcesPath: '',
|
||||
execPath: undefined as unknown as string,
|
||||
packaged: false
|
||||
})
|
||||
|
||||
expect(status.available).toBe(false)
|
||||
expect(status.error).toContain('未找到 TraceMemo Reader Skill 文件')
|
||||
expect(status.githubUrl).toContain('github.com')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Reader Skill document identity', () => {
|
||||
afterEach(() => {
|
||||
while (roots.length) rmSync(roots.pop()!, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('exposes markdown, version and a content hash that matches the file bytes', () => {
|
||||
const root = fixtureRoot()
|
||||
const skillPath = join(root, 'docs', 'skill', 'tracememo-reader', 'SKILL.md')
|
||||
writeSkill(skillPath, '1.3.0')
|
||||
const content = readFileSync(skillPath, 'utf-8')
|
||||
|
||||
const document = readSkillDocument(skillPath)
|
||||
expect(document).not.toBeNull()
|
||||
expect(document?.version).toBe('1.3.0')
|
||||
expect(document?.name).toBe('tracememo-reader')
|
||||
expect(document?.markdown).toBe(content)
|
||||
expect(document?.contentHash).toBe(sha256(content))
|
||||
expect(document?.filePath).toBe(skillPath)
|
||||
expect(Number.isNaN(Date.parse(document?.modifiedAt || ''))).toBe(false)
|
||||
})
|
||||
|
||||
it('returns null for a missing file instead of throwing', () => {
|
||||
const root = fixtureRoot()
|
||||
expect(readSkillDocument(join(root, 'missing', 'SKILL.md'))).toBeNull()
|
||||
})
|
||||
|
||||
// 自助更新的正确性依赖这条:Agent 覆盖 SKILL.md 后,health 必须立刻报出新版本。
|
||||
it('invalidates the cache once the file on disk is replaced', () => {
|
||||
const root = fixtureRoot()
|
||||
const skillPath = join(root, 'docs', 'skill', 'tracememo-reader', 'SKILL.md')
|
||||
writeSkill(skillPath, '1.3.0')
|
||||
expect(readSkillDocument(skillPath)?.version).toBe('1.3.0')
|
||||
|
||||
writeSkill(skillPath, '1.4.0-with-a-longer-body-to-change-the-file-size')
|
||||
const updated = readSkillDocument(skillPath)
|
||||
expect(updated?.version).toBe('1.4.0-with-a-longer-body-to-change-the-file-size')
|
||||
expect(updated?.contentHash).not.toBe(sha256(skillBody('1.3.0')))
|
||||
})
|
||||
})
|
||||
|
||||
describe('SKILL.md frontmatter contract', () => {
|
||||
it('parses name and version from a plain frontmatter block', () => {
|
||||
expect(parseSkillFrontmatter('---\nname: a\nversion: 2.1.0\n---\nbody')).toEqual({
|
||||
name: 'a',
|
||||
version: '2.1.0'
|
||||
})
|
||||
})
|
||||
|
||||
it('ignores files without frontmatter and tolerates CRLF, quotes and comments', () => {
|
||||
expect(parseSkillFrontmatter('# no frontmatter\n')).toEqual({})
|
||||
expect(parseSkillFrontmatter('---\r\n# comment\r\nversion: "3.0.1"\r\n---\r\n')).toEqual({
|
||||
version: '3.0.1'
|
||||
})
|
||||
})
|
||||
|
||||
/**
|
||||
* 防漂移契约:版本号只有一个真源,就是 SKILL.md 的 frontmatter。
|
||||
* 这里独立地按行正则提取一次,与解析结果逐字比对 —— 任何"把版本写回源码常量"的
|
||||
* 回归都会让这条测试变红。
|
||||
*/
|
||||
it('keeps the reader Skill version declared in the repository SKILL.md', () => {
|
||||
const workspace = resolve(__dirname, '../..')
|
||||
const skillPath = join(workspace, 'docs', 'skill', 'tracememo-reader', 'SKILL.md')
|
||||
const content = readFileSync(skillPath, 'utf-8')
|
||||
const declared = /^version:[ \t]*(\S+)[ \t]*$/m.exec(content)
|
||||
|
||||
expect(declared).not.toBeNull()
|
||||
expect(parseSkillFrontmatter(content).version).toBe(declared![1])
|
||||
expect(declared![1]).toMatch(/^\d+\.\d+\.\d+$/)
|
||||
|
||||
const status = resolveSkillResourceStatus({
|
||||
appPath: workspace,
|
||||
cwd: workspace,
|
||||
resourcesPath: join(workspace, 'node_modules', 'electron', 'resources'),
|
||||
execPath: join(workspace, 'node_modules', 'electron', 'electron.exe'),
|
||||
packaged: false
|
||||
})
|
||||
expect(status).toMatchObject({
|
||||
available: true,
|
||||
source: 'development',
|
||||
version: declared![1],
|
||||
filePath: skillPath
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user