mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-08-17 19:47:08 +08:00
feat: 完成 TraceMemo v2.2.0 品牌身份与数据迁移升级
- 将 WechatExplorer 产品身份升级为 TraceMemo - 更新 appId、Runtime Identity、Reader Skill 和 API 环境变量 - 增加旧用户数据、Knowledge、Token 与 AI Provider 安全迁移 - 保留 Windows WeFlow 和旧版配置兼容 - 完善首次启动迁移测试及 v2.2.0 发布文档 - 移除 macOS Intel x64 构建与发布支持
This commit is contained in:
@@ -62,4 +62,19 @@ describe('ApiTokenStore', () => {
|
||||
expect(fs.existsSync(filePath)).toBe(false)
|
||||
expect(store.revealToken().token).toBeUndefined()
|
||||
})
|
||||
|
||||
it('does not silently replace a legacy token when migration is deferred', () => {
|
||||
const store = new ApiTokenStore(filePath)
|
||||
store.setAutomaticGenerationBlocked('legacy token migration pending')
|
||||
|
||||
expect(store.ensureToken()).toMatchObject({
|
||||
success: false,
|
||||
hasToken: false,
|
||||
error: 'legacy token migration pending'
|
||||
})
|
||||
expect(fs.existsSync(filePath)).toBe(false)
|
||||
|
||||
expect(store.rotateToken()).toMatchObject({ success: true, hasToken: true })
|
||||
expect(fs.existsSync(filePath)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
import fs from 'fs-extra'
|
||||
import { DatabaseSync } from 'node:sqlite'
|
||||
import os from 'os'
|
||||
import path from 'path'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const electronState = vi.hoisted(() => ({
|
||||
paths: new Map<string, string>([
|
||||
['appData', '/tmp/tracememo-migration-test-app-data'],
|
||||
['temp', '/tmp'],
|
||||
['home', '/tmp']
|
||||
]),
|
||||
name: ''
|
||||
}))
|
||||
|
||||
vi.mock('electron', () => ({
|
||||
app: {
|
||||
isPackaged: true,
|
||||
setName: (name: string) => {
|
||||
electronState.name = name
|
||||
},
|
||||
getPath: (name: string) => electronState.paths.get(name) || '/tmp',
|
||||
setPath: (name: string, value: string) => electronState.paths.set(name, value)
|
||||
},
|
||||
dialog: { showMessageBox: vi.fn() },
|
||||
safeStorage: {
|
||||
isEncryptionAvailable: () => true,
|
||||
encryptString: (value: string) => Buffer.from(value, 'utf8').reverse(),
|
||||
decryptString: (value: Buffer) => Buffer.from(value).reverse().toString('utf8')
|
||||
}
|
||||
}))
|
||||
|
||||
import { assessMigration, executeMigration } from '../../src/main/app-data-migration'
|
||||
import { getUserDataRoots } from '../../src/main/app-data-paths'
|
||||
|
||||
let root = ''
|
||||
|
||||
beforeEach(() => {
|
||||
root = fs.mkdtempSync(path.join(os.tmpdir(), 'tracememo-migration-'))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
fs.removeSync(root)
|
||||
})
|
||||
|
||||
function roots() {
|
||||
return getUserDataRoots(path.join(root, 'Application Support'))
|
||||
}
|
||||
|
||||
function writeFixture(filePath: string, content = 'fixture'): void {
|
||||
fs.ensureDirSync(path.dirname(filePath))
|
||||
fs.writeFileSync(filePath, content)
|
||||
}
|
||||
|
||||
describe('TraceMemo app data migration', () => {
|
||||
it('treats a new TraceMemo install as clean when no legacy directory exists', () => {
|
||||
const assessment = assessMigration(roots())
|
||||
expect(assessment).toMatchObject({
|
||||
shouldPrompt: false,
|
||||
reason: 'clean-install',
|
||||
currentHasAssets: false
|
||||
})
|
||||
expect(assessment.selection.selected).toBe(roots().current)
|
||||
})
|
||||
|
||||
it('ignores an empty WechatExplorer legacy directory', () => {
|
||||
const fixture = roots()
|
||||
fs.ensureDirSync(fixture.legacy)
|
||||
expect(assessMigration(fixture)).toMatchObject({
|
||||
shouldPrompt: false,
|
||||
reason: 'legacy-empty'
|
||||
})
|
||||
})
|
||||
|
||||
it('detects legacy settings but never proposes overwriting valid TraceMemo data', () => {
|
||||
const fixture = roots()
|
||||
writeFixture(path.join(fixture.legacy, 'settings.json'), '{"dbRoot":"legacy"}')
|
||||
expect(assessMigration(fixture)).toMatchObject({
|
||||
shouldPrompt: true,
|
||||
reason: 'legacy-assets-detected',
|
||||
sourceRoot: fixture.legacy
|
||||
})
|
||||
|
||||
writeFixture(path.join(fixture.current, 'settings.json'), '{"dbRoot":"current"}')
|
||||
expect(assessMigration(fixture)).toMatchObject({
|
||||
shouldPrompt: false,
|
||||
reason: 'current-data-present'
|
||||
})
|
||||
})
|
||||
|
||||
it('copies Settings, Knowledge companions, Token, Provider keys and Agent credentials', async () => {
|
||||
const fixture = roots()
|
||||
const source = fixture.legacy
|
||||
const target = fixture.current
|
||||
writeFixture(path.join(source, 'settings.json'), '{"dbRoot":"legacy-db"}')
|
||||
writeFixture(path.join(source, 'ai-providers.json'), '{"version":1,"providers":[]}')
|
||||
writeFixture(path.join(source, 'local-api-token.bin'), 'legacy-encrypted-token')
|
||||
writeFixture(path.join(source, 'ai-provider-keys.bin'), 'legacy-encrypted-provider')
|
||||
|
||||
const knowledgeDirectory = path.join(source, 'knowledge', 'account-hash')
|
||||
fs.ensureDirSync(knowledgeDirectory)
|
||||
const knowledgePath = path.join(knowledgeDirectory, 'knowledge.sqlite')
|
||||
const database = new DatabaseSync(knowledgePath)
|
||||
database.exec('PRAGMA journal_mode=WAL')
|
||||
database.exec('PRAGMA wal_autocheckpoint=0')
|
||||
database.exec('CREATE TABLE messages (id INTEGER PRIMARY KEY, text TEXT NOT NULL)')
|
||||
database.exec("INSERT INTO messages(text) VALUES ('migration fixture')")
|
||||
expect(fs.existsSync(`${knowledgePath}-wal`)).toBe(true)
|
||||
expect(fs.existsSync(`${knowledgePath}-shm`)).toBe(true)
|
||||
|
||||
const agentLegacy = path.join(root, 'home', '.wechatexplorer', 'accounts')
|
||||
const agentCurrent = path.join(root, 'home', '.tracememo', 'accounts')
|
||||
writeFixture(path.join(agentLegacy, 'account.json'), '{"token":"credential"}')
|
||||
writeFixture(path.join(agentLegacy, 'account.sync.json'), '{"cursor":"1"}')
|
||||
|
||||
try {
|
||||
const result = await executeMigration(source, target, {
|
||||
decryptLegacySecrets: async () => ({
|
||||
token: 'A'.repeat(43),
|
||||
aiProviderKeys: { version: 1, keys: { provider: 'provider-secret' } },
|
||||
databaseKeys: {},
|
||||
failures: []
|
||||
}),
|
||||
agentRoots: () => ({ legacy: agentLegacy, current: agentCurrent }),
|
||||
now: () => new Date('2026-08-11T00:00:00.000Z')
|
||||
})
|
||||
|
||||
expect(result.state.status).toBe('completed')
|
||||
expect(fs.readFileSync(path.join(target, 'settings.json'), 'utf8')).toContain('legacy-db')
|
||||
expect(
|
||||
fs.existsSync(path.join(target, 'knowledge', 'account-hash', 'knowledge.sqlite'))
|
||||
).toBe(true)
|
||||
expect(
|
||||
fs.existsSync(path.join(target, 'knowledge', 'account-hash', 'knowledge.sqlite-wal'))
|
||||
).toBe(true)
|
||||
expect(
|
||||
fs.existsSync(path.join(target, 'knowledge', 'account-hash', 'knowledge.sqlite-shm'))
|
||||
).toBe(true)
|
||||
expect(
|
||||
Buffer.from(fs.readFileSync(path.join(target, 'local-api-token.bin')))
|
||||
.reverse()
|
||||
.toString('utf8')
|
||||
).toBe('A'.repeat(43))
|
||||
expect(
|
||||
JSON.parse(
|
||||
Buffer.from(fs.readFileSync(path.join(target, 'ai-provider-keys.bin')))
|
||||
.reverse()
|
||||
.toString('utf8')
|
||||
)
|
||||
).toEqual({ version: 1, keys: { provider: 'provider-secret' } })
|
||||
expect(fs.readFileSync(path.join(agentCurrent, 'account.json'), 'utf8')).toContain(
|
||||
'credential'
|
||||
)
|
||||
expect(fs.existsSync(path.join(source, 'settings.json'))).toBe(true)
|
||||
expect(
|
||||
fs.existsSync(path.join(source, 'knowledge', 'account-hash', 'knowledge.sqlite'))
|
||||
).toBe(true)
|
||||
} finally {
|
||||
database.close()
|
||||
}
|
||||
})
|
||||
|
||||
it('is idempotent and does not overwrite existing TraceMemo assets', async () => {
|
||||
const fixture = roots()
|
||||
writeFixture(path.join(fixture.legacy, 'settings.json'), '{"dbRoot":"legacy"}')
|
||||
writeFixture(path.join(fixture.current, 'settings.json'), '{"dbRoot":"current"}')
|
||||
|
||||
const result = await executeMigration(fixture.legacy, fixture.current, {
|
||||
decryptLegacySecrets: async () => ({ databaseKeys: {}, failures: [] }),
|
||||
agentRoots: () => ({
|
||||
legacy: path.join(root, 'agent-legacy'),
|
||||
current: path.join(root, 'agent-current')
|
||||
}),
|
||||
now: () => new Date('2026-08-11T00:00:00.000Z')
|
||||
})
|
||||
|
||||
expect(result.state.items['settings.json']).toBe('skipped')
|
||||
expect(fs.readFileSync(path.join(fixture.current, 'settings.json'), 'utf8')).toContain(
|
||||
'current'
|
||||
)
|
||||
expect(fs.readFileSync(path.join(fixture.legacy, 'settings.json'), 'utf8')).toContain('legacy')
|
||||
})
|
||||
})
|
||||
@@ -39,11 +39,11 @@ describe('production runtime packaging', () => {
|
||||
|
||||
it('requires the bundled Reader Skill declared by extraResources', () => {
|
||||
const resources = join(root, 'reader-skill-resources')
|
||||
const skillPath = join(resources, 'skill', 'wechatexplorer-reader', 'SKILL.md')
|
||||
const skillPath = join(resources, 'skill', 'tracememo-reader', 'SKILL.md')
|
||||
const config = readFileSync(resolve(__dirname, '../../electron-builder.yml'), 'utf8')
|
||||
|
||||
expect(config).toContain('docs/skill/wechatexplorer-reader')
|
||||
expect(config).toContain('to: skill/wechatexplorer-reader')
|
||||
expect(config).toContain('docs/skill/tracememo-reader')
|
||||
expect(config).toContain('to: skill/tracememo-reader')
|
||||
expect(() => validateReaderSkillRuntime(resources)).toThrow(
|
||||
/Missing bundled TraceMemo Reader Skill/
|
||||
)
|
||||
|
||||
@@ -38,13 +38,13 @@ describe('Reader Skill resource resolution', () => {
|
||||
it('finds the repository Skill from the current working directory in development', () => {
|
||||
const root = fixtureRoot()
|
||||
const runtime = environment(root, false)
|
||||
const skillPath = join(runtime.cwd, 'docs', 'skill', 'wechatexplorer-reader', 'SKILL.md')
|
||||
const skillPath = join(runtime.cwd, 'docs', 'skill', 'tracememo-reader', 'SKILL.md')
|
||||
writeSkill(skillPath)
|
||||
|
||||
expect(resolveSkillResourceStatus(runtime)).toMatchObject({
|
||||
available: true,
|
||||
source: 'development',
|
||||
version: 'v1.1',
|
||||
version: 'v1.2',
|
||||
filePath: skillPath,
|
||||
directoryPath: dirname(skillPath)
|
||||
})
|
||||
@@ -63,12 +63,25 @@ describe('Reader Skill resource resolution', () => {
|
||||
expect(status).toMatchObject({
|
||||
available: true,
|
||||
source: 'development',
|
||||
version: 'v1.1',
|
||||
filePath: join(workspace, 'docs', 'skill', 'wechatexplorer-reader', 'SKILL.md')
|
||||
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)
|
||||
|
||||
expect(resolveSkillResourceStatus(runtime)).toMatchObject({
|
||||
available: true,
|
||||
source: 'bundled',
|
||||
filePath: skillPath
|
||||
})
|
||||
})
|
||||
|
||||
it('falls back to the legacy Skill directory for one compatibility release', () => {
|
||||
const root = fixtureRoot()
|
||||
const runtime = environment(root, true)
|
||||
const skillPath = join(runtime.resourcesPath, 'skill', 'wechatexplorer-reader', 'SKILL.md')
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import fs from 'fs-extra'
|
||||
import crypto from 'crypto'
|
||||
import os from 'os'
|
||||
import path from 'path'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
@@ -114,6 +115,28 @@ describe('WCDB message shard pagination', () => {
|
||||
|
||||
expect(first).toBe(second)
|
||||
expect(first).not.toContain('微信聊天记录')
|
||||
expect(first).toContain(path.join('TraceMemo', 'path-bridges'))
|
||||
expect(fs.realpathSync(first)).toBe(fs.realpathSync(accountRoot))
|
||||
})
|
||||
|
||||
it('reuses an existing legacy Windows path bridge without creating a new one', () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'wxe-legacy-path-bridge-'))
|
||||
temporaryDirectories.push(root)
|
||||
const publicRoot = path.join(root, 'Public')
|
||||
const accountRoot = path.join(root, '微信聊天记录', 'wxid_legacy')
|
||||
fs.ensureDirSync(path.join(accountRoot, 'db_storage'))
|
||||
const bridgeId = crypto
|
||||
.createHash('sha256')
|
||||
.update(path.resolve(accountRoot).toLowerCase())
|
||||
.digest('hex')
|
||||
.slice(0, 24)
|
||||
const legacyBridge = path.join(publicRoot, 'WechatExplorer', 'path-bridges', bridgeId)
|
||||
fs.ensureDirSync(path.dirname(legacyBridge))
|
||||
fs.symlinkSync(accountRoot, legacyBridge, 'junction')
|
||||
|
||||
expect(resolveWindowsNativeAccountRoot(accountRoot, { platform: 'win32', publicRoot })).toBe(
|
||||
legacyBridge
|
||||
)
|
||||
expect(fs.existsSync(path.join(publicRoot, 'TraceMemo'))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user