import { beforeAll, describe, expect, it } from "bun:test"; import type { AssistantMessage } from "@sayknow-cli/ai"; import { resetSettingsForTest, Settings } from "@sayknow-cli/coding-agent/config/settings"; import { setLanguage } from "@sayknow-cli/coding-agent/i18n/index"; import { AssistantMessageComponent } from "@sayknow-cli/coding-agent/modes/components/assistant-message"; import { BashExecutionComponent } from "@sayknow-cli/coding-agent/modes/components/bash-execution"; import { CustomEditor } from "@sayknow-cli/coding-agent/modes/components/custom-editor"; import { EvalExecutionComponent } from "@sayknow-cli/coding-agent/modes/components/eval-execution"; import { FooterComponent } from "@sayknow-cli/coding-agent/modes/components/footer"; import { STATUS_LINE_PRESETS } from "@sayknow-cli/coding-agent/modes/components/status-line/presets"; import { UserMessageComponent } from "@sayknow-cli/coding-agent/modes/components/user-message"; import { WelcomeComponent } from "@sayknow-cli/coding-agent/modes/components/welcome"; import { resolveWelcomeLogoMode } from "@sayknow-cli/coding-agent/modes/interactive-mode"; import { getEditorTheme, initTheme, theme } from "@sayknow-cli/coding-agent/modes/theme/theme"; import type { AgentSession } from "@sayknow-cli/coding-agent/session/agent-session"; import { type TUI, visibleWidth } from "@sayknow-cli/tui"; import { ToolExecutionComponent } from "../../../src/modes/components/tool-execution"; import { StatusLineComponent } from "../../../src/modes/components/tool-status-header"; function createFooterSession(): AgentSession { return { state: { messages: [], model: { id: "very-long-model-name-for-footer-budget", contextWindow: 200_000 }, }, sessionManager: { getSessionName: () => "forge-session", getSessionId: () => "session-123456", getUsageStatistics: () => ({ input: 1234, output: 567, cacheRead: 89, cacheWrite: 12, premiumRequests: 0, cost: 0.123, }), getEntries: () => [ { type: "message", message: { role: "assistant", usage: { input: 1234, output: 567, cacheRead: 89, cacheWrite: 12, cost: { total: 0.123 }, premiumRequests: 0, }, }, }, ], }, getContextUsage: () => ({ tokens: 85_000, contextWindow: 200_000, percent: 42.5, source: "provider_anchor" as const, }), getGoalModeState: () => undefined, getAsyncJobSnapshot: () => ({ running: [] }), isFastModeActive: () => false, modelRegistry: { isUsingOAuth: () => false }, } as unknown as AgentSession; } function createAssistantMessage(text: string): AssistantMessage { return { role: "assistant", content: [{ type: "text", text }], api: "anthropic-messages", provider: "anthropic", model: "claude-sonnet-4-5", usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, }, stopReason: "stop", timestamp: Date.now(), }; } beforeAll(async () => { resetSettingsForTest(); await Settings.init({ inMemory: true }); // Settings.init auto-detects the system language; pin English so these // English UI assertions are deterministic regardless of the test machine. setLanguage("en"); await initTheme(false); }); describe("redesigned interactive shell chrome", () => { it("renders the user prompt behind a rail instead of a labelled bubble", () => { const lines = new UserMessageComponent("hello\n\nsecond paragraph").render(80); const plain = lines.map(line => Bun.stripANSI(line).replace(/\x1b\]133;[ABC]\x07/g, "")); const body = plain.slice(1); expect(plain[0]!.trim()).toBe(""); for (const line of body) expect(line.startsWith("▌ ")).toBe(true); expect(body.join("\n")).toContain("▌ hello"); expect(body.join("\n")).toContain("▌ second paragraph"); expect(plain.join("\n")).not.toContain("user"); // No filled bubble: the prompt sits on the transcript background. expect(lines.join("")).not.toMatch(/\x1b\[48;/); }); it("marks replayed prompts as replay on a dim rail", () => { const plain = Bun.stripANSI(new UserMessageComponent("hello", true).render(80).join("\n")); expect(plain).toContain("▌ replay"); expect(plain).toContain("▌ hello"); }); it("renders sayknow turns without the generic assistant label", () => { const assistant = Bun.stripANSI( new AssistantMessageComponent(createAssistantMessage("hi")).render(80).join("\n"), ); expect(assistant).toContain("sayknow"); expect(assistant).not.toContain("assistant"); expect(assistant).not.toContain("sayknow reply"); expect(assistant).not.toContain("▌"); }); it("sizes submitted Korean user prompts to the current viewport width", () => { const prompt = "안녕하세요 ".repeat(30); const narrowLines = new UserMessageComponent(prompt).render(80); const wideLines = new UserMessageComponent(prompt).render(160); const contentWidths = (lines: string[]) => lines .map(line => Bun.stripANSI(line) .replace(/\x1b\]133;[ABC]\x07/g, "") .trimEnd(), ) .filter(line => line.includes("안녕하세요")) .map(line => visibleWidth(line)); const narrowContentWidths = contentWidths(narrowLines); const wideContentWidths = contentWidths(wideLines); expect(wideContentWidths.length).toBeLessThan(narrowContentWidths.length); expect(Math.max(...wideContentWidths)).toBeGreaterThan(120); for (const line of narrowLines) { expect(visibleWidth(line)).toBeLessThanOrEqual(80); } for (const line of wideLines) { expect(visibleWidth(line)).toBeLessThanOrEqual(160); } }); it("keeps the Sayknow launch surface responsive", () => { const component = new WelcomeComponent("1.2.3", "gpt-5.5", "openai"); const lines = component.render(54); const rendered = Bun.stripANSI(lines.join("\n")); expect(rendered).toContain("╔═╗╔═╗"); expect(rendered).toContain("gpt-5.5"); // No enclosing box: the wordmark and sections stand on the background. expect(rendered).not.toContain("╭"); expect(rendered).not.toContain("│"); for (const line of lines) { expect(visibleWidth(line)).toBeLessThanOrEqual(54); } }); it("pads the launch card to the full terminal width", () => { const component = new WelcomeComponent("1.2.3", "gpt-5.5", "openai"); const narrowLines = component.render(100); const wideLines = component.render(160); const titleRow = (lines: string[]) => Bun.stripANSI(lines.find(line => line.includes("╔═╗╔═╗")) ?? ""); const narrowTop = titleRow(narrowLines); const wideTop = titleRow(wideLines); expect(visibleWidth(narrowTop)).toBe(100); expect(visibleWidth(wideTop)).toBe(160); expect(visibleWidth(wideTop)).toBeGreaterThan(visibleWidth(narrowTop)); expect(wideTop).toContain("╔═╗╔═╗"); for (const line of wideLines) { expect(visibleWidth(line)).toBeLessThanOrEqual(160); } }); it("keeps the launch card mascot-free in ASCII and Unicode modes", () => { const ascii = Bun.stripANSI( new WelcomeComponent("1.2.3", "gpt-5.5", "openai", [], "ascii").render(80).join("\n"), ); const unicode = Bun.stripANSI( new WelcomeComponent("1.2.3", "gpt-5.5", "openai", [], "unicode").render(80).join("\n"), ); expect(ascii).toContain("Sayknow-CLI v1.2.3"); expect(ascii).not.toMatch(/[█▀▄╔\u{10eeee}]/u); expect(unicode).toContain("╔═╗╔═╗"); expect(unicode).not.toMatch(/[▀▄\u{10eeee}]/u); }); it("resolves welcome banner auto and manual override modes", () => { expect(resolveWelcomeLogoMode("auto", { WT_SESSION: "session-id" }, "win32")).toBe("unicode"); expect(resolveWelcomeLogoMode("auto", { WT_SESSION: "session-id" }, "linux")).toBe("unicode"); expect(resolveWelcomeLogoMode("auto", {}, "win32")).toBe("unicode"); expect(resolveWelcomeLogoMode("unicode", { WT_SESSION: "session-id" }, "win32")).toBe("unicode"); expect(resolveWelcomeLogoMode("square", { WT_SESSION: "session-id" }, "win32")).toBe("square"); expect(resolveWelcomeLogoMode("ascii", {}, "linux")).toBe("ascii"); }); it("renders the live composer as a borderless opencode-style prompt", () => { const editor = new CustomEditor(getEditorTheme()); editor.setBorderVisible(false); editor.setPromptGutter("› "); editor.setPaddingX(1); editor.setText("draft"); const rendered = Bun.stripANSI(editor.render(40).join("\n")); expect(rendered).toContain("› draft"); expect(rendered).not.toContain("╭"); expect(rendered).not.toContain("╰"); }); it("renders the main status rail outside the borderless composer", () => { const statusLine = new StatusLineComponent(createFooterSession()); const editor = new CustomEditor(getEditorTheme()); editor.setBorderVisible(false); editor.setPromptGutter("› "); editor.setPaddingX(1); editor.setText("draft"); const statusRendered = Bun.stripANSI(statusLine.render(140).join("\n")); const editorRendered = Bun.stripANSI(editor.render(140).join("\n")); expect(statusRendered).toContain("very-long-model-name-for-footer-budget"); expect(statusRendered).toContain("forge-session"); expect(statusRendered).toMatch(/very-long-model-name-for-footer-budget[^\n]*\d+(\.\d+)?%/); expect(editorRendered).toContain("› draft"); expect(editorRendered).not.toContain("very-long-model-name-for-footer-budget"); expect(editorRendered).not.toContain("╭"); }); it("renders execution rails without breaking output caps", () => { const ui = { requestRender: () => {} } as unknown as TUI; const bash = new BashExecutionComponent("printf ready", ui, false); bash.setComplete(0, false, { output: Array.from({ length: 160 }, (_, i) => `line-${i}`).join("\n") }); const bashRendered = Bun.stripANSI(bash.render(80).join("\n")); expect(bashRendered).toContain("shell · $ printf"); expect(bashRendered).toContain("ctrl+o to expand"); expect(bashRendered).not.toContain("line-0\n"); for (const line of bash.render(80)) { expect(visibleWidth(line)).toBeLessThanOrEqual(80); } }); it("draws tool results as an unfilled rail instead of a boxed card", () => { const ui = { requestRender: () => {} } as unknown as TUI; const render = (isError: boolean) => { const tool = new ToolExecutionComponent("bash", { command: "git push" }, {}, undefined, ui); tool.updateResult({ content: [{ type: "text", text: "fatal: no remote" }], isError }, false); return tool.render(60); }; for (const isError of [false, true]) { const lines = render(isError); const plain = lines.map(line => Bun.stripANSI(line)); const body = plain.filter(line => line.trim() !== ""); expect(lines.join("")).not.toMatch(/\x1b\[48;/); for (const boxGlyph of ["┌", "┐", "└", "┘", "╭", "╯"]) expect(plain.join("\n")).not.toContain(boxGlyph); expect(body[0]).toContain("Bash"); expect(body.slice(1).every(line => /^\s[│├]/.test(line))).toBe(true); expect(plain.join("\n")).toContain("fatal: no remote"); for (const line of lines) expect(visibleWidth(line)).toBeLessThanOrEqual(60); } // The rail, not a fill, carries the error state. expect(render(true).join("")).toContain(theme.fg("error", `${theme.boxSharp.vertical} `)); }); it("keeps eval execution headers compact and mode-labeled", () => { const ui = { requestRender: () => {} } as unknown as TUI; const py = new EvalExecutionComponent("print('ready')", ui, false, "python"); const js = new EvalExecutionComponent("1 + 1", ui, false, "js"); expect(Bun.stripANSI(py.render(80).join("\n"))).toContain("python · >>>"); expect(Bun.stripANSI(js.render(80).join("\n"))).toContain("node · >>>"); }); it("keeps eval continuation aligned for multiline code", () => { const ui = { requestRender: () => {} } as unknown as TUI; const py = new EvalExecutionComponent("print('a')\nprint('b')", ui, false, "python"); const stripped = Bun.stripANSI(py.render(80).join("\n")); expect(stripped).toContain("python · >>> print('a')"); expect(stripped).toContain(" print('b')"); }); it("keeps OSC 133 prompt markers scoped to the message body", () => { const rendered = new UserMessageComponent("hello").render(80); expect(rendered[0]).not.toContain("\x1b]133;A\x07"); expect(rendered[1]).toContain("\x1b]133;A\x07"); expect(rendered[rendered.length - 1]).toContain("\x1b]133;B\x07\x1b]133;C\x07"); }); it("budgets footer prefixes before truncating pulse", () => { const footer = new FooterComponent(createFooterSession()); const lines = footer.render(72); const rendered = Bun.stripANSI(lines.join("\n")); expect(rendered).toContain("cwd"); expect(rendered).toContain("pulse"); expect(rendered).toContain("very-long-mod"); for (const line of lines) { expect(visibleWidth(line)).toBeLessThanOrEqual(72); } }); it("keeps public status presets on the SKC identity", () => { for (const [name, preset] of Object.entries(STATUS_LINE_PRESETS)) { expect(preset.leftSegments, name).not.toContain("pi"); } expect(STATUS_LINE_PRESETS.full.leftSegments).toContain("sayknow"); expect(STATUS_LINE_PRESETS.nerd.leftSegments).toContain("sayknow"); }); it("keeps the default status preset dense and pulse-forward", () => { expect(STATUS_LINE_PRESETS.default.leftSegments).toEqual(["model", "mode", "git", "pr", "path"]); expect(STATUS_LINE_PRESETS.default.rightSegments).toEqual(["session_name", "jobs", "token_rate", "cost"]); expect(STATUS_LINE_PRESETS.default.segmentOptions?.path?.maxLength).toBe(32); }); it("adds a default plus usage status preset without changing default", () => { expect(STATUS_LINE_PRESETS["default-usage"].leftSegments).toEqual(STATUS_LINE_PRESETS.default.leftSegments); expect(STATUS_LINE_PRESETS["default-usage"].rightSegments).toEqual([ "session_name", "jobs", "token_rate", "usage", "cost", ]); expect(STATUS_LINE_PRESETS["default-usage"].segmentOptions).toEqual(STATUS_LINE_PRESETS.default.segmentOptions); }); it("keeps forge launch rendering on the bounded-work path", () => { const component = new WelcomeComponent("1.2.3", "gpt-5.5", "openai", [ { name: "very-long-session-name".repeat(10), timeAgo: "2h" }, ]); const first = component.render(96); const second = component.render(96); expect(first).toHaveLength(second.length); expect(first.join("\n")).toBe(second.join("\n")); for (const line of first) { expect(visibleWidth(line)).toBeLessThanOrEqual(96); } }); });