feat(FN-4521): complete Step 2 — fix done lineage aggregation
Fusion-Task-Id: FN-4521 Fusion-Task-Lineage: 675472a2-480d-4b9e-8861-98d2d82680db
This commit is contained in:
148
packages/dashboard/src/__tests__/routes-diff-lineage.test.ts
Normal file
148
packages/dashboard/src/__tests__/routes-diff-lineage.test.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import type { Task, TaskCommitAssociation } from "@fusion/core";
|
||||
import { EventEmitter } from "node:events";
|
||||
import { createServer } from "../server.js";
|
||||
|
||||
class RealGitStore extends EventEmitter {
|
||||
private tasks = new Map<string, Task>();
|
||||
private associations = new Map<string, TaskCommitAssociation[]>();
|
||||
|
||||
constructor(private rootDir: string) {
|
||||
super();
|
||||
}
|
||||
|
||||
getRootDir(): string {
|
||||
return this.rootDir;
|
||||
}
|
||||
|
||||
getFusionDir(): string {
|
||||
return join(this.rootDir, ".fusion");
|
||||
}
|
||||
|
||||
getDatabase() {
|
||||
return {
|
||||
exec: () => {},
|
||||
prepare: () => ({ run: () => ({ changes: 0 }), get: () => undefined, all: () => [] }),
|
||||
};
|
||||
}
|
||||
|
||||
getMissionStore() {
|
||||
return {
|
||||
listMissions: async () => [],
|
||||
listTemplates: async () => [],
|
||||
};
|
||||
}
|
||||
|
||||
async listTasks(): Promise<Task[]> {
|
||||
return Array.from(this.tasks.values());
|
||||
}
|
||||
|
||||
getTask(id: string): Task | undefined {
|
||||
return this.tasks.get(id);
|
||||
}
|
||||
|
||||
addTask(task: Task): void {
|
||||
this.tasks.set(task.id, task);
|
||||
}
|
||||
|
||||
setAssociations(lineageId: string, associations: TaskCommitAssociation[]): void {
|
||||
this.associations.set(lineageId, associations);
|
||||
}
|
||||
|
||||
async getTaskCommitAssociationsByLineageId(lineageId: string): Promise<TaskCommitAssociation[]> {
|
||||
return this.associations.get(lineageId) ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
function git(cwd: string, ...args: string[]): string {
|
||||
return execFileSync("git", args, { cwd, encoding: "utf8" }).trim();
|
||||
}
|
||||
|
||||
function commitFile(cwd: string, file: string, content: string, message: string): string {
|
||||
writeFileSync(join(cwd, file), content);
|
||||
git(cwd, "add", file);
|
||||
git(cwd, "commit", "-m", message);
|
||||
return git(cwd, "rev-parse", "HEAD");
|
||||
}
|
||||
|
||||
describe("FN-4521 done-task lineage aggregation", () => {
|
||||
it("keeps lineage-only files and excludes interleaved non-lineage commits", async () => {
|
||||
const rootDir = mkdtempSync(join(tmpdir(), "fn-4521-lineage-"));
|
||||
|
||||
try {
|
||||
git(rootDir, "init", "-b", "main");
|
||||
git(rootDir, "config", "user.email", "fusion@example.com");
|
||||
git(rootDir, "config", "user.name", "Fusion");
|
||||
|
||||
commitFile(rootDir, "base.txt", "base\n", "A base");
|
||||
|
||||
git(rootDir, "checkout", "-b", "task-branch");
|
||||
const commitB = commitFile(rootDir, "a.ts", "export const a = 1;\n", "B task change a");
|
||||
|
||||
git(rootDir, "checkout", "main");
|
||||
commitFile(rootDir, "unrelated.ts", "export const unrelated = true;\n", "C foreign change");
|
||||
|
||||
git(rootDir, "checkout", "task-branch");
|
||||
git(rootDir, "merge", "main", "--no-edit");
|
||||
const commitD = commitFile(rootDir, "b.ts", "export const b = 2;\n", "D task change b");
|
||||
|
||||
git(rootDir, "checkout", "main");
|
||||
git(rootDir, "merge", "task-branch", "--no-ff", "-m", "M merge task branch");
|
||||
const mergeCommit = git(rootDir, "rev-parse", "HEAD");
|
||||
|
||||
const lineageId = "lin-fn-4521";
|
||||
const store = new RealGitStore(rootDir);
|
||||
store.addTask({
|
||||
id: "FN-4521",
|
||||
title: "lineage test",
|
||||
description: "lineage test",
|
||||
column: "done",
|
||||
dependencies: [],
|
||||
steps: [],
|
||||
currentStep: 0,
|
||||
log: [],
|
||||
createdAt: "2026-05-14T00:00:00.000Z",
|
||||
updatedAt: "2026-05-14T00:00:00.000Z",
|
||||
columnMovedAt: "2026-05-14T00:00:00.000Z",
|
||||
lineageId,
|
||||
baseBranch: "main",
|
||||
mergeDetails: { commitSha: mergeCommit, filesChanged: 2 },
|
||||
} as Task);
|
||||
|
||||
const mkAssoc = (sha: string, authoredAt: string): TaskCommitAssociation => ({
|
||||
lineageId,
|
||||
commitSha: sha,
|
||||
commitSubject: sha,
|
||||
authoredAt,
|
||||
matchedBy: "manual",
|
||||
confidence: 1,
|
||||
taskIdSnapshot: "FN-4521",
|
||||
note: null,
|
||||
createdAt: authoredAt,
|
||||
updatedAt: authoredAt,
|
||||
});
|
||||
|
||||
store.setAssociations(lineageId, [
|
||||
mkAssoc(commitB, "2026-05-14T00:00:01.000Z"),
|
||||
mkAssoc(commitD, "2026-05-14T00:00:02.000Z"),
|
||||
]);
|
||||
|
||||
const app = createServer(store as any);
|
||||
const { get } = await import("../test-request.js");
|
||||
const response = await get(app, "/api/tasks/FN-4521/diff");
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
const paths = response.body.files.map((f: { path: string }) => f.path).sort();
|
||||
expect(paths).toContain("a.ts");
|
||||
expect(paths).toContain("b.ts");
|
||||
// FN-4521 regression: pre-fix netRange (B^..merge) swept in unrelated.ts from interleaved commit C.
|
||||
expect(paths).not.toContain("unrelated.ts");
|
||||
} finally {
|
||||
rmSync(rootDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -304,7 +304,7 @@ describe("FN-4308 multi-commit done task aggregation", () => {
|
||||
expect(response.body.stats.filesChanged).toBe(2);
|
||||
});
|
||||
|
||||
it("falls back to commitSha enumeration when aggregation under-counts mergeDetails", async () => {
|
||||
it("keeps lineage aggregation when mergeDetails filesChanged is higher", async () => {
|
||||
const store = new MockStore();
|
||||
store.addTask(createTask({ column: "done", lineageId: "lin-1", mergeDetails: { commitSha: "merge", filesChanged: 3 } }));
|
||||
store.setAssociations("lin-1", [makeAssociation("assoc", "2026-04-01T00:00:00.000Z")]);
|
||||
@@ -335,7 +335,7 @@ describe("FN-4308 multi-commit done task aggregation", () => {
|
||||
const app = createServer(store as any);
|
||||
const response = await requestDiff(app);
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.files.map((f: any) => f.path).sort()).toEqual(["one.ts", "three.ts", "two.ts"]);
|
||||
expect(response.body.files.map((f: any) => f.path).sort()).toEqual(["a.txt", "b.txt"]);
|
||||
expect(response.body.files.length).toBe(response.body.stats.filesChanged);
|
||||
});
|
||||
|
||||
|
||||
@@ -412,7 +412,8 @@ async function collectDoneTaskFiles(task: DoneTaskAggregationTask, scopedStore:
|
||||
};
|
||||
}
|
||||
|
||||
const byPath = new Map<string, DoneTaskFileStatus>();
|
||||
const byPath = new Map<string, AggregatedDoneTaskFile>();
|
||||
let usedAggregation = false;
|
||||
|
||||
for (const sha of reachableShas) {
|
||||
let diffSpec: Awaited<ReturnType<typeof resolveCommitDiffSpec>>;
|
||||
@@ -429,34 +430,32 @@ async function collectDoneTaskFiles(task: DoneTaskAggregationTask, scopedStore:
|
||||
continue;
|
||||
}
|
||||
|
||||
if (filesForSha.length > 0) {
|
||||
usedAggregation = true;
|
||||
}
|
||||
|
||||
for (const file of filesForSha) {
|
||||
const existing = byPath.get(file.path);
|
||||
if (!existing || statusPriority(file.status) > statusPriority(existing)) {
|
||||
byPath.set(file.path, file.status);
|
||||
if (!existing) {
|
||||
byPath.set(file.path, { ...file });
|
||||
continue;
|
||||
}
|
||||
|
||||
const representative = (file.additions + file.deletions) > (existing.additions + existing.deletions) ? file.patch : existing.patch;
|
||||
const status = statusPriority(file.status) > statusPriority(existing.status) ? file.status : existing.status;
|
||||
|
||||
byPath.set(file.path, {
|
||||
path: file.path,
|
||||
status,
|
||||
additions: existing.additions + file.additions,
|
||||
deletions: existing.deletions + file.deletions,
|
||||
patch: representative,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const earliestSha = reachableShas[0];
|
||||
const latestSha = reachableShas[reachableShas.length - 1];
|
||||
if (!earliestSha || !latestSha) {
|
||||
return {
|
||||
files: [],
|
||||
stats: { filesChanged: 0, additions: 0, deletions: 0 },
|
||||
usedAggregation: false,
|
||||
};
|
||||
}
|
||||
const files = Array.from(byPath.values());
|
||||
|
||||
let earliestParent = EMPTY_TREE_SHA;
|
||||
try {
|
||||
earliestParent = (await runGitCommand(["rev-parse", `${earliestSha}^`], rootDir, 5000)).trim() || EMPTY_TREE_SHA;
|
||||
} catch {
|
||||
earliestParent = EMPTY_TREE_SHA;
|
||||
}
|
||||
|
||||
const netRange = `${earliestParent}..${latestSha}`;
|
||||
const netFiles = await collectDoneRangeFiles(netRange, rootDir).catch(() => []);
|
||||
const files = netFiles.map((file) => ({ ...file, status: byPath.get(file.path) ?? file.status }));
|
||||
return {
|
||||
files,
|
||||
stats: {
|
||||
@@ -464,7 +463,7 @@ async function collectDoneTaskFiles(task: DoneTaskAggregationTask, scopedStore:
|
||||
additions: files.reduce((sum, file) => sum + file.additions, 0),
|
||||
deletions: files.reduce((sum, file) => sum + file.deletions, 0),
|
||||
},
|
||||
usedAggregation: true,
|
||||
usedAggregation,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -605,7 +604,7 @@ export function registerSessionDiffRoutes(router: Router, deps: SessionDiffRoute
|
||||
|
||||
const aggregated = await collectDoneTaskFiles(doneTaskForDiff, scopedStore);
|
||||
const expectedFilesChanged = task.mergeDetails?.filesChanged ?? 0;
|
||||
const aggregationLooksComplete = expectedFilesChanged <= 0 || aggregated.stats.filesChanged === expectedFilesChanged;
|
||||
const aggregationLooksComplete = expectedFilesChanged <= 0 || aggregated.stats.filesChanged >= expectedFilesChanged;
|
||||
|
||||
if (aggregated.usedAggregation && aggregated.files.length > 0 && aggregationLooksComplete) {
|
||||
res.json({
|
||||
@@ -824,7 +823,7 @@ export function registerSessionDiffRoutes(router: Router, deps: SessionDiffRoute
|
||||
|
||||
const aggregated = await collectDoneTaskFiles(doneTaskForDiff, scopedStore);
|
||||
const expectedFilesChanged = task.mergeDetails?.filesChanged ?? 0;
|
||||
const aggregationLooksComplete = expectedFilesChanged <= 0 || aggregated.stats.filesChanged === expectedFilesChanged;
|
||||
const aggregationLooksComplete = expectedFilesChanged <= 0 || aggregated.stats.filesChanged >= expectedFilesChanged;
|
||||
|
||||
if (aggregated.usedAggregation && aggregated.files.length > 0 && aggregationLooksComplete) {
|
||||
res.json(aggregated.files.map((file) => ({ path: file.path, status: file.status, diff: file.patch })));
|
||||
|
||||
Reference in New Issue
Block a user