What changed: ZCode 3.14 has a 数据存储路径 (data storage path) setting. Changing it copies ~/.zcode/v2 to <folder>/.zcode/v2 (all but setting.json, copyDataDirectory's filter) and from the next start ZCode reads credentials.json and runtime/provider from there. The choice stays in the home folder's .zcode/v2/setting.json as dataBaseDir (readBootstrapDataBaseDirFromDisk), and setting.json itself is always read from the home folder (getSettingsFile: ZCODE_DESKTOP_HOME_DIR, HOME, USERPROFILE). The plugin read credentials.json and runtime/provider from ~/.zcode/v2 only, so for such a user "ZCode app's sign-in" read the stale copy left at home (or nothing). It now reads dataBaseDir as magpie's built-in does (magpie 41972b25f, internal/appdir ZCodeDataIn: trimmed, ignored when empty, not a string or not absolute) for credentials.json and the zcode-builtin.json walk, and keeps reading setting.json (the team plan) from the home folder. The credential key's seed is unchanged: ZCode builds it from os.homedir(), not the data folder. Read from ZCode 3.14.3's app.asar (a copy): out/main/index.js (bootstrap), out/main/chunk-*.js and out/host/chunk-*.js (getDataBaseDir, getAppConfigDir, copyDataDirectory, getCredentialsFile, the settings file under the home folder, defaultCredentialSecret). What was run: - moved.test.mjs (new): with dataBaseDir set, ownSignIn takes the moved folder's key and session over the stale home copy, and a team plan named in the home setting.json; "", blank, relative, number and null keep the home folder. With zcodeDir put back on homedir() it fails (old key read). - HOME=$(mktemp -d) bun test at the repo root: 1185 pass, 0 fail. - bun scripts/check.mjs zcode. - Not run: a real ZCode with a moved folder, Windows.
59 lines
3.0 KiB
JavaScript
59 lines
3.0 KiB
JavaScript
// ZCode 3.14 can move its data to another folder (数据存储路径): it copies
|
|
// ~/.zcode/v2 there, all but setting.json, records the folder in the home
|
|
// folder's setting.json as dataBaseDir, and from then on reads
|
|
// credentials.json from the new folder. "ZCode app's sign-in" follows it, as
|
|
// magpie's built-in does (internal/appdir ZCodeDataIn), and still takes the
|
|
// team plan from the home folder's setting.json, where ZCode keeps it.
|
|
import { test, expect, beforeAll } from "bun:test"
|
|
import { homedir, tmpdir } from "node:os"
|
|
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs"
|
|
import { createCipheriv, createHash, randomBytes } from "node:crypto"
|
|
import { join } from "node:path"
|
|
|
|
let _internal
|
|
beforeAll(async () => {
|
|
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
|
|
process.env.ZCODE_CREDENTIAL_SECRET = "test-seed"
|
|
;({ _internal } = await import("./index.mjs"))
|
|
})
|
|
|
|
function enc(text) {
|
|
const key = createHash("sha256").update("test-seed").digest()
|
|
const iv = randomBytes(12)
|
|
const c = createCipheriv("aes-256-gcm", key, iv)
|
|
const data = Buffer.concat([c.update(text, "utf8"), c.final()])
|
|
return "enc:v1:" + [iv, c.getAuthTag(), data].map((b) => b.toString("base64url")).join(".")
|
|
}
|
|
const writeStore = (v2, store) => {
|
|
mkdirSync(v2, { recursive: true })
|
|
writeFileSync(join(v2, "credentials.json"), JSON.stringify(Object.fromEntries(Object.entries(store).map(([k, v]) => [k, enc(v)]))))
|
|
}
|
|
const KEY = "account-provider:coding-plan:account:zai-individual-coding-plan:account:u1:api-key"
|
|
|
|
test("ZCode's sign-in is read from the folder its data was moved to", () => {
|
|
const homeV2 = join(homedir(), ".zcode", "v2")
|
|
const moved = mkdtempSync(join(tmpdir(), "zcode-moved-"))
|
|
const movedV2 = join(moved, ".zcode", "v2")
|
|
const setting = (s) => writeFileSync(join(homeV2, "setting.json"), JSON.stringify(s))
|
|
try {
|
|
// what ZCode leaves after the move: the old copy at home, the live one moved
|
|
writeStore(homeV2, { [KEY]: "old.stale", zcodejwttoken: "j-old" })
|
|
writeStore(movedV2, { [KEY]: "new.live", zcodejwttoken: "j-new", "oauth:zai:access_token": "tok-team" })
|
|
setting({ locale: "zh-CN", dataBaseDir: moved })
|
|
expect(_internal.ownSignIn()).toMatchObject({ key: "new.live", jwt: "j-new" })
|
|
|
|
// the team plan ZCode switched to stays in the home folder's setting.json
|
|
setting({ dataBaseDir: " " + moved + " ", providerFamilyConnectionSelections: { zai: { kind: "team-coding-plan", organizationId: "org1", projectId: "p1" } } })
|
|
expect(_internal.ownSignIn()).toMatchObject({ token: "tok-team", org: "org1", project: "p1", jwt: "j-new" })
|
|
|
|
// an empty, blank, relative or non-string dataBaseDir is no move
|
|
for (const d of ["", " ", "ZCodeData", 7, null]) {
|
|
setting({ dataBaseDir: d })
|
|
expect(_internal.ownSignIn()).toMatchObject({ key: "old.stale", jwt: "j-old" })
|
|
}
|
|
} finally {
|
|
rmSync(moved, { recursive: true, force: true })
|
|
rmSync(join(homeV2, "setting.json"), { force: true })
|
|
}
|
|
})
|