import { flush } from "solid-js"; import { beforeEach, describe, expect, test, vi } from "vitest"; import type { GraphEvent } from "../../src/lib/bindings"; import { createGraphView } from "../../src/lib/graph"; import { linearHistory } from "../fixtures/rows"; const requestMore = vi.fn(async () => {}); /** Lets a test push events as if the Rust side had sent them. */ let send: (event: GraphEvent) => void; vi.mock("../../src/lib/api", () => ({ streamGraph: vi.fn(async (_id: string, _budget: number, onEvent: (e: GraphEvent) => void) => { send = onEvent; return { requestMore }; }), })); beforeEach(() => { requestMore.mockClear(); requestMore.mockImplementation(async () => {}); }); describe("createGraphView", () => { test("starts with nothing and does nothing", () => { const view = createGraphView(); expect(view.rows()).toEqual([]); expect(view.status()).toBe("idle"); }); test("collects the rows the backend pushes", async () => { const view = createGraphView(); await view.show("repo"); send({ event: "rows", data: { start: 0, rows: linearHistory(10) } }); await flush(); expect(view.rows()).toHaveLength(10); expect(view.status()).toBe("streaming"); }); test("appends later batches in order", async () => { const view = createGraphView(); await view.show("repo"); send({ event: "rows", data: { start: 0, rows: linearHistory(5) } }); send({ event: "rows", data: { start: 5, rows: linearHistory(10).slice(5) } }); await flush(); expect(view.rows().map((row) => row.row)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); }); test("places a batch by the index it declares, not by what arrived before", async () => { // `start` exists so a batch lands correctly even if the frontend's idea of // how many rows it has does not match the backend's. const view = createGraphView(); await view.show("repo"); send({ event: "rows", data: { start: 0, rows: linearHistory(10) } }); await flush(); send({ event: "rows", data: { start: 4, rows: linearHistory(6).slice(4) } }); await flush(); expect(view.rows()).toHaveLength(6); expect(view.rows().at(-1)?.row).toBe(5); }); test("knows when the history is fully drawn", async () => { const view = createGraphView(); await view.show("repo"); send({ event: "rows", data: { start: 0, rows: linearHistory(3) } }); send({ event: "exhausted", data: { total: 3, degraded: false } }); await flush(); expect(view.status()).toBe("complete"); expect(view.message()).toBeUndefined(); }); test("says so when the ordering had to be approximated", async () => { // Silently drawing a slightly wrong graph would be worse than admitting it. const view = createGraphView(); await view.show("repo"); send({ event: "exhausted", data: { total: 0, degraded: true } }); await flush(); expect(view.message()).toContain("skewed"); }); test("surfaces a failure from the walk", async () => { const view = createGraphView(); await view.show("repo"); send({ event: "failed", data: { message: "the odb went away" } }); await flush(); expect(view.status()).toBe("failed"); expect(view.message()).toBe("the odb went away"); }); test("asks for more rows when told to", async () => { const view = createGraphView(); await view.show("repo"); send({ event: "rows", data: { start: 0, rows: linearHistory(10) } }); await flush(); view.loadMore(); expect(requestMore).toHaveBeenCalledTimes(1); }); test("does not ask again while a request is still in flight", async () => { // The scroll sentinel fires repeatedly; without this the backend would be // asked for the same rows many times over. const view = createGraphView(); await view.show("repo"); send({ event: "rows", data: { start: 0, rows: linearHistory(10) } }); await flush(); view.loadMore(); view.loadMore(); view.loadMore(); expect(requestMore).toHaveBeenCalledTimes(1); }); test("asks again once the rows it was waiting for arrive", async () => { const view = createGraphView(); await view.show("repo"); send({ event: "rows", data: { start: 0, rows: linearHistory(10) } }); await flush(); view.loadMore(); send({ event: "rows", data: { start: 10, rows: linearHistory(20).slice(10) } }); await flush(); view.loadMore(); expect(requestMore).toHaveBeenCalledTimes(2); }); test("stops asking once the history is complete", async () => { const view = createGraphView(); await view.show("repo"); send({ event: "exhausted", data: { total: 0, degraded: false } }); await flush(); view.loadMore(); expect(requestMore).not.toHaveBeenCalled(); }); test("reports a failed request rather than getting stuck", async () => { const view = createGraphView(); await view.show("repo"); send({ event: "rows", data: { start: 0, rows: linearHistory(10) } }); await flush(); requestMore.mockRejectedValueOnce({ kind: "workerStopped", message: "worker stopped" }); view.loadMore(); await flush(); await Promise.resolve(); expect(view.status()).toBe("failed"); expect(view.message()).toBe("worker stopped"); }); test("clears what was there when a different repository is shown", async () => { const view = createGraphView(); await view.show("first"); send({ event: "rows", data: { start: 0, rows: linearHistory(10) } }); await flush(); await view.show("second"); await flush(); expect(view.rows()).toEqual([]); }); });