mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-09-09 19:31:57 +02:00
When the selected response's version no longer matches the live request, the response header grows a "Request Changed" control with View Diff and Restore. It follows the GraphQL editor's shape — a state-labelled dropdown button rather than a new icon in the action row — because the label is the whole point: it has to say *that* something is different before it offers to do anything about it. Hidden when the two agree, and for responses recorded before versioning existed, which have no version and never will. The diff reuses the Git dialog's DiffViewer over YAML renderings of the two documents, so a request diff reads like a commit diff. Restore flushes pending edits first (the backend restores from the database, so a debounced keystroke would otherwise land on top of it) and calls wasUpdatedExternally afterwards, because the write came from this window and the store's echo suppression would leave open editors showing the old content. EditSessionTracker turns the boundaries only the frontend can see — switching requests, losing focus, closing, falling idle for 60s — into snapshot calls. It tracks no content of its own: content addressing already decides whether a boundary had anything behind it, which is what keeps this a timer and two assignments instead of a change-tracking system.
76 lines
2.2 KiB
TypeScript
76 lines
2.2 KiB
TypeScript
import type { ModelVersionReason } from "@yaakapp-internal/models";
|
|
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
import { EditSessionTracker } from "./editSessionTracker";
|
|
|
|
type Capture = [requestId: string, reason: ModelVersionReason];
|
|
|
|
function tracker(idleMs = 1000) {
|
|
const captured: Capture[] = [];
|
|
return {
|
|
captured,
|
|
tracker: new EditSessionTracker((id, reason) => captured.push([id, reason]), idleMs),
|
|
};
|
|
}
|
|
|
|
describe("EditSessionTracker", () => {
|
|
beforeEach(() => vi.useFakeTimers());
|
|
afterEach(() => vi.useRealTimers());
|
|
|
|
test("captures a request once it has been left alone", () => {
|
|
const { tracker: t, captured } = tracker();
|
|
t.noteEdit("rq_1");
|
|
|
|
vi.advanceTimersByTime(999);
|
|
expect(captured).toEqual([]);
|
|
|
|
vi.advanceTimersByTime(1);
|
|
expect(captured).toEqual([["rq_1", "idle"]]);
|
|
});
|
|
|
|
test("a burst of edits is one capture, not one per keystroke", () => {
|
|
const { tracker: t, captured } = tracker();
|
|
for (let i = 0; i < 10; i++) {
|
|
t.noteEdit("rq_1");
|
|
vi.advanceTimersByTime(500);
|
|
}
|
|
expect(captured).toEqual([]);
|
|
|
|
vi.advanceTimersByTime(1000);
|
|
expect(captured).toEqual([["rq_1", "idle"]]);
|
|
});
|
|
|
|
test("captures the request being left, not the one being opened", () => {
|
|
const { tracker: t, captured } = tracker();
|
|
t.noteActiveRequest("rq_1");
|
|
expect(captured).toEqual([]);
|
|
|
|
t.noteActiveRequest("rq_2");
|
|
expect(captured).toEqual([["rq_1", "switch"]]);
|
|
});
|
|
|
|
test("re-selecting the same request is not a boundary", () => {
|
|
const { tracker: t, captured } = tracker();
|
|
t.noteActiveRequest("rq_1");
|
|
t.noteActiveRequest("rq_1");
|
|
expect(captured).toEqual([]);
|
|
});
|
|
|
|
test("blur and close capture the request still on screen", () => {
|
|
const { tracker: t, captured } = tracker();
|
|
t.noteActiveRequest("rq_1");
|
|
t.noteBoundary();
|
|
t.noteBoundary();
|
|
expect(captured).toEqual([
|
|
["rq_1", "switch"],
|
|
["rq_1", "switch"],
|
|
]);
|
|
});
|
|
|
|
test("nothing is captured before a request is open", () => {
|
|
const { tracker: t, captured } = tracker();
|
|
t.noteBoundary();
|
|
t.noteActiveRequest(null);
|
|
expect(captured).toEqual([]);
|
|
});
|
|
});
|