Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/gigit. git client
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181import { 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([]); });});