|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import type { Span } from "./model.js"; |
| 4 | +import { stepTitle } from "./wording.js"; |
| 5 | + |
| 6 | +function toolSpan(input: string, label?: string): Span { |
| 7 | + const span: Span = { |
| 8 | + id: "span-1", |
| 9 | + parentId: null, |
| 10 | + kind: "tool", |
| 11 | + name: "Bash", |
| 12 | + status: "ok", |
| 13 | + startMs: 0, |
| 14 | + durationMs: 1, |
| 15 | + input, |
| 16 | + }; |
| 17 | + if (label === undefined) return span; |
| 18 | + return { ...span, label }; |
| 19 | +} |
| 20 | + |
| 21 | +describe("stepTitle", () => { |
| 22 | + test("a label wins over the command", () => { |
| 23 | + const input = JSON.stringify({ command: "ls -la" }); |
| 24 | + expect(stepTitle(toolSpan(input, "List files"))).toBe("List files"); |
| 25 | + }); |
| 26 | + |
| 27 | + test("a multiline command keeps the first line", () => { |
| 28 | + const command = "git status\ngit diff"; |
| 29 | + const input = JSON.stringify({ command }); |
| 30 | + expect(stepTitle(toolSpan(input))).toBe("git status"); |
| 31 | + expect(JSON.parse(input).command).toBe(command); |
| 32 | + }); |
| 33 | + |
| 34 | + test("a command of 80 characters stays whole", () => { |
| 35 | + const command = "b".repeat(80); |
| 36 | + const input = JSON.stringify({ cmd: command }); |
| 37 | + expect(stepTitle(toolSpan(input))).toBe(command); |
| 38 | + expect(JSON.parse(input).cmd).toBe(command); |
| 39 | + }); |
| 40 | + |
| 41 | + test("a blank command falls through to the step name", () => { |
| 42 | + expect(stepTitle(toolSpan(JSON.stringify({ command: " \nls" })))).toBe( |
| 43 | + "Bash", |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + test("a longer command is clipped", () => { |
| 48 | + const command = ` ${"a".repeat(90)} \nrest`; |
| 49 | + const input = JSON.stringify({ command }); |
| 50 | + expect(stepTitle(toolSpan(input))).toBe(`${"a".repeat(77)}…`); |
| 51 | + expect(JSON.parse(input).command).toBe(command); |
| 52 | + }); |
| 53 | + |
| 54 | + test("a command cut inside an emoji keeps the code point", () => { |
| 55 | + const command = `${"a".repeat(76)}\u{1F600}${"b".repeat(20)}`; |
| 56 | + const input = JSON.stringify({ command }); |
| 57 | + const span = toolSpan(input); |
| 58 | + const title = stepTitle(span); |
| 59 | + expect(title).toBe(`${"a".repeat(76)}\u{1F600}…`); |
| 60 | + expect(title.endsWith("…")).toBe(true); |
| 61 | + expect([...title].length).toBeLessThanOrEqual(78); |
| 62 | + expect(loneSurrogate(title)).toBe(false); |
| 63 | + expect(span.input).toBe(input); |
| 64 | + }); |
| 65 | + |
| 66 | + test("a reasoning cut inside an emoji keeps the code point", () => { |
| 67 | + const thinking = `${"c".repeat(84)}\u{1F600}${"d".repeat(20)}\nnext`; |
| 68 | + const span: Span = { |
| 69 | + id: "span-r", |
| 70 | + parentId: null, |
| 71 | + kind: "reasoning", |
| 72 | + name: "Think", |
| 73 | + status: "ok", |
| 74 | + startMs: 0, |
| 75 | + durationMs: 1, |
| 76 | + thinking, |
| 77 | + }; |
| 78 | + const title = stepTitle(span); |
| 79 | + expect(title).toBe(`${"c".repeat(84)}\u{1F600}…`); |
| 80 | + expect(title.endsWith("…")).toBe(true); |
| 81 | + expect([...title].length).toBeLessThanOrEqual(86); |
| 82 | + expect(loneSurrogate(title)).toBe(false); |
| 83 | + expect(span.thinking).toBe(thinking); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +function loneSurrogate(value: string): boolean { |
| 88 | + for (let index = 0; index < value.length; index += 1) { |
| 89 | + const code = value.charCodeAt(index); |
| 90 | + const high = code >= 0xd800 && code <= 0xdbff; |
| 91 | + const low = code >= 0xdc00 && code <= 0xdfff; |
| 92 | + if (high) { |
| 93 | + const next = value.charCodeAt(index + 1); |
| 94 | + if (!(next >= 0xdc00 && next <= 0xdfff)) return true; |
| 95 | + index += 1; |
| 96 | + continue; |
| 97 | + } |
| 98 | + if (low) return true; |
| 99 | + } |
| 100 | + return false; |
| 101 | +} |
0 commit comments