Skip to content
Merged
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
36 changes: 18 additions & 18 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"@intx/types": "workspace:*",
"@intx/workflow-host": "0.3.0",
"@modelcontextprotocol/sdk": "^1.29.0",
"@opentui/core": "0.5.12",
"@opentui/core": "0.6.4",
"arktype": "catalog:",
"isomorphic-git": "catalog:",
"@corbits/code-agent-designer": "workspace:*",
Expand All @@ -125,13 +125,13 @@
"ws": "^8.21.0"
},
"optionalDependencies": {
"@opentui/core-darwin-arm64": "0.5.12",
"@opentui/core-darwin-x64": "0.5.12",
"@opentui/core-linux-arm64": "0.5.12",
"@opentui/core-linux-arm64-musl": "0.5.12",
"@opentui/core-linux-x64": "0.5.12",
"@opentui/core-linux-x64-musl": "0.5.12",
"@opentui/core-win32-arm64": "0.5.12",
"@opentui/core-win32-x64": "0.5.12"
"@opentui/core-darwin-arm64": "0.6.4",
"@opentui/core-darwin-x64": "0.6.4",
"@opentui/core-linux-arm64": "0.6.4",
"@opentui/core-linux-arm64-musl": "0.6.4",
"@opentui/core-linux-x64": "0.6.4",
"@opentui/core-linux-x64-musl": "0.6.4",
"@opentui/core-win32-arm64": "0.6.4",
"@opentui/core-win32-x64": "0.6.4"
}
}
161 changes: 136 additions & 25 deletions src/tui/runner/settings-theme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,43 @@
*/

import { afterEach, describe, expect, test } from "bun:test";
import { RGBA, type CapturedSpan } from "@opentui/core";
import { RGBA, SyntaxStyle, type CapturedSpan } from "@opentui/core";

import { corbitsDark, corbitsLight, setTheme, UI } from "../theme.js";
import { transcriptSyntaxStyle } from "../stream.js";
import { withTestRenderer, type Harness } from "../harness.js";
import { settleSpans } from "../test-helpers.js";
import { appendStreamRow } from "../shell/chrome.js";
import { createAppShell } from "../shell/index.js";
import { setPromptRecognitionSource } from "../shell/internals.js";
import { syncPromptHighlights } from "../shell/prompt.js";
import { applyThemePinLive, repaintShellForTheme } from "./settings.js";

afterEach(() => {
setTheme("corbits-dark");
});

/** Live heading colour in the transcript registry. */
function registryHeadingFg(): RGBA | undefined {
return transcriptSyntaxStyle().getAllStyles().get("markup.heading.1")?.fg;
function registryHeadingFg(h: Harness): RGBA | undefined {
return transcriptSyntaxStyle(h.renderer)
.getAllStyles()
.get("markup.heading.1")?.fg;
}

function painted(spans: CapturedSpan[], text: string, fg: RGBA): boolean {
return spans.some((s) => s.text.includes(text) && s.fg.equals(fg));
/** `destroy()` frees the native style and there is no finalizer; a later
* read throws instead of returning a palette. */
function syntaxStyleAlive(style: SyntaxStyle): boolean {
try {
style.getStyleCount();
return true;
} catch (err) {
if (err instanceof Error && err.message.includes("destroyed")) return false;
throw err;
}
}

async function settleSpans(
h: Harness,
ready: (spans: CapturedSpan[]) => boolean,
): Promise<CapturedSpan[]> {
let spans = h.captureSpans().lines.flatMap((line) => line.spans);
for (let i = 0; i < 400 && !ready(spans); i++) {
await h.renderOnce();
await new Promise((resolve) => setTimeout(resolve, 25));
spans = h.captureSpans().lines.flatMap((line) => line.spans);
}
return spans;
function painted(spans: CapturedSpan[], text: string, fg: RGBA): boolean {
return spans.some((s) => s.text.includes(text) && s.fg.equals(fg));
}

describe("applyThemePinLive", () => {
Expand All @@ -64,7 +68,7 @@ describe("applyThemePinLive", () => {
painted(spans, "pin cycle", darkHeading),
);
expect(painted(before, "pin cycle", darkHeading)).toBe(true);
expect(registryHeadingFg()?.equals(darkHeading)).toBe(true);
expect(registryHeadingFg(h)?.equals(darkHeading)).toBe(true);

// No cost movement anywhere in this cycle: the meter never moves,
// so only an unconditional repaint repaints anything.
Expand All @@ -74,7 +78,7 @@ describe("applyThemePinLive", () => {
expect(applied).toBe("corbits-light");
expect(UI.name).toBe("corbits-light");
expect(UI.ground).not.toBe(darkGround);
expect(registryHeadingFg()?.equals(lightHeading)).toBe(true);
expect(registryHeadingFg(h)?.equals(lightHeading)).toBe(true);

const after = await settleSpans(h, (spans) =>
painted(spans, "pin cycle", lightHeading),
Expand Down Expand Up @@ -106,14 +110,14 @@ describe("applyThemePinLive", () => {
painted(spans, "pin cycle", lightHeading),
);
expect(painted(before, "pin cycle", lightHeading)).toBe(true);
expect(registryHeadingFg()?.equals(lightHeading)).toBe(true);
expect(registryHeadingFg(h)?.equals(lightHeading)).toBe(true);

const applied = applyThemePinLive("dark", () =>
repaintShellForTheme(shell),
);
expect(applied).toBe("corbits-dark");
expect(UI.name).toBe("corbits-dark");
expect(registryHeadingFg()?.equals(darkHeading)).toBe(true);
expect(registryHeadingFg(h)?.equals(darkHeading)).toBe(true);

const after = await settleSpans(h, (spans) =>
painted(spans, "pin cycle", darkHeading),
Expand All @@ -129,11 +133,118 @@ describe("applyThemePinLive", () => {
);
}, 30000);

test("auto re-resolves through the startup path and rebuilds the registry", () => {
test("auto re-resolves through the startup path and rebuilds the registry", async () => {
setTheme("corbits-dark");
const applied = applyThemePinLive("auto", () => undefined);
expect(["corbits-dark", "corbits-light"]).toContain(applied);
expect(UI.name).toBe(applied);
expect(registryHeadingFg()?.equals(RGBA.fromHex(UI.heading))).toBe(true);
await withTestRenderer(async (h) => {
const applied = applyThemePinLive("auto", () => undefined);
expect(["corbits-dark", "corbits-light"]).toContain(applied);
expect(UI.name).toBe(applied);
expect(registryHeadingFg(h)?.equals(RGBA.fromHex(UI.heading))).toBe(true);
});
});

test("a second pin destroys the transcript style the rows dropped", async () => {
await withTestRenderer(
async (h) => {
setTheme("corbits-dark");
const shell = createAppShell(h.renderer, {
terminal: { columns: 80, rows: 40 },
wireKeys: false,
});
try {
appendStreamRow(shell, { role: "assistant", text: "# pin cycle" });
const first = transcriptSyntaxStyle(h.renderer);
applyThemePinLive("light", () => repaintShellForTheme(shell));
const second = transcriptSyntaxStyle(h.renderer);
expect(second).not.toBe(first);
expect(syntaxStyleAlive(first)).toBe(false);
expect(syntaxStyleAlive(second)).toBe(true);

applyThemePinLive("dark", () => repaintShellForTheme(shell));
const third = transcriptSyntaxStyle(h.renderer);
expect(third).not.toBe(second);
expect(syntaxStyleAlive(second)).toBe(false);
expect(syntaxStyleAlive(third)).toBe(true);
} finally {
shell.dispose();
setTheme("corbits-dark");
}
},
{ width: 80, height: 40 },
);
});

test("refreshing the transcript cache leaves a style rows still paint", async () => {
await withTestRenderer(
async (h) => {
setTheme("corbits-dark");
const shell = createAppShell(h.renderer, {
terminal: { columns: 80, rows: 40 },
wireKeys: false,
});
try {
appendStreamRow(shell, { role: "assistant", text: "# pin cycle" });
const painted = transcriptSyntaxStyle(h.renderer);
setTheme("corbits-light");
const rebuilt = transcriptSyntaxStyle(h.renderer);
expect(rebuilt).not.toBe(painted);
expect(syntaxStyleAlive(painted)).toBe(true);
repaintShellForTheme(shell);
expect(syntaxStyleAlive(painted)).toBe(false);
expect(syntaxStyleAlive(transcriptSyntaxStyle(h.renderer))).toBe(
true,
);
} finally {
shell.dispose();
setTheme("corbits-dark");
}
},
{ width: 80, height: 40 },
);
});

test("a pin destroys the prompt style only after the prompt drops it", async () => {
await withTestRenderer(
async (h) => {
setTheme("corbits-dark");
const shell = createAppShell(h.renderer, {
terminal: { columns: 80, rows: 40 },
wireKeys: false,
});
setPromptRecognitionSource(shell, () => ({
commandNames: ["implement"],
}));
const originalDestroy = SyntaxStyle.prototype.destroy;
let destroyedWhileHeld = false;
try {
shell.prompt.value = "/implement";
syncPromptHighlights(shell);
const painted = shell.prompt.syntaxStyle;
expect(painted).not.toBeNull();
if (painted === null) return;
SyntaxStyle.prototype.destroy = function destroy(this: SyntaxStyle) {
if (this === painted && shell.prompt.syntaxStyle === painted) {
destroyedWhileHeld = true;
}
return originalDestroy.call(this);
};
applyThemePinLive("light", () => repaintShellForTheme(shell));
expect(shell.prompt.syntaxStyle).toBe(painted);
expect(syntaxStyleAlive(painted)).toBe(true);
syncPromptHighlights(shell);
expect(destroyedWhileHeld).toBe(false);
expect(shell.prompt.syntaxStyle).not.toBe(painted);
expect(syntaxStyleAlive(painted)).toBe(false);
const current = shell.prompt.syntaxStyle;
expect(current).not.toBeNull();
if (current !== null) expect(syntaxStyleAlive(current)).toBe(true);
} finally {
SyntaxStyle.prototype.destroy = originalDestroy;
shell.dispose();
setTheme("corbits-dark");
}
},
{ width: 80, height: 40 },
);
});
});
2 changes: 2 additions & 0 deletions src/tui/shell/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ import {
transcriptRowChildren,
transcriptRowLayout,
transcriptRowOffset,
releaseRetiredTranscriptSyntaxStyles,
} from "./transcript.js";

function syncPending(shell: AppShell): void {
Expand Down Expand Up @@ -1411,6 +1412,7 @@ export function repaintTranscriptWindow(shell: AppShell): void {
),
);
});
releaseRetiredTranscriptSyntaxStyles(shell);
}

/** Tear the landing down on the first transcript row. The prompt box jumps
Expand Down
3 changes: 3 additions & 0 deletions src/tui/shell/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ export type ShellRenderer = Pick<
| "off"
| "isDestroyed"
| "clearSelection"
// 0.6.x native style construction needs the owning Context: the renderer's
// native scene.
| "nativeScene"
>;

export interface AppShellOptions {
Expand Down
Loading
Loading