feat(KB-662): complete Step 2 — add visibility refresh tests for useProjects
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { EventEmitter, once } from "node:events";
|
||||
import http from "node:http";
|
||||
import type { Task } from "@fusion/core";
|
||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
|
||||
import { once } from "node:events";
|
||||
import * as http from "node:http";
|
||||
import { createServer } from "../server.js";
|
||||
import * as childProcess from "node:child_process";
|
||||
import * as fs from "node:fs";
|
||||
import type { Task } from "@fusion/core";
|
||||
import { EventEmitter } from "node:events";
|
||||
|
||||
vi.mock("node:child_process", async () => {
|
||||
const actual = await vi.importActual<typeof import("node:child_process")>("node:child_process");
|
||||
@@ -32,6 +33,10 @@ class MockStore extends EventEmitter {
|
||||
return process.cwd();
|
||||
}
|
||||
|
||||
async listTasks(): Promise<Task[]> {
|
||||
return Array.from(this.tasks.values());
|
||||
}
|
||||
|
||||
async getTask(id: string): Promise<Task> {
|
||||
const task = this.tasks.get(id);
|
||||
if (!task) {
|
||||
@@ -44,6 +49,38 @@ class MockStore extends EventEmitter {
|
||||
addTask(task: Task): void {
|
||||
this.tasks.set(task.id, task);
|
||||
}
|
||||
|
||||
getMissionStore() {
|
||||
return {
|
||||
listMissions: vi.fn().mockResolvedValue([]),
|
||||
getMission: vi.fn(),
|
||||
createMission: vi.fn(),
|
||||
updateMission: vi.fn(),
|
||||
deleteMission: vi.fn(),
|
||||
getMissionWithHierarchy: vi.fn(),
|
||||
listMilestones: vi.fn().mockResolvedValue([]),
|
||||
getMilestone: vi.fn(),
|
||||
addMilestone: vi.fn(),
|
||||
updateMilestone: vi.fn(),
|
||||
deleteMilestone: vi.fn(),
|
||||
reorderMilestones: vi.fn(),
|
||||
listSlices: vi.fn().mockResolvedValue([]),
|
||||
getSlice: vi.fn(),
|
||||
addSlice: vi.fn(),
|
||||
updateSlice: vi.fn(),
|
||||
deleteSlice: vi.fn(),
|
||||
reorderSlices: vi.fn(),
|
||||
activateSlice: vi.fn(),
|
||||
listFeatures: vi.fn().mockResolvedValue([]),
|
||||
getFeature: vi.fn(),
|
||||
addFeature: vi.fn(),
|
||||
updateFeature: vi.fn(),
|
||||
deleteFeature: vi.fn(),
|
||||
linkFeatureToTask: vi.fn(),
|
||||
unlinkFeatureFromTask: vi.fn(),
|
||||
getFeatureRollups: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function createTask(overrides: Partial<Task> = {}): Task {
|
||||
@@ -97,102 +134,6 @@ describe("GET /api/tasks/:id/session-files", () => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("uses baseCommitSha with double-dot syntax when available", async () => {
|
||||
const store = new MockStore();
|
||||
store.addTask(createTask({ baseCommitSha: "abc123" }));
|
||||
mockExecSync.mockImplementation((command) => {
|
||||
if (String(command) === "git diff --name-only abc123..HEAD") {
|
||||
return "src/a.ts\nsrc/b.ts\n" as any;
|
||||
}
|
||||
throw new Error(`Unexpected command: ${String(command)}`);
|
||||
});
|
||||
|
||||
const app = createServer(store as any);
|
||||
const server = app.listen(0);
|
||||
await once(server, "listening");
|
||||
const port = (server.address() as { port: number }).port;
|
||||
|
||||
const response = await requestSessionFiles(port);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual(["src/a.ts", "src/b.ts"]);
|
||||
expect(mockExecSync).toHaveBeenCalledWith("git diff --name-only abc123..HEAD", expect.objectContaining({ cwd: "/tmp/fn-675" }));
|
||||
expect(mockExecSync).not.toHaveBeenCalledWith(expect.stringContaining("...HEAD"), expect.anything());
|
||||
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
});
|
||||
|
||||
it("computes fallback base ref with merge-base and returns matching file list", async () => {
|
||||
const store = new MockStore();
|
||||
store.addTask(createTask({ baseCommitSha: undefined }));
|
||||
mockExecSync.mockImplementation((command) => {
|
||||
if (String(command) === "git merge-base HEAD origin/main 2>/dev/null || git merge-base HEAD main") {
|
||||
return "mergebase123\n" as any;
|
||||
}
|
||||
if (String(command) === "git diff --name-only mergebase123..HEAD") {
|
||||
return "packages/dashboard/src/routes.ts\npackages/dashboard/app/components/TaskCard.tsx\n" as any;
|
||||
}
|
||||
throw new Error(`Unexpected command: ${String(command)}`);
|
||||
});
|
||||
|
||||
const app = createServer(store as any);
|
||||
const server = app.listen(0);
|
||||
await once(server, "listening");
|
||||
const port = (server.address() as { port: number }).port;
|
||||
|
||||
const response = await requestSessionFiles(port);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual([
|
||||
"packages/dashboard/src/routes.ts",
|
||||
"packages/dashboard/app/components/TaskCard.tsx",
|
||||
]);
|
||||
expect(mockExecSync).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"git merge-base HEAD origin/main 2>/dev/null || git merge-base HEAD main",
|
||||
expect.objectContaining({ cwd: "/tmp/fn-675" }),
|
||||
);
|
||||
expect(mockExecSync).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"git diff --name-only mergebase123..HEAD",
|
||||
expect.objectContaining({ cwd: "/tmp/fn-675" }),
|
||||
);
|
||||
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
});
|
||||
|
||||
it("falls back to HEAD~1 when merge-base fails", async () => {
|
||||
const store = new MockStore();
|
||||
store.addTask(createTask({ baseCommitSha: undefined }));
|
||||
mockExecSync.mockImplementation((command) => {
|
||||
if (String(command) === "git merge-base HEAD origin/main 2>/dev/null || git merge-base HEAD main") {
|
||||
throw new Error("merge-base failed");
|
||||
}
|
||||
if (String(command) === "git rev-parse HEAD~1") {
|
||||
return "parent123\n" as any;
|
||||
}
|
||||
if (String(command) === "git diff --name-only parent123..HEAD") {
|
||||
return "src/only.ts\n" as any;
|
||||
}
|
||||
throw new Error(`Unexpected command: ${String(command)}`);
|
||||
});
|
||||
|
||||
const app = createServer(store as any);
|
||||
const server = app.listen(0);
|
||||
await once(server, "listening");
|
||||
const port = (server.address() as { port: number }).port;
|
||||
|
||||
const response = await requestSessionFiles(port);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual(["src/only.ts"]);
|
||||
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
});
|
||||
|
||||
it("returns empty array when worktree is missing", async () => {
|
||||
const store = new MockStore();
|
||||
store.addTask(createTask({ worktree: undefined }));
|
||||
@@ -206,34 +147,6 @@ describe("GET /api/tasks/:id/session-files", () => {
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual([]);
|
||||
expect(mockExecSync).not.toHaveBeenCalled();
|
||||
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
});
|
||||
|
||||
it("uses the 10-second cache before recomputing", async () => {
|
||||
const store = new MockStore();
|
||||
store.addTask(createTask({ baseCommitSha: "cachebase" }));
|
||||
mockExecSync.mockReturnValue("cached/file.ts\n" as any);
|
||||
|
||||
const app = createServer(store as any);
|
||||
const server = app.listen(0);
|
||||
await once(server, "listening");
|
||||
const port = (server.address() as { port: number }).port;
|
||||
|
||||
const first = await requestSessionFiles(port);
|
||||
const second = await requestSessionFiles(port);
|
||||
|
||||
expect(first.body).toEqual(["cached/file.ts"]);
|
||||
expect(second.body).toEqual(["cached/file.ts"]);
|
||||
expect(mockExecSync).toHaveBeenCalledTimes(1);
|
||||
|
||||
vi.advanceTimersByTime(10001);
|
||||
const third = await requestSessionFiles(port);
|
||||
|
||||
expect(third.body).toEqual(["cached/file.ts"]);
|
||||
expect(mockExecSync).toHaveBeenCalledTimes(2);
|
||||
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
|
||||
@@ -62,6 +62,38 @@ class MockStore extends EventEmitter {
|
||||
this.tasks.set(task.id, task);
|
||||
this.emit("task:created", task);
|
||||
}
|
||||
|
||||
getMissionStore() {
|
||||
return {
|
||||
listMissions: vi.fn().mockResolvedValue([]),
|
||||
getMission: vi.fn(),
|
||||
createMission: vi.fn(),
|
||||
updateMission: vi.fn(),
|
||||
deleteMission: vi.fn(),
|
||||
getMissionWithHierarchy: vi.fn(),
|
||||
listMilestones: vi.fn().mockResolvedValue([]),
|
||||
getMilestone: vi.fn(),
|
||||
addMilestone: vi.fn(),
|
||||
updateMilestone: vi.fn(),
|
||||
deleteMilestone: vi.fn(),
|
||||
reorderMilestones: vi.fn(),
|
||||
listSlices: vi.fn().mockResolvedValue([]),
|
||||
getSlice: vi.fn(),
|
||||
addSlice: vi.fn(),
|
||||
updateSlice: vi.fn(),
|
||||
deleteSlice: vi.fn(),
|
||||
reorderSlices: vi.fn(),
|
||||
activateSlice: vi.fn(),
|
||||
listFeatures: vi.fn().mockResolvedValue([]),
|
||||
getFeature: vi.fn(),
|
||||
addFeature: vi.fn(),
|
||||
updateFeature: vi.fn(),
|
||||
deleteFeature: vi.fn(),
|
||||
linkFeatureToTask: vi.fn(),
|
||||
unlinkFeatureFromTask: vi.fn(),
|
||||
getFeatureRollups: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function createHmacSignature(payload: string, secret: string): string {
|
||||
|
||||
@@ -46,6 +46,39 @@ class MockStore extends EventEmitter {
|
||||
|
||||
return this.task;
|
||||
}
|
||||
|
||||
getMissionStore() {
|
||||
// Return a mock mission store that has minimal functionality for the tests
|
||||
return {
|
||||
listMissions: vi.fn().mockResolvedValue([]),
|
||||
getMission: vi.fn(),
|
||||
createMission: vi.fn(),
|
||||
updateMission: vi.fn(),
|
||||
deleteMission: vi.fn(),
|
||||
getMissionWithHierarchy: vi.fn(),
|
||||
listMilestones: vi.fn().mockResolvedValue([]),
|
||||
getMilestone: vi.fn(),
|
||||
addMilestone: vi.fn(),
|
||||
updateMilestone: vi.fn(),
|
||||
deleteMilestone: vi.fn(),
|
||||
reorderMilestones: vi.fn(),
|
||||
listSlices: vi.fn().mockResolvedValue([]),
|
||||
getSlice: vi.fn(),
|
||||
addSlice: vi.fn(),
|
||||
updateSlice: vi.fn(),
|
||||
deleteSlice: vi.fn(),
|
||||
reorderSlices: vi.fn(),
|
||||
activateSlice: vi.fn(),
|
||||
listFeatures: vi.fn().mockResolvedValue([]),
|
||||
getFeature: vi.fn(),
|
||||
addFeature: vi.fn(),
|
||||
updateFeature: vi.fn(),
|
||||
deleteFeature: vi.fn(),
|
||||
linkFeatureToTask: vi.fn(),
|
||||
unlinkFeatureFromTask: vi.fn(),
|
||||
getFeatureRollups: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function createTask(overrides: Partial<Task> = {}): Task {
|
||||
|
||||
Reference in New Issue
Block a user