From 93e09875ecad2a1b9fae9b11ca4fd569710f81af Mon Sep 17 00:00:00 2001 From: nav Date: Thu, 9 Apr 2026 01:32:49 +0000 Subject: [PATCH] test: cover corrupted client state --- tests/state-and-rules.test.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/state-and-rules.test.ts b/tests/state-and-rules.test.ts index efea902..e0b1bc2 100644 --- a/tests/state-and-rules.test.ts +++ b/tests/state-and-rules.test.ts @@ -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 { join } from "node:path"; @@ -13,6 +13,7 @@ import { hasClientSecret, loadYonexusClientState, saveYonexusClientState, + YonexusClientStateCorruptionError, type YonexusClientState } from "../plugin/core/state.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.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", () => {