Files
archie/ui/test/chat-dock-float.test.ts
T
sam eb7d852567 refactor(ui/chat): remove chat features the server never sends
The dashboard talks only to the modules/chat API, which sends no slash
commands, model list, voice support, attachments or any question other
than an approval, so the UI built on those could never appear:

- the slash command menu, Retry, Undo, Branch and Preview compression in
  the More menu, and the /model, /update and /new handling
- the model picker, the update card and the api.update* calls
- voice recording and the task tags nothing ever added
- the questionnaire, picker and clarify cards, and the "Always this
  session" answer, so components/ui/questionnaire loses its last user
- channel.ts, command-scroll.ts and turn.ts, which nothing imported
- the old dock-storage migration and the second capitalised spelling of
  message and tool fields

Tasks are gone from the binary too, so chat links and prompts speak of
runs: the #N task-link step in the markdown renderer goes, since an agent
already links pages through dashboard_navigate.

state.ts keeps the shared state and actions; types.ts, stream.ts,
transcript.ts, escape.ts and dock-window.ts hold the types, the stream
reader, turn-to-message building, Escape layers and the dock's pointer
and keyboard handling. ChatDock.vue drops from 560 lines to 307.
2026-11-26 01:59:26 +11:00

171 lines
6.6 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
MAX_W,
MIN_H,
MIN_W,
barPlacement,
defaultLayout,
fitRect,
handleOrientation,
handleValue,
keyboardCover,
keyStep,
normalizeLayout,
phoneBarBottom,
pinnedWidth,
resizeRect,
snapEdge,
type DockLayout,
type ResizeDir,
} from "../src/chat/ChatDockLayout.ts";
const VW = 1280;
const VH = 800;
test("a stored layout is validated and clamped into the viewport", () => {
const cases: Array<[unknown, DockLayout]> = [
[null, defaultLayout(VW, VH)],
[{}, defaultLayout(VW, VH)],
[
{ mode: "right", x: 4000, y: -50, w: 10, h: 99999 },
{ mode: "right", x: 1280 - MIN_W - 8, y: 8, w: MIN_W, h: 800 - 16 },
],
[
{ mode: "float", x: 100, y: 120, w: 440, h: 560 },
{ mode: "float", x: 100, y: 120, w: 440, h: 560 },
],
[
{ mode: "sideways", x: "10", y: Number.NaN, w: 440, h: 560 },
{ ...defaultLayout(VW, VH), w: 440, h: 560 },
],
];
for (const [raw, want] of cases) {
assert.deepEqual(normalizeLayout(raw, VW, VH), want, JSON.stringify(raw));
}
});
test("fitRect keeps the whole rectangle inside the viewport at any size", () => {
const cases: Array<[[number, number, number, number], [number, number, number, number]]> = [
[[-100, -100, 440, 560], [8, 8, 440, 560]],
[[1200, 700, 440, 560], [1280 - 440 - 8, 800 - 560 - 8, 440, 560]],
[[0, 0, 99999, 99999], [8, 8, 720, 784]],
[[0, 0, 100, 100], [8, 8, MIN_W, MIN_H]],
];
for (const [[x, y, w, h], want] of cases) {
assert.deepEqual(fitRect({ x, y, w, h }, VW, VH), { x: want[0], y: want[1], w: want[2], h: want[3] });
}
// A viewport smaller than the minimum still yields a usable rectangle.
assert.deepEqual(fitRect({ x: 0, y: 0, w: 500, h: 500 }, 300, 200), { x: 8, y: 8, w: 284, h: 184 });
});
test("all eight edges and corners resize the rectangle in their own direction", () => {
const origin = { x: 100, y: 100, w: 400, h: 300 };
const cases: Array<[ResizeDir, number, number, [number, number, number, number]]> = [
["e", 20, 0, [100, 100, 420, 300]],
["e", -20, 0, [100, 100, 380, 300]],
["s", 0, 30, [100, 100, 400, 330]],
["w", 20, 0, [120, 100, 380, 300]],
["w", -20, 0, [80, 100, 420, 300]],
["n", 0, 20, [100, 120, 400, 280]],
["n", 0, -20, [100, 80, 400, 320]],
["se", 10, 10, [100, 100, 410, 310]],
["nw", -10, -10, [90, 90, 410, 310]],
["ne", 10, -10, [100, 90, 410, 310]],
["sw", -10, 10, [90, 100, 410, 310]],
];
for (const [dir, dx, dy, want] of cases) {
assert.deepEqual(
resizeRect(origin, dir, dx, dy, VW, VH),
{ x: want[0], y: want[1], w: want[2], h: want[3] },
dir,
);
}
});
test("resize never collapses the window below the minimum, exceeds the maximum, or pushes it off screen", () => {
const origin = { x: 100, y: 100, w: 400, h: 300 };
assert.deepEqual(resizeRect(origin, "e", -9999, 0, VW, VH), { x: 100, y: 100, w: MIN_W, h: 300 });
assert.deepEqual(resizeRect(origin, "s", 0, -9999, VW, VH), { x: 100, y: 100, w: 400, h: MIN_H });
assert.deepEqual(resizeRect(origin, "w", 9999, 0, VW, VH), { x: 100 + 400 - MIN_W, y: 100, w: MIN_W, h: 300 });
assert.deepEqual(resizeRect(origin, "n", 0, 9999, VW, VH), { x: 100, y: 100 + 300 - MIN_H, w: 400, h: MIN_H });
assert.deepEqual(resizeRect({ x: 0, y: 0, w: 400, h: 300 }, "e", 99999, 0, VW, VH), { x: 0, y: 0, w: MAX_W, h: 300 });
assert.deepEqual(resizeRect({ x: 500, y: 100, w: 400, h: 300 }, "w", -9999, 0, VW, VH), {
x: 500 + 400 - MAX_W,
y: 100,
w: MAX_W,
h: 300,
});
assert.deepEqual(resizeRect({ x: 0, y: 0, w: 400, h: 300 }, "s", 0, 99999, VW, VH), { x: 0, y: 0, w: 400, h: VH - 8 });
});
test("a pointer near a side edge offers that pinned split, otherwise none", () => {
const cases: Array<[number, "left" | "right" | null]> = [
[0, "left"],
[64, "left"],
[65, null],
[640, null],
[1215, null],
[1216, "right"],
[1280, "right"],
];
for (const [x, want] of cases) {
assert.equal(snapEdge(x, VW), want, String(x));
}
});
// Guards the minimised composer floating mid-page under where the window was.
test("the minimised bar docks at the bottom of the screen on the pinned side", () => {
const rect = { x: 300, y: 100, w: 440, h: 560 };
assert.deepEqual(barPlacement(rect, "float", VW), { x: 300, bottom: 8, w: 640 });
assert.deepEqual(barPlacement(rect, "left", VW), { x: 0, bottom: 8, w: 640 });
assert.deepEqual(barPlacement(rect, "right", VW), { x: 1280 - 640, bottom: 8, w: 640 });
// A bar that would run off the right edge slides back on screen.
assert.deepEqual(barPlacement({ ...rect, x: 900 }, "float", VW), { x: 1280 - 640 - 8, bottom: 8, w: 640 });
// A narrow screen caps the bar at its width.
assert.deepEqual(barPlacement({ ...rect, x: 0, w: 320 }, "float", 500), { x: 8, bottom: 8, w: 484 });
});
test("the pinned width is clamped to what the viewport allows", () => {
assert.equal(pinnedWidth(500, VW), 500);
assert.equal(pinnedWidth(9999, VW), 720);
assert.equal(pinnedWidth(100, VW), MIN_W);
assert.equal(pinnedWidth(700, 900), 420); // vw - 480 caps below MAX_W
});
test("keyboard steps and handle metadata follow the contract", () => {
assert.equal(keyStep(false), 10);
assert.equal(keyStep(true), 40);
const dirs = ["n", "s", "e", "w", "ne", "nw", "se", "sw"] as const;
for (const dir of dirs) {
assert.match(handleOrientation(dir), /^(horizontal|vertical)$/);
assert.ok(Number.isFinite(handleValue(dir, { x: 0, y: 0, w: 412.4, h: 287.6 })));
}
assert.equal(handleValue("n", { x: 0, y: 0, w: 100, h: 287.6 }), 288);
assert.equal(handleValue("se", { x: 0, y: 0, w: 412.4, h: 100 }), 412);
assert.equal(handleOrientation("e"), "vertical");
assert.equal(handleOrientation("w"), "vertical");
assert.equal(handleOrientation("s"), "horizontal");
});
test("the on-screen keyboard's cover of the layout viewport is measured, never negative", () => {
const cases: Array<[{ height: number; offsetTop: number } | null, number]> = [
[null, 0],
[{ height: 800, offsetTop: 0 }, 0],
[{ height: 500, offsetTop: 0 }, 300],
[{ height: 500, offsetTop: 20 }, 280],
[{ height: 460.4, offsetTop: 10 }, 330],
[{ height: 900, offsetTop: 0 }, 0],
];
for (const [vv, want] of cases) {
assert.equal(keyboardCover(800, vv), want, JSON.stringify(vv));
}
});
test("the phone bar sits above the keyboard while one is up, else at the safe area", () => {
assert.equal(phoneBarBottom(0), "max(0.5rem, env(safe-area-inset-bottom))");
assert.equal(phoneBarBottom(300), "308px");
assert.equal(phoneBarBottom(1), "9px");
});