import type { ComputerUpdate } from "@rakazo/contracts"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createComputerUpdates } from "./computer-updates.js"; const update: ComputerUpdate = { id: "update-1", botId: "bot-1", name: "Writer", mode: "team", action: "update", status: "running", stage: "saving", }; afterEach(() => vi.useRealTimers()); describe("computer update presentation", () => { it("keeps a newly started update when an older poll returns, and closing does not stop polling", async () => { vi.useFakeTimers(); let resolve!: (rows: ComputerUpdate[]) => void; const list = vi.fn( () => new Promise((done) => { resolve = done; }), ); const store = createComputerUpdates({ list, start: async () => update, dismiss: async () => {}, releaseInterrupted: async () => {}, }); const stop = store.watch(); await store.start("bot-1"); resolve([]); await Promise.resolve(); expect(store.getSnapshot()).toEqual({ updates: [update], openId: update.id }); store.open(null); await vi.advanceTimersByTimeAsync(1500); resolve([{ ...update, stage: "restoring" }]); await Promise.resolve(); expect(store.getSnapshot()).toEqual({ updates: [{ ...update, stage: "restoring" }], openId: null, }); stop(); }); it("restores background progress on a new mount and ignores responses after disposal", async () => { vi.useFakeTimers(); const store = createComputerUpdates({ list: async () => [update], start: async () => update, dismiss: async () => {}, releaseInterrupted: async () => {}, }); let stop = store.watch(); await Promise.resolve(); expect(store.getSnapshot().updates).toEqual([update]); stop(); stop = store.watch(); stop(); await Promise.resolve(); expect(store.getSnapshot().updates).toEqual([]); }); });