feat(KB-651): add task file diffs API
- Add API endpoint to fetch git diffs for tasks in review - Implement file diff routes with support for changed files view - Add comprehensive test suite for file diffs endpoint - Update dashboard API client for diff fetching capabilities
This commit is contained in:
276
packages/dashboard/src/__tests__/routes-file-diffs.test.ts
Normal file
276
packages/dashboard/src/__tests__/routes-file-diffs.test.ts
Normal file
@@ -0,0 +1,276 @@
|
||||
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 { createServer } from "../server.js";
|
||||
import * as childProcess from "node:child_process";
|
||||
import * as fs from "node:fs";
|
||||
|
||||
vi.mock("node:child_process", async () => {
|
||||
const actual = await vi.importActual<typeof import("node:child_process")>("node:child_process");
|
||||
return {
|
||||
...actual,
|
||||
execSync: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("node:fs", async () => {
|
||||
const actual = await vi.importActual<typeof import("node:fs")>("node:fs");
|
||||
return {
|
||||
...actual,
|
||||
existsSync: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
const mockExecSync = vi.mocked(childProcess.execSync);
|
||||
const mockExistsSync = vi.mocked(fs.existsSync);
|
||||
|
||||
class MockStore extends EventEmitter {
|
||||
private tasks = new Map<string, Task>();
|
||||
|
||||
getRootDir(): string {
|
||||
return process.cwd();
|
||||
}
|
||||
|
||||
getMissionStore() {
|
||||
return {
|
||||
listMissions: vi.fn().mockResolvedValue([]),
|
||||
createMission: vi.fn(),
|
||||
getMission: vi.fn(),
|
||||
updateMission: vi.fn(),
|
||||
deleteMission: vi.fn(),
|
||||
listTemplates: vi.fn().mockResolvedValue([]),
|
||||
createTemplate: vi.fn(),
|
||||
getTemplate: vi.fn(),
|
||||
updateTemplate: vi.fn(),
|
||||
deleteTemplate: vi.fn(),
|
||||
instantiateMission: vi.fn(),
|
||||
};
|
||||
}
|
||||
|
||||
async listTasks(): Promise<Task[]> {
|
||||
return Array.from(this.tasks.values());
|
||||
}
|
||||
|
||||
async getTask(id: string): Promise<Task> {
|
||||
const task = this.tasks.get(id);
|
||||
if (!task) {
|
||||
const error = Object.assign(new Error("Task not found"), { code: "ENOENT" });
|
||||
throw error;
|
||||
}
|
||||
return task;
|
||||
}
|
||||
|
||||
addTask(task: Task): void {
|
||||
this.tasks.set(task.id, task);
|
||||
}
|
||||
}
|
||||
|
||||
function createTask(overrides: Partial<Task> = {}): Task {
|
||||
return {
|
||||
id: "KB-651",
|
||||
title: "Test task",
|
||||
description: "Test description",
|
||||
column: "in-progress",
|
||||
dependencies: [],
|
||||
steps: [],
|
||||
currentStep: 0,
|
||||
log: [],
|
||||
createdAt: "2026-04-01T00:00:00.000Z",
|
||||
updatedAt: "2026-04-01T00:00:00.000Z",
|
||||
columnMovedAt: "2026-04-01T00:00:00.000Z",
|
||||
worktree: "/tmp/kb-651",
|
||||
baseBranch: "main",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
async function requestFileDiffs(port: number, taskId = "KB-651"): Promise<{ status: number; body: any }> {
|
||||
return await new Promise((resolve, reject) => {
|
||||
const req = http.request(
|
||||
{
|
||||
hostname: "127.0.0.1",
|
||||
port,
|
||||
path: `/api/tasks/${taskId}/file-diffs`,
|
||||
method: "GET",
|
||||
},
|
||||
(res) => {
|
||||
let data = "";
|
||||
res.on("data", (chunk) => (data += chunk));
|
||||
res.on("end", () => resolve({ status: res.statusCode!, body: JSON.parse(data) }));
|
||||
},
|
||||
);
|
||||
req.on("error", reject);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
describe("GET /api/tasks/:id/file-diffs", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockExistsSync.mockImplementation((path) => path === "/tmp/kb-651");
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-04-01T12:00:00.000Z"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("returns changed files with per-file diffs and supports rename metadata", async () => {
|
||||
const store = new MockStore();
|
||||
store.addTask(createTask({ baseBranch: "main" }));
|
||||
|
||||
mockExecSync.mockImplementation((command) => {
|
||||
const cmd = String(command);
|
||||
if (cmd === "git diff --name-status main...HEAD") {
|
||||
return "M\tsrc/updated.ts\nA\tsrc/added.ts\nD\tsrc/deleted.ts\nR100\tsrc/old-name.ts\tsrc/new-name.ts\n" as any;
|
||||
}
|
||||
if (cmd === "git diff main...HEAD -- \"src/updated.ts\"") {
|
||||
return "diff --git a/src/updated.ts b/src/updated.ts\n--- a/src/updated.ts\n+++ b/src/updated.ts\n+hello\n" as any;
|
||||
}
|
||||
if (cmd === "git diff main...HEAD -- \"src/added.ts\"") {
|
||||
return "diff --git a/src/added.ts b/src/added.ts\nnew file mode 100644\n+++ b/src/added.ts\n+added\n" as any;
|
||||
}
|
||||
if (cmd === "git diff main...HEAD -- \"src/deleted.ts\"") {
|
||||
return "diff --git a/src/deleted.ts b/src/deleted.ts\n--- a/src/deleted.ts\n+++ /dev/null\n-deleted\n" as any;
|
||||
}
|
||||
if (cmd === "git diff main...HEAD -- \"src/new-name.ts\"") {
|
||||
return "diff --git a/src/old-name.ts b/src/new-name.ts\nsimilarity index 100%\nrename from src/old-name.ts\nrename to src/new-name.ts\n" as any;
|
||||
}
|
||||
throw new Error(`Unexpected command: ${cmd}`);
|
||||
});
|
||||
|
||||
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 requestFileDiffs(port);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(mockExecSync.mock.calls.map(([cmd]) => String(cmd))).toEqual([
|
||||
"git diff --name-status main...HEAD",
|
||||
'git diff main...HEAD -- "src/updated.ts"',
|
||||
'git diff main...HEAD -- "src/added.ts"',
|
||||
'git diff main...HEAD -- "src/deleted.ts"',
|
||||
'git diff main...HEAD -- "src/new-name.ts"',
|
||||
]);
|
||||
expect(response.body).toEqual([
|
||||
{ path: "src/updated.ts", status: "modified", diff: expect.stringContaining("+hello") },
|
||||
{ path: "src/added.ts", status: "added", diff: expect.stringContaining("+added") },
|
||||
{ path: "src/deleted.ts", status: "deleted", diff: expect.stringContaining("-deleted") },
|
||||
{
|
||||
path: "src/new-name.ts",
|
||||
status: "renamed",
|
||||
oldPath: "src/old-name.ts",
|
||||
diff: expect.stringContaining("rename from src/old-name.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 }));
|
||||
|
||||
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 requestFileDiffs(port);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual([]);
|
||||
expect(mockExecSync).not.toHaveBeenCalled();
|
||||
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
});
|
||||
|
||||
it("falls back to HEAD diff when base branch diff fails", async () => {
|
||||
const store = new MockStore();
|
||||
store.addTask(createTask({ baseBranch: "main" }));
|
||||
|
||||
mockExecSync.mockImplementation((command) => {
|
||||
const cmd = String(command);
|
||||
if (cmd === "git diff --name-status main...HEAD") {
|
||||
throw new Error("bad base branch");
|
||||
}
|
||||
if (cmd === "git diff --name-status HEAD") {
|
||||
return "M\tsrc/local.ts\n" as any;
|
||||
}
|
||||
if (cmd === "git diff HEAD -- \"src/local.ts\"") {
|
||||
return "diff --git a/src/local.ts b/src/local.ts\n+local\n" as any;
|
||||
}
|
||||
throw new Error(`Unexpected command: ${cmd}`);
|
||||
});
|
||||
|
||||
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 requestFileDiffs(port);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(mockExecSync.mock.calls.map(([cmd]) => String(cmd))).toEqual([
|
||||
"git diff --name-status main...HEAD",
|
||||
"git diff --name-status HEAD",
|
||||
'git diff HEAD -- "src/local.ts"',
|
||||
]);
|
||||
expect(response.body).toEqual([{ path: "src/local.ts", status: "modified", diff: expect.stringContaining("+local") }]);
|
||||
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
});
|
||||
|
||||
it("uses the 10-second cache before recomputing", async () => {
|
||||
const store = new MockStore();
|
||||
store.addTask(createTask({ baseBranch: "main" }));
|
||||
|
||||
mockExecSync.mockImplementation((command) => {
|
||||
const cmd = String(command);
|
||||
if (cmd === "git diff --name-status main...HEAD") {
|
||||
return "M\tsrc/cached.ts\n" as any;
|
||||
}
|
||||
if (cmd === "git diff main...HEAD -- \"src/cached.ts\"") {
|
||||
return "diff --git a/src/cached.ts b/src/cached.ts\n+cached\n" as any;
|
||||
}
|
||||
throw new Error(`Unexpected command: ${cmd}`);
|
||||
});
|
||||
|
||||
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 requestFileDiffs(port);
|
||||
const second = await requestFileDiffs(port);
|
||||
|
||||
expect(first.body).toEqual([{ path: "src/cached.ts", status: "modified", diff: expect.stringContaining("+cached") }]);
|
||||
expect(second.body).toEqual([{ path: "src/cached.ts", status: "modified", diff: expect.stringContaining("+cached") }]);
|
||||
expect(mockExecSync.mock.calls.map(([cmd]) => String(cmd))).toEqual([
|
||||
"git diff --name-status main...HEAD",
|
||||
'git diff main...HEAD -- "src/cached.ts"',
|
||||
]);
|
||||
|
||||
vi.advanceTimersByTime(10001);
|
||||
const third = await requestFileDiffs(port);
|
||||
|
||||
expect(third.body).toEqual([{ path: "src/cached.ts", status: "modified", diff: expect.stringContaining("+cached") }]);
|
||||
expect(mockExecSync.mock.calls.map(([cmd]) => String(cmd))).toEqual([
|
||||
"git diff --name-status main...HEAD",
|
||||
'git diff main...HEAD -- "src/cached.ts"',
|
||||
"git diff --name-status main...HEAD",
|
||||
'git diff main...HEAD -- "src/cached.ts"',
|
||||
]);
|
||||
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
});
|
||||
});
|
||||
@@ -1062,6 +1062,13 @@ export function createApiRoutes(store: TaskStore, options?: ServerOptions): Rout
|
||||
console.debug("[planning:routes:registered]", planningRoutes);
|
||||
}
|
||||
const sessionFilesCache = new Map<string, { files: string[]; expiresAt: number }>();
|
||||
const fileDiffsCache = new Map<
|
||||
string,
|
||||
{
|
||||
files: Array<{ path: string; status: "added" | "modified" | "deleted" | "renamed"; diff: string; oldPath?: string }>;
|
||||
expiresAt: number;
|
||||
}
|
||||
>();
|
||||
|
||||
// Get GitHub token from options or env
|
||||
const githubToken = options?.githubToken ?? process.env.GITHUB_TOKEN;
|
||||
@@ -6273,73 +6280,88 @@ Output ONLY the prompt text (no markdown, no explanations).`;
|
||||
|
||||
/**
|
||||
* GET /api/tasks/:id/file-diffs
|
||||
* Fetch simplified file diffs for a task.
|
||||
* Query: ?worktree=path
|
||||
* Returns: TaskFileDiff[]
|
||||
* Fetch changed files with individual git diffs for a task worktree.
|
||||
* Returns: Array<{ path, status, diff, oldPath? }>
|
||||
*/
|
||||
router.get("/tasks/:id/file-diffs", async (req, res) => {
|
||||
try {
|
||||
const task = store.getTask(req.params.id);
|
||||
if (!task) {
|
||||
res.status(404).json({ error: "Task not found" });
|
||||
const task = await store.getTask(req.params.id);
|
||||
if (!task.worktree || !existsSync(task.worktree)) {
|
||||
res.json([]);
|
||||
return;
|
||||
}
|
||||
|
||||
const worktree = typeof req.query.worktree === "string" ? req.query.worktree : undefined;
|
||||
const cwd = worktree || store.getRootDir();
|
||||
|
||||
// Get the base commit
|
||||
let baseCommit = "HEAD~1";
|
||||
|
||||
// Get the diff stat for file list
|
||||
const { execSync } = await import("node:child_process");
|
||||
|
||||
const filesOutput = execSync(`git diff --name-status ${baseCommit}..HEAD`, {
|
||||
encoding: "utf-8",
|
||||
cwd,
|
||||
timeout: 10000,
|
||||
});
|
||||
|
||||
const files: Array<{
|
||||
path: string;
|
||||
status: "added" | "modified" | "deleted";
|
||||
additions: number;
|
||||
deletions: number;
|
||||
patch: string;
|
||||
}> = [];
|
||||
|
||||
for (const line of filesOutput.trim().split("\n")) {
|
||||
if (!line.trim()) continue;
|
||||
|
||||
const parts = line.split("\t");
|
||||
const statusCode = parts[0];
|
||||
const filePath = parts[1];
|
||||
|
||||
let status: "added" | "modified" | "deleted";
|
||||
if (statusCode.startsWith("A")) status = "added";
|
||||
else if (statusCode.startsWith("D")) status = "deleted";
|
||||
else status = "modified";
|
||||
|
||||
let patch = "";
|
||||
try {
|
||||
patch = execSync(`git diff ${baseCommit}..HEAD -- "${filePath}"`, {
|
||||
encoding: "utf-8",
|
||||
cwd,
|
||||
timeout: 10000,
|
||||
});
|
||||
} catch {
|
||||
// Ignore errors for individual files
|
||||
}
|
||||
|
||||
const additions = (patch.match(/^\+[^+]/gm) || []).length;
|
||||
const deletions = (patch.match(/^-[^-]/gm) || []).length;
|
||||
|
||||
files.push({ path: filePath, status, additions, deletions, patch });
|
||||
const cached = fileDiffsCache.get(task.id);
|
||||
if (cached && cached.expiresAt > Date.now()) {
|
||||
res.json(cached.files);
|
||||
return;
|
||||
}
|
||||
|
||||
const baseBranch = task.baseBranch ?? "main";
|
||||
const cwd = task.worktree;
|
||||
let filesOutput = "";
|
||||
let diffBase = `${baseBranch}...HEAD`;
|
||||
|
||||
try {
|
||||
filesOutput = execSync(`git diff --name-status ${diffBase}`, {
|
||||
cwd,
|
||||
encoding: "utf-8",
|
||||
timeout: 5000,
|
||||
}).trim();
|
||||
} catch {
|
||||
diffBase = "HEAD";
|
||||
filesOutput = execSync("git diff --name-status HEAD", {
|
||||
cwd,
|
||||
encoding: "utf-8",
|
||||
timeout: 5000,
|
||||
}).trim();
|
||||
}
|
||||
|
||||
const files = filesOutput
|
||||
? filesOutput.split("\n").filter(Boolean).map((line) => {
|
||||
const parts = line.split("\t");
|
||||
const statusCode = parts[0] ?? "M";
|
||||
let status: "added" | "modified" | "deleted" | "renamed" = "modified";
|
||||
let path = parts[1] ?? "";
|
||||
let oldPath: string | undefined;
|
||||
|
||||
if (statusCode.startsWith("A")) {
|
||||
status = "added";
|
||||
} else if (statusCode.startsWith("D")) {
|
||||
status = "deleted";
|
||||
} else if (statusCode.startsWith("R")) {
|
||||
status = "renamed";
|
||||
oldPath = parts[1];
|
||||
path = parts[2] ?? parts[1] ?? "";
|
||||
}
|
||||
|
||||
let diff = "";
|
||||
try {
|
||||
diff = execSync(`git diff ${diffBase} -- "${path}"`, {
|
||||
cwd,
|
||||
encoding: "utf-8",
|
||||
timeout: 5000,
|
||||
});
|
||||
} catch {
|
||||
diff = "";
|
||||
}
|
||||
|
||||
return oldPath ? { path, status, diff, oldPath } : { path, status, diff };
|
||||
})
|
||||
: [];
|
||||
|
||||
fileDiffsCache.set(task.id, {
|
||||
files,
|
||||
expiresAt: Date.now() + 10000,
|
||||
});
|
||||
|
||||
res.json(files);
|
||||
} catch (err: any) {
|
||||
res.status(500).json({ error: err.message });
|
||||
if (err.code === "ENOENT") {
|
||||
res.status(404).json({ error: `Task ${req.params.id} not found` });
|
||||
} else {
|
||||
res.status(500).json({ error: err.message || "Internal server error" });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user