Files
yaak-mountain-loop/apps/yaak-client/lib/restoreRequestVersion.test.ts
Gregory SchierandClaude Opus 5.5 07662257c1 Put the restored request in the store before refreshing editors
Restore told open editors to re-read the request before its change event
had reached the store, so header and parameter rows could keep showing
the old content. The restore RPC now returns the restored request, and
the frontend puts it in the store before calling wasUpdatedExternally,
the same way deleteModel applies its change locally once the RPC
resolves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 09:57:40 -07:00

55 lines
2.1 KiB
TypeScript

import type { ModelVersion } from "@yaakapp-internal/models";
import { beforeEach, describe, expect, test, vi } from "vite-plus/test";
const mocks = vi.hoisted(() => ({
calls: [] as string[],
flushAllModelWrites: vi.fn(),
mergeModelsInStore: vi.fn(),
wasUpdatedExternally: vi.fn(),
rpc: vi.fn(),
}));
vi.mock("@yaakapp-internal/models", () => ({
flushAllModelWrites: mocks.flushAllModelWrites,
mergeModelsInStore: mocks.mergeModelsInStore,
}));
vi.mock("../hooks/useRequestUpdateKey", () => ({
wasUpdatedExternally: mocks.wasUpdatedExternally,
}));
vi.mock("./rpc", () => ({ rpc: mocks.rpc }));
vi.mock("./toast", () => ({ showToast: vi.fn() }));
const restored = { model: "http_request", id: "rq_1", url: "https://example.com/one" };
const version = { id: "mv_1", modelId: "rq_1" } as ModelVersion;
describe("applyRequestVersion", () => {
beforeEach(() => {
mocks.calls = [];
mocks.flushAllModelWrites.mockImplementation(async () => mocks.calls.push("flush"));
mocks.rpc.mockImplementation(async (cmd: string) => {
mocks.calls.push(cmd);
return restored;
});
mocks.mergeModelsInStore.mockImplementation(() => mocks.calls.push("store"));
mocks.wasUpdatedExternally.mockImplementation(() => mocks.calls.push("editors"));
});
test("puts the restored request in the store before editors re-read it", async () => {
const { applyRequestVersion } = await import("./restoreRequestVersion");
await applyRequestVersion(version);
expect(mocks.calls).toEqual(["flush", "models_restore_request_version", "store", "editors"]);
expect(mocks.mergeModelsInStore).toHaveBeenCalledWith("http_request", [restored]);
expect(mocks.wasUpdatedExternally).toHaveBeenCalledWith("rq_1");
});
test("a failed restore leaves the store and editors alone", async () => {
mocks.rpc.mockRejectedValue(new Error("no such version"));
const { applyRequestVersion } = await import("./restoreRequestVersion");
await expect(applyRequestVersion(version)).rejects.toThrow("no such version");
expect(mocks.mergeModelsInStore).not.toHaveBeenCalled();
expect(mocks.wasUpdatedExternally).not.toHaveBeenCalled();
});
});