Files
kandev/apps/web/lib/state/slices/session/walkthroughs.test.ts
T
José Almeida 3e237e177a feat: add agent-authored code walkthroughs (#1647)
* feat: add code-walkthrough backend (MCP tool, service, persistence, WS events)

* feat: render agent code walkthroughs as line-anchored cards + editor window

* test: add code-walkthrough re-emit mock-agent scenario

* fix: rework code-walkthrough UX (floating card, line reveal, minimize, clickable file)

* fix: improve code walkthrough editor UX

* fix: backfill walkthrough launcher after websocket connect

* fix: improve walkthrough requests and editor anchors

* fix: clear walkthrough highlights on close

* feat: add walkthrough discard and prompt customization

* fix: send walkthrough requests through saved prompt

* fix: support postgres built-in prompt seeding

* fix: address walkthrough review feedback

* fix: address walkthrough review follow-ups

* fix: harden walkthrough lifecycle races

* fix: reuse walkthrough notification cleanup

* fix: guard stale walkthrough file opens

* fix: abort stale file opens before paint

* fix: stabilize github settings e2e readiness

* fix: use stable github settings e2e selector
2026-07-10 08:34:57 +01:00

138 lines
4.9 KiB
TypeScript

import { beforeEach, describe, it, expect, vi } from "vitest";
import { create } from "zustand";
import { immer } from "zustand/middleware/immer";
import { createSessionSlice } from "./session-slice";
import type { SessionSlice } from "./types";
import type { TaskWalkthrough, WalkthroughStep } from "@/lib/types/http";
const mockGetPlanLastSeen = vi.fn();
const mockSetPlanLastSeen = vi.fn();
const mockGetWalkthroughLastSeen = vi.fn();
const mockSetWalkthroughLastSeen = vi.fn();
vi.mock("@/lib/local-storage", () => ({
getPlanLastSeen: (...args: unknown[]) => mockGetPlanLastSeen(...args),
setPlanLastSeen: (...args: unknown[]) => mockSetPlanLastSeen(...args),
}));
vi.mock("@/lib/walkthrough-notification-storage", () => ({
getWalkthroughLastSeen: (...args: unknown[]) => mockGetWalkthroughLastSeen(...args),
setWalkthroughLastSeen: (...args: unknown[]) => mockSetWalkthroughLastSeen(...args),
}));
function makeStore() {
return create<SessionSlice>()(immer(createSessionSlice));
}
const TASK_ID = "task-1";
const TS_EPOCH = "2026-04-20T00:00:00Z";
const TS_LATER = "2026-04-20T01:00:00Z";
function steps(n: number): WalkthroughStep[] {
return Array.from({ length: n }, (_, i) => ({
file: `f${i}.go`,
line: i + 1,
text: `step ${i}`,
}));
}
function makeWalkthrough(overrides: Partial<TaskWalkthrough> = {}): TaskWalkthrough {
return {
id: "wt-1",
task_id: TASK_ID,
title: "Walkthrough",
steps: steps(3),
created_by: "agent",
created_at: TS_EPOCH,
updated_at: TS_EPOCH,
...overrides,
};
}
describe("walkthrough slice", () => {
beforeEach(() => {
vi.clearAllMocks();
mockGetPlanLastSeen.mockReturnValue(null);
mockGetWalkthroughLastSeen.mockReturnValue(null);
});
it("setWalkthrough stores the tour and defaults the active step to 0", () => {
const store = makeStore();
store.getState().setWalkthrough(TASK_ID, makeWalkthrough());
expect(store.getState().walkthroughs.byTaskId[TASK_ID]?.steps).toHaveLength(3);
expect(store.getState().walkthroughs.activeStepByTaskId[TASK_ID]).toBe(0);
});
it("setWalkthroughActiveStep clamps within [0, len-1]", () => {
const store = makeStore();
store.getState().setWalkthrough(TASK_ID, makeWalkthrough());
store.getState().setWalkthroughActiveStep(TASK_ID, 99);
expect(store.getState().walkthroughs.activeStepByTaskId[TASK_ID]).toBe(2);
store.getState().setWalkthroughActiveStep(TASK_ID, -5);
expect(store.getState().walkthroughs.activeStepByTaskId[TASK_ID]).toBe(0);
});
it("replacing with a shorter tour clamps the active step back into range", () => {
const store = makeStore();
store.getState().setWalkthrough(TASK_ID, makeWalkthrough());
store.getState().setWalkthroughActiveStep(TASK_ID, 2);
store.getState().setWalkthrough(TASK_ID, makeWalkthrough({ steps: steps(1) }));
expect(store.getState().walkthroughs.activeStepByTaskId[TASK_ID]).toBe(0);
});
it("replacing with a different tour resets the active step", () => {
const store = makeStore();
store.getState().setWalkthrough(TASK_ID, makeWalkthrough());
store.getState().setWalkthroughActiveStep(TASK_ID, 2);
store.getState().setWalkthrough(TASK_ID, makeWalkthrough({ id: "wt-2" }));
expect(store.getState().walkthroughs.activeStepByTaskId[TASK_ID]).toBe(0);
});
it("setWalkthrough(null) resets the active step to 0", () => {
const store = makeStore();
store.getState().setWalkthrough(TASK_ID, makeWalkthrough());
store.getState().setWalkthroughActiveStep(TASK_ID, 2);
store.getState().setWalkthrough(TASK_ID, null);
expect(store.getState().walkthroughs.byTaskId[TASK_ID]).toBeNull();
expect(store.getState().walkthroughs.activeStepByTaskId[TASK_ID]).toBe(0);
});
it("markWalkthroughSeen records the current updated_at", () => {
const store = makeStore();
store.getState().setWalkthrough(TASK_ID, makeWalkthrough({ updated_at: TS_LATER }));
store.getState().markWalkthroughSeen(TASK_ID);
expect(store.getState().walkthroughs.lastSeenUpdatedAtByTaskId[TASK_ID]).toBe(TS_LATER);
expect(mockSetWalkthroughLastSeen).toHaveBeenCalledWith(TASK_ID, TS_LATER);
});
it("setWalkthrough hydrates stored lastSeenUpdatedAtByTaskId", () => {
mockGetWalkthroughLastSeen.mockReturnValue(TS_LATER);
const store = makeStore();
store.getState().setWalkthrough(TASK_ID, makeWalkthrough({ updated_at: TS_LATER }));
expect(store.getState().walkthroughs.lastSeenUpdatedAtByTaskId[TASK_ID]).toBe(TS_LATER);
});
it("setWalkthrough does not advance lastSeenUpdatedAtByTaskId on update", () => {
const store = makeStore();
store.getState().setWalkthrough(TASK_ID, makeWalkthrough({ updated_at: TS_EPOCH }));
store.getState().markWalkthroughSeen(TASK_ID);
store.getState().setWalkthrough(TASK_ID, makeWalkthrough({ updated_at: TS_LATER }));
expect(store.getState().walkthroughs.lastSeenUpdatedAtByTaskId[TASK_ID]).toBe(TS_EPOCH);
});
});