Files
openchamber/packages/web/server/lib/walkthrough/routes.test.js
T
Bohdan Triapitsyn 34d0ff7383 feat(walkthrough): guided AI walkthrough for diffs, branches, and PRs (#2572)
A diff is ordered by file path, which is almost never the order in which a
change makes sense. This adds a Walkthrough surface that reorders it: the model
groups related hunks into stops, explains what each group changes about
behavior, and orders the stops so each builds on the last. It explains and
orders; judging code stays with the existing Review action.

Reviews uncommitted work (all, staged, unstaged), a branch against its base, or
a pull request. Generation is always user-initiated — nothing runs on a timer,
on a file change, or as a side effect of opening a panel.

Invariants worth preserving:

- Hunk identity is derived on the server and only there. Ids are content
  hashes, so an anchor that no longer resolves is proof the code it described
  changed, and staleness needs no heuristics. The client matches ids to ids and
  never recomputes them; two implementations would have to agree forever.
- The digest is never truncated. A diff that does not fit the model's context
  is refused with an actionable reason, because a walkthrough written against
  half a diff reads as confident and is wrong.
- Nothing disappears. Lockfiles and other generated output are excluded from
  the model's input by name — never by size — and everything no stop covers is
  listed at the end, so "have I seen all of it" stays answerable.
- Cost is explicit. Results are content-addressed, so returning the working
  tree to an earlier state costs nothing; generation outlives its request, so a
  refresh detaches the client rather than discarding paid-for work, and only an
  explicit cancel stops it.

Supporting changes to shared modules:

- git: expose the existing getRangeDiff as GET /api/git
  listUntrackedPaths and getUntrackedDiffs. The latter resolve the repository
  once for a batch instead of per file, taking a panel
  ~340ms on an 80-file working tree.
- small-model: structured output across four wire forma
  and abort signal, and an onOverflow policy so an oversized prompt fails
  loudly instead of being silently clipped. A provider
  remembered so the prompt-side fallback goes first next time.
- models.dev metadata: surface structured_output as tri
  false blocks a model, a missing field does not, because the catalog omits it
  for roughly half of all models.

Desktop and tablet only: VS Code serves Git through its
these routes, and the mobile shell does not consume the surface registry.

Docs: packages/docs walkthrough page in English and all eight locales.
2026-08-02 16:22:55 +03:00

111 lines
3.6 KiB
JavaScript

import express from 'express';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { registerWalkthroughRoutes } from './routes.js';
// These run over real HTTP on purpose. The bug this file exists for was
// invisible to unit tests: the service and the store were both correct, and the
// response was dropped by a disconnect check that misread a healthy request.
const SOURCE = { kind: 'working-tree', scope: 'all' };
describe('walkthrough routes', () => {
let server;
let base;
let releaseJob;
let job;
const service = {
async getWalkthrough() {
return { walkthrough: null, hunks: [], hunkCount: 0, generating: Boolean(job) };
},
async generateWalkthrough() {
if (job) return job;
job = new Promise((resolve) => {
releaseJob = () => resolve({ walkthrough: { title: 'DONE' }, hunks: [], hunkCount: 1 });
}).finally(() => { job = null; });
return job;
},
async cancelWalkthroughGeneration() {
return { cancelled: Boolean(job) };
},
};
const generate = (signal) => fetch(`${base}/api/walkthrough/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ directory: '/repo', source: SOURCE }),
signal,
});
beforeEach(async () => {
job = null;
releaseJob = undefined;
const app = express();
app.use(express.json());
registerWalkthroughRoutes(app, { getWalkthroughService: async () => service });
server = app.listen(0);
await new Promise((resolve) => server.once('listening', resolve));
base = `http://127.0.0.1:${server.address().port}`;
});
afterEach(async () => {
await new Promise((resolve) => server.close(resolve));
});
it('answers a generation request that nobody interrupted', async () => {
const pending = generate();
await new Promise((resolve) => setTimeout(resolve, 20));
releaseJob();
const body = await (await pending).json();
expect(body.walkthrough).toEqual({ title: 'DONE' });
});
it('delivers the result to a client that reconnected after a refresh', async () => {
const controller = new AbortController();
generate(controller.signal).catch(() => {});
await new Promise((resolve) => setTimeout(resolve, 20));
controller.abort();
await new Promise((resolve) => setTimeout(resolve, 20));
// The reloaded page sees work in progress and re-attaches to it.
const read = await (await fetch(
`${base}/api/walkthrough?directory=/repo&source=${encodeURIComponent(JSON.stringify(SOURCE))}`,
)).json();
expect(read.generating).toBe(true);
const reattached = generate();
await new Promise((resolve) => setTimeout(resolve, 20));
releaseJob();
const body = await (await reattached).json();
expect(body.walkthrough).toEqual({ title: 'DONE' });
});
it('rejects a request without a directory before touching the service', async () => {
const response = await fetch(`${base}/api/walkthrough/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ source: SOURCE }),
});
expect(response.status).toBe(400);
expect(job).toBeNull();
});
it('cancels through its own endpoint rather than a dropped connection', async () => {
generate().catch(() => {});
await new Promise((resolve) => setTimeout(resolve, 20));
const response = await fetch(`${base}/api/walkthrough/cancel`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ directory: '/repo', source: SOURCE }),
});
expect(await response.json()).toEqual({ cancelled: true });
releaseJob();
});
});