Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions core/__tests__/generate-review-walkthrough.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { expect, test, vi } from 'vite-plus/test';
import { generateReviewWalkthrough } from '../lib/generate-review-walkthrough.ts';
import type { RepositoryState, ReviewCommitEvolution } from '../types.ts';
import { createChangedFile } from './helpers/fixtures.ts';

const baseState = {
branch: 'feature',
files: [createChangedFile('src/app.ts')],
generatedAt: 1,
launchPath: '/repo',
root: '/repo',
source: {
number: 1,
provider: 'github',
type: 'pull-request',
url: 'https://github.com/nkzw-tech/codiff/pull/1',
},
} satisfies RepositoryState;

const draft = {
chapters: [
{
blurb: 'Review the change.',
icon: 'gear',
id: 'c1',
stops: [
{
hunkIds: ['h1'],
id: 's1',
importance: 'critical',
prose: 'Check this.',
title: 'Main change',
},
],
title: 'Core',
},
],
focus: 'Focus',
kind: 'narrative',
title: 'Walkthrough',
version: 4,
};

test('generateReviewWalkthrough whole-diff path prompts once and normalizes', async () => {
const runModel = vi.fn(async () => ({ draft }));
const result = await generateReviewWalkthrough({
agent: 'codex',
runModel,
states: { whole: baseState },
structure: 'whole-diff',
});
expect(result.status).toBe('ready');
if (result.status !== 'ready') {
return;
}
expect(runModel).toHaveBeenCalledTimes(1);
expect(result.walkthrough.kind).toBe('narrative');
expect(result.plan.structure).toBe('whole-diff');
});

test('generateReviewWalkthrough units path fans out and composes', async () => {
const evolution = {
recommendation: {
rationale: 'Multiple units changed.',
suggestedStructure: 'commit-by-commit',
},
summary: {
absorbedIntoBase: 0,
added: 2,
ambiguous: 0,
pairingCoverage: 0,
removed: 0,
retained: 0,
reviewable: 2,
revised: 0,
rewrittenSamePatch: 0,
},
units: [
{
after: {
authoredAt: '2026-01-01T00:00:00.000Z',
authorName: 'Ada',
parentIds: [],
sha: 'a'.repeat(40),
shortSha: 'aaaaaaa',
subject: 'one',
},
confidence: 'exact',
id: 'introduced:a',
kind: 'introduced',
order: 0,
reviewable: true,
},
{
after: {
authoredAt: '2026-01-01T01:00:00.000Z',
authorName: 'Ada',
parentIds: [],
sha: 'b'.repeat(40),
shortSha: 'bbbbbbb',
subject: 'two',
},
confidence: 'exact',
id: 'introduced:b',
kind: 'introduced',
order: 1,
reviewable: true,
},
],
} satisfies ReviewCommitEvolution;

const runModel = vi.fn(async () => ({ draft }));
const unitState = {
...baseState,
files: [createChangedFile('src/unit.ts')],
} satisfies RepositoryState;

const result = await generateReviewWalkthrough({
agent: 'codex',
evolution,
runModel,
states: {
byUnitId: {
'introduced:a': unitState,
'introduced:b': unitState,
},
whole: baseState,
},
structure: 'units',
});

expect(result.status).toBe('ready');
if (result.status !== 'ready') {
return;
}
expect(runModel).toHaveBeenCalledTimes(2);
expect(result.plan.structure).toBe('units');
expect(result.walkthrough.chapters.length).toBeGreaterThan(0);
expect(result.walkthrough.title).toContain('Commit-by-commit');
});

test('generateReviewWalkthrough fails clearly without whole state', async () => {
const result = await generateReviewWalkthrough({
agent: 'codex',
runModel: async () => ({ draft }),
states: {},
structure: 'whole-diff',
});
expect(result).toEqual({
reason: 'Whole-diff walkthrough generation requires a whole RepositoryState.',
status: 'failed',
});
});
113 changes: 113 additions & 0 deletions core/__tests__/review-history.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { expect, test } from 'vite-plus/test';
import {
commitRevisionLabel,
diffComparison,
diffRange,
resolveReviewPlan,
reviewableUnits,
revisionRef,
reviewVersionOption,
versionOptionHeadCommitId,
versionRevisionLabel,
} from '../lib/review-history.ts';
import type { ReviewEvolutionUnit } from '../types.ts';

const base = revisionRef('a'.repeat(40), commitRevisionLabel('base'));
const headOld = revisionRef('b'.repeat(40), versionRevisionLabel('v1'));
const headNew = revisionRef('c'.repeat(40), versionRevisionLabel('v2'));

const before = diffRange(base, headOld);
const after = diffRange(base, headNew);
const comparison = diffComparison(before, after);

const units: ReadonlyArray<ReviewEvolutionUnit> = [
{
after: {
authoredAt: '2026-01-01T00:00:00.000Z',
authorName: 'A',
parentIds: [base.commitId],
sha: headNew.commitId,
shortSha: headNew.commitId.slice(0, 7),
subject: 'feat: add',
},
confidence: 'high',
id: 'introduced-1',
kind: 'introduced',
order: 0,
reviewable: true,
},
{
after: {
authoredAt: '2026-01-01T00:00:00.000Z',
authorName: 'A',
parentIds: [base.commitId],
sha: headOld.commitId,
shortSha: headOld.commitId.slice(0, 7),
subject: 'same',
},
before: {
authoredAt: '2026-01-01T00:00:00.000Z',
authorName: 'A',
parentIds: [base.commitId],
sha: headOld.commitId,
shortSha: headOld.commitId.slice(0, 7),
subject: 'same',
},
confidence: 'exact',
id: 'retained-1',
kind: 'retained',
order: 1,
reviewable: false,
},
];

test('projects version options through DiffRange identity', () => {
const version = reviewVersionOption({
createdAt: '2026-01-02T00:00:00.000Z',
id: '2',
number: 2,
range: after,
});
expect(versionOptionHeadCommitId(version)).toBe(headNew.commitId);
expect(version.range.base.commitId).toBe(base.commitId);
});

test('filters non-reviewable evolution markers from unit plans', () => {
expect(reviewableUnits(units).map((unit) => unit.kind)).toEqual(['introduced']);
});

test('resolves whole-diff and unit plans from recommendations', () => {
expect(
resolveReviewPlan({
comparison,
recommendation: {
rationale: 'Low pairing confidence.',
suggestedStructure: 'whole-diff',
},
units,
}),
).toMatchObject({ structure: 'whole-diff' });

expect(
resolveReviewPlan({
comparison,
recommendation: {
rationale: 'Review introduced units.',
suggestedStructure: 'commit-by-commit',
},
structure: 'auto',
units,
}),
).toMatchObject({
structure: 'units',
units: [{ kind: 'introduced' }],
});

expect(
resolveReviewPlan({
comparison,
structure: 'whole-diff',
units,
}),
).toMatchObject({ structure: 'whole-diff' });
});
Loading