|
| 1 | +// Memory Wiki tests cover the shared guarded vault page write helper. |
| 2 | +import { FsSafeError } from "openclaw/plugin-sdk/security-runtime"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import { writeGuardedVaultPage } from "./vault-page-write.js"; |
| 5 | + |
| 6 | +type FakeVault = Parameters<typeof writeGuardedVaultPage>[0]["vault"]; |
| 7 | + |
| 8 | +function fakeVault(write: () => Promise<void>): { |
| 9 | + vault: FakeVault; |
| 10 | + remove: ReturnType<typeof vi.fn>; |
| 11 | +} { |
| 12 | + const remove = vi.fn(async () => {}); |
| 13 | + return { vault: { write: vi.fn(write), remove } as unknown as FakeVault, remove }; |
| 14 | +} |
| 15 | + |
| 16 | +describe("writeGuardedVaultPage", () => { |
| 17 | + it("recovers from a transient concurrent-rewrite path-mismatch by retrying", async () => { |
| 18 | + let attempts = 0; |
| 19 | + const { vault } = fakeVault(async () => { |
| 20 | + attempts += 1; |
| 21 | + if (attempts < 3) { |
| 22 | + // A concurrent atomic rewrite replaces the page mid-operation. |
| 23 | + throw new FsSafeError("path-mismatch", "unable to resolve opened file path"); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + await expect( |
| 28 | + writeGuardedVaultPage({ |
| 29 | + vault, |
| 30 | + pagePath: "sources/page.md", |
| 31 | + content: "body", |
| 32 | + pageStat: null, |
| 33 | + pageLabel: "imported source page", |
| 34 | + }), |
| 35 | + ).resolves.toBeUndefined(); |
| 36 | + expect(attempts).toBe(3); |
| 37 | + }); |
| 38 | + |
| 39 | + it("rethrows a labeled error when the path-mismatch persists across attempts", async () => { |
| 40 | + const { vault } = fakeVault(async () => { |
| 41 | + throw new FsSafeError("path-mismatch", "unable to resolve opened file path"); |
| 42 | + }); |
| 43 | + |
| 44 | + await expect( |
| 45 | + writeGuardedVaultPage({ |
| 46 | + vault, |
| 47 | + pagePath: "sources/page.md", |
| 48 | + content: "body", |
| 49 | + pageStat: null, |
| 50 | + pageLabel: "imported source page", |
| 51 | + }), |
| 52 | + ).rejects.toThrow( |
| 53 | + /Refusing to write imported source page \(path-mismatch\): sources\/page\.md/u, |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it("does not retry persistent non-race guard failures and keeps fatal wording", async () => { |
| 58 | + const { vault } = fakeVault(async () => { |
| 59 | + throw new FsSafeError("not-file", "target is not a regular file"); |
| 60 | + }); |
| 61 | + |
| 62 | + await expect( |
| 63 | + writeGuardedVaultPage({ |
| 64 | + vault, |
| 65 | + pagePath: "concepts/page.md", |
| 66 | + content: "body", |
| 67 | + pageStat: null, |
| 68 | + pageLabel: "OKF concept page", |
| 69 | + }), |
| 70 | + ).rejects.toThrow(/Refusing to write OKF concept page \(not-file\): concepts\/page\.md/u); |
| 71 | + expect((vault as unknown as { write: ReturnType<typeof vi.fn> }).write).toHaveBeenCalledTimes( |
| 72 | + 1, |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("maps symlink and path-alias swaps to the symlink wording without retrying", async () => { |
| 77 | + const { vault } = fakeVault(async () => { |
| 78 | + throw new FsSafeError("symlink", "page resolved through a symlink"); |
| 79 | + }); |
| 80 | + |
| 81 | + await expect( |
| 82 | + writeGuardedVaultPage({ |
| 83 | + vault, |
| 84 | + pagePath: "sources/page.md", |
| 85 | + content: "body", |
| 86 | + pageStat: null, |
| 87 | + pageLabel: "imported source page", |
| 88 | + }), |
| 89 | + ).rejects.toThrow(/Refusing to write imported source page through symlink: sources\/page\.md/u); |
| 90 | + expect((vault as unknown as { write: ReturnType<typeof vi.fn> }).write).toHaveBeenCalledTimes( |
| 91 | + 1, |
| 92 | + ); |
| 93 | + }); |
| 94 | +}); |
0 commit comments