|
| 1 | +// Terminal Core tests cover note behavior. |
| 2 | +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; |
| 3 | +import { stripAnsi } from "./ansi.js"; |
| 4 | +import { note, withSuppressedNotes, wrapNoteMessage } from "./note.js"; |
| 5 | + |
| 6 | +function spyStdoutWrite() { |
| 7 | + const chunks: string[] = []; |
| 8 | + const spy = vi.spyOn(process.stdout, "write").mockImplementation((chunk: unknown) => { |
| 9 | + if (typeof chunk === "string") chunks.push(chunk); |
| 10 | + return true; |
| 11 | + }); |
| 12 | + return { spy, chunks }; |
| 13 | +} |
| 14 | + |
| 15 | +function setColumns(n: number) { |
| 16 | + Object.defineProperty(process.stdout, "columns", { |
| 17 | + value: n, |
| 18 | + configurable: true, |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +describe("wrapNoteMessage", () => { |
| 23 | + it("preserves a copy-sensitive path that exceeds the wrap width", () => { |
| 24 | + const longPath = |
| 25 | + "~/.openclaw/agents/main/sessions/9c2acae5-841f-4aea-936b-fdb513b60202.jsonl.lock"; |
| 26 | + const msg = `- Found lock at ${longPath} pid=86519 (alive)`; |
| 27 | + const result = wrapNoteMessage(msg, { columns: 80, maxWidth: 68 }); |
| 28 | + const lines = result.split("\n"); |
| 29 | + const pathLine = lines.find((l) => l.includes(".jsonl.lock")); |
| 30 | + expect(pathLine).toBeDefined(); |
| 31 | + expect(pathLine!).toContain(longPath); |
| 32 | + }); |
| 33 | + |
| 34 | + it("preserves a URL that exceeds the wrap width", () => { |
| 35 | + const url = |
| 36 | + "https://example.com/very/long/path/that/exceeds/normal/line/width/endpoint/v1/users"; |
| 37 | + const msg = `- See ${url} for details`; |
| 38 | + const result = wrapNoteMessage(msg, { columns: 80, maxWidth: 70 }); |
| 39 | + const lines = result.split("\n"); |
| 40 | + const urlLine = lines.find((l) => l.includes("https://")); |
| 41 | + expect(urlLine).toBeDefined(); |
| 42 | + expect(urlLine!).toContain(url); |
| 43 | + }); |
| 44 | + |
| 45 | + it("wraps long non-copy-sensitive words at the wrap width", () => { |
| 46 | + const longWord = "supercalifragilisticexpialidocious"; |
| 47 | + const result = wrapNoteMessage(`- ${longWord}`, { |
| 48 | + columns: 80, |
| 49 | + maxWidth: 20, |
| 50 | + }); |
| 51 | + const lines = result.split("\n"); |
| 52 | + expect(lines.length).toBeGreaterThan(1); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe("note box rendering", () => { |
| 57 | + let writeSpy: ReturnType<typeof vi.spyOn>; |
| 58 | + let chunks: string[]; |
| 59 | + |
| 60 | + beforeEach(() => { |
| 61 | + setColumns(80); |
| 62 | + const spied = spyStdoutWrite(); |
| 63 | + writeSpy = spied.spy; |
| 64 | + chunks = spied.chunks; |
| 65 | + }); |
| 66 | + |
| 67 | + afterEach(() => { |
| 68 | + writeSpy.mockRestore(); |
| 69 | + }); |
| 70 | + |
| 71 | + function getOutput(): string { |
| 72 | + return stripAnsi(chunks.join("")); |
| 73 | + } |
| 74 | + |
| 75 | + it("renders a note box with title and message", () => { |
| 76 | + note("hello world", "Title"); |
| 77 | + const out = getOutput(); |
| 78 | + expect(out).toContain("◇"); |
| 79 | + expect(out).toContain("Title"); |
| 80 | + expect(out).toContain("hello world"); |
| 81 | + expect(out).toContain("╮"); |
| 82 | + expect(out).toContain("╰"); |
| 83 | + expect(out).toContain("╯"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("renders content lines with proper box borders", () => { |
| 87 | + note("- item one\n- item two", "List"); |
| 88 | + const out = getOutput(); |
| 89 | + expect(out).toContain("│"); |
| 90 | + expect(out).toContain("- item one"); |
| 91 | + expect(out).toContain("- item two"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("does not re-wrap a copy-sensitive path inside the box", () => { |
| 95 | + const lockPath = |
| 96 | + "~/.openclaw/agents/main/sessions/9c2acae5-841f-4aea-936b-fdb513b60202.jsonl.lock"; |
| 97 | + const msg = `- Found 1 session lock file.\n- ${lockPath} pid=86519 (alive) age=2m47s stale=no`; |
| 98 | + note(msg, "Session locks"); |
| 99 | + const out = getOutput(); |
| 100 | + // The path must appear intact — not split across lines inside the box. |
| 101 | + expect(out).toContain(lockPath); |
| 102 | + // The extension ".jsonl.lock" must appear as one unbroken unit. |
| 103 | + expect(out).toContain(".jsonl.lock"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("allows a copy-sensitive line to overflow the box right border", () => { |
| 107 | + const longPath = "/" + "a/".repeat(30) + "very-long-filename.jsonl.lock"; |
| 108 | + const msg = `- ${longPath}`; |
| 109 | + note(msg, "Path overflow"); |
| 110 | + const out = getOutput(); |
| 111 | + // The path must appear intact. |
| 112 | + expect(out).toContain(longPath); |
| 113 | + // The bottom border should still be rendered. |
| 114 | + expect(out).toContain("╰"); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe("note suppression", () => { |
| 119 | + let writeSpy: ReturnType<typeof vi.spyOn>; |
| 120 | + |
| 121 | + beforeEach(() => { |
| 122 | + setColumns(80); |
| 123 | + writeSpy = vi.spyOn(process.stdout, "write").mockImplementation(() => true); |
| 124 | + }); |
| 125 | + |
| 126 | + afterEach(() => { |
| 127 | + writeSpy.mockRestore(); |
| 128 | + vi.unstubAllEnvs(); |
| 129 | + }); |
| 130 | + |
| 131 | + it("suppresses output when OPENCLAW_SUPPRESS_NOTES is set", () => { |
| 132 | + vi.stubEnv("OPENCLAW_SUPPRESS_NOTES", "1"); |
| 133 | + note("should not appear"); |
| 134 | + expect(writeSpy).not.toHaveBeenCalled(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("suppresses output inside withSuppressedNotes callback", () => { |
| 138 | + withSuppressedNotes(() => { |
| 139 | + note("should not appear"); |
| 140 | + }); |
| 141 | + expect(writeSpy).not.toHaveBeenCalled(); |
| 142 | + }); |
| 143 | +}); |
0 commit comments