| 1 | |
| 2 | |
| 3 | import { createHash } from 'node:crypto'; |
| 4 | import { mkdir, readFile, writeFile } from 'node:fs/promises'; |
| 5 | import { dirname, join, resolve } from 'node:path'; |
| 6 | import { pathToFileURL } from 'node:url'; |
| 7 | |
| 8 | const entries = [ |
| 9 | '.claude-cage/config.json', |
| 10 | '.claude-cage/environment.json', |
| 11 | '.claude-cage/account.json', |
| 12 | '.claude-cage/password-vault.json', |
| 13 | '.claude-cage/favorites.json', |
| 14 | '.claude-cage/projects.json', |
| 15 | '.claude-cage/workbench.json', |
| 16 | '.claude-cage/appearance.json', |
| 17 | '.claude-cage/webview2/Default/Local Storage/leveldb/000001.log', |
| 18 | '.claude-cage/webkit/data/WebsiteData/LocalStorage/storage.sqlite3', |
| 19 | '.claude-cage/state/lima/persistent/disk', |
| 20 | '.claude-cage-workspace/project.txt', |
| 21 | ]; |
| 22 | const digest = (data) => createHash('sha256').update(data).digest('hex'); |
| 23 | |
| 24 | export async function seed(home, manifest) { |
| 25 | const records = {}; |
| 26 | for (const name of entries) { |
| 27 | const path = join(home, name); |
| 28 | await mkdir(dirname(path), { recursive: true, mode: 0o700 }); |
| 29 | const bytes = Buffer.from(`Cage package test: preserve ${name}\n历史、收藏与登录数据\n\0\xff`, 'utf8'); |
| 30 | await writeFile(path, bytes, { flag: 'wx', mode: 0o600 }); |
| 31 | records[name] = digest(bytes); |
| 32 | } |
| 33 | await writeFile(manifest, JSON.stringify(records), { flag: 'wx', mode: 0o600 }); |
| 34 | } |
| 35 | |
| 36 | export async function verify(home, manifest) { |
| 37 | const records = JSON.parse(await readFile(manifest, 'utf8')); |
| 38 | for (const [name, expected] of Object.entries(records)) { |
| 39 | if (digest(await readFile(join(home, name))) !== expected) { |
| 40 | throw new Error(`Installation changed personal data: ${name}`); |
| 41 | } |
| 42 | } |
| 43 | return Object.keys(records).length; |
| 44 | } |
| 45 | |
| 46 | if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) { |
| 47 | const [operation, home, manifest] = process.argv.slice(2); |
| 48 | if (!home || !manifest || !['seed', 'verify'].includes(operation)) { |
| 49 | throw new Error('Usage: personal-data-fixture.mjs seed|verify TEST_HOME MANIFEST'); |
| 50 | } |
| 51 | if (operation === 'seed') await seed(home, manifest); |
| 52 | else console.log(`PASS: ${await verify(home, manifest)} personal data files preserved byte for byte.`); |
| 53 | } |