test: cover corrupted client state

This commit is contained in:
nav
2026-04-09 01:32:49 +00:00
parent 65c1f92cc1
commit 93e09875ec

View File

@@ -1,4 +1,4 @@
import { mkdtemp, readFile, rm } from "node:fs/promises"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import { join } from "node:path"; import { join } from "node:path";
@@ -13,6 +13,7 @@ import {
hasClientSecret, hasClientSecret,
loadYonexusClientState, loadYonexusClientState,
saveYonexusClientState, saveYonexusClientState,
YonexusClientStateCorruptionError,
type YonexusClientState type YonexusClientState
} from "../plugin/core/state.js"; } from "../plugin/core/state.js";
import { signMessage, verifySignature } from "../plugin/crypto/keypair.js"; import { signMessage, verifySignature } from "../plugin/crypto/keypair.js";
@@ -81,6 +82,23 @@ describe("Yonexus.Client state store", () => {
expect(second.state.privateKey).toBe(first.state.privateKey); expect(second.state.privateKey).toBe(first.state.privateKey);
expect(second.state.publicKey).toBe(first.state.publicKey); expect(second.state.publicKey).toBe(first.state.publicKey);
}); });
it("SR-06: raises a corruption error for malformed client state files", async () => {
const filePath = await createTempStatePath();
await saveYonexusClientState(filePath, {
...createInitialClientState("client-a"),
updatedAt: 1_710_000_000
});
await writeFile(filePath, '{"version":1,"identifier":"client-a","updatedAt":"bad"}\n', "utf8");
await expect(loadYonexusClientState(filePath, "client-a")).rejects.toBeInstanceOf(
YonexusClientStateCorruptionError
);
await expect(loadYonexusClientState(filePath, "client-a")).rejects.toThrow(
"invalid updatedAt"
);
});
}); });
describe("Yonexus.Client rule registry", () => { describe("Yonexus.Client rule registry", () => {