File size: 9,134 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | import { afterEach, describe, expect, it, vi } from "vitest";
import { visibleWidth } from "./ansi.js";
import { wrapNoteMessage } from "./note.js";
import { renderTable } from "./table.js";
describe("renderTable", () => {
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
afterEach(() => {
vi.unstubAllEnvs();
if (originalPlatformDescriptor) {
Object.defineProperty(process, "platform", originalPlatformDescriptor);
}
});
it("prefers shrinking flex columns to avoid wrapping non-flex labels", () => {
const out = renderTable({
width: 40,
columns: [
{ key: "Item", header: "Item", minWidth: 10 },
{ key: "Value", header: "Value", flex: true, minWidth: 24 },
],
rows: [{ Item: "Dashboard", Value: "http://127.0.0.1:18789/" }],
});
expect(out).toContain("Dashboard");
expect(out).toMatch(/β Dashboard\s+β/);
});
it("expands flex columns to fill available width", () => {
const width = 60;
const out = renderTable({
width,
columns: [
{ key: "Item", header: "Item", minWidth: 10 },
{ key: "Value", header: "Value", flex: true, minWidth: 24 },
],
rows: [{ Item: "OS", Value: "macos 26.2 (arm64)" }],
});
const firstLine = out.trimEnd().split("\n")[0] ?? "";
expect(visibleWidth(firstLine)).toBe(width);
});
it("wraps ANSI-colored cells without corrupting escape sequences", () => {
const out = renderTable({
width: 36,
columns: [
{ key: "K", header: "K", minWidth: 3 },
{ key: "V", header: "V", flex: true, minWidth: 10 },
],
rows: [
{
K: "X",
V: `\x1b[33m${"a".repeat(120)}\x1b[0m`,
},
],
});
const ansiToken = new RegExp(String.raw`\u001b\[[0-9;]*m|\u001b\]8;;.*?\u001b\\`, "gs");
let escapeIndex = out.indexOf("\u001b");
while (escapeIndex >= 0) {
ansiToken.lastIndex = escapeIndex;
const match = ansiToken.exec(out);
expect(match?.index).toBe(escapeIndex);
escapeIndex = out.indexOf("\u001b", escapeIndex + 1);
}
});
it("resets ANSI styling on wrapped lines", () => {
const reset = "\x1b[0m";
const out = renderTable({
width: 24,
columns: [
{ key: "K", header: "K", minWidth: 3 },
{ key: "V", header: "V", flex: true, minWidth: 10 },
],
rows: [
{
K: "X",
V: `\x1b[31m${"a".repeat(80)}${reset}`,
},
],
});
const lines = out.split("\n").filter((line) => line.includes("a"));
for (const line of lines) {
const resetIndex = line.lastIndexOf(reset);
const lastSep = line.lastIndexOf("β");
expect(resetIndex).toBeGreaterThan(-1);
expect(lastSep).toBeGreaterThan(resetIndex);
}
});
it("trims leading spaces on wrapped ANSI-colored continuation lines", () => {
const out = renderTable({
width: 113,
columns: [
{ key: "Status", header: "Status", minWidth: 10 },
{ key: "Skill", header: "Skill", minWidth: 18, flex: true },
{ key: "Description", header: "Description", minWidth: 24, flex: true },
{ key: "Source", header: "Source", minWidth: 10 },
],
rows: [
{
Status: "β ready",
Skill: "π€οΈ weather",
Description:
`\x1b[2mGet current weather and forecasts via wttr.in or Open-Meteo. ` +
`Use when: user asks about weather, temperature, or forecasts for any location.` +
`\x1b[0m`,
Source: "openclaw-bundled",
},
],
});
const lines = out
.trimEnd()
.split("\n")
.filter((line) => line.includes("Use when"));
expect(lines).toHaveLength(1);
expect(lines[0]).toContain("\u001b[2mUse when");
expect(lines[0]).not.toContain("β Use when");
expect(lines[0]).not.toContain("β \x1b[2m Use when");
});
it("respects explicit newlines in cell values", () => {
const out = renderTable({
width: 48,
columns: [
{ key: "A", header: "A", minWidth: 6 },
{ key: "B", header: "B", minWidth: 10, flex: true },
],
rows: [{ A: "row", B: "line1\nline2" }],
});
const lines = out.trimEnd().split("\n");
const line1Index = lines.findIndex((line) => line.includes("line1"));
const line2Index = lines.findIndex((line) => line.includes("line2"));
expect(line1Index).toBeGreaterThan(-1);
expect(line2Index).toBe(line1Index + 1);
});
it("keeps table borders aligned when cells contain wide emoji graphemes", () => {
const width = 72;
const out = renderTable({
width,
columns: [
{ key: "Status", header: "Status", minWidth: 10 },
{ key: "Skill", header: "Skill", minWidth: 18 },
{ key: "Description", header: "Description", minWidth: 18, flex: true },
{ key: "Source", header: "Source", minWidth: 10 },
],
rows: [
{
Status: "β missing",
Skill: "πΈ peekaboo",
Description: "Capture screenshots from macOS windows and keep table wrapping stable.",
Source: "openclaw-bundled",
},
],
});
for (const line of out.trimEnd().split("\n")) {
expect(visibleWidth(line)).toBe(width);
}
});
it("consumes unsupported escape sequences without hanging", () => {
const out = renderTable({
width: 48,
columns: [
{ key: "K", header: "K", minWidth: 6 },
{ key: "V", header: "V", minWidth: 12, flex: true },
],
rows: [{ K: "row", V: "before \x1b[2J after" }],
});
expect(out).toContain("before");
expect(out).toContain("after");
});
it("falls back to ASCII borders on legacy Windows consoles", () => {
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
vi.stubEnv("WT_SESSION", "");
vi.stubEnv("TERM_PROGRAM", "");
vi.stubEnv("TERM", "vt100");
const out = renderTable({
columns: [
{ key: "A", header: "A", minWidth: 6 },
{ key: "B", header: "B", minWidth: 10, flex: true },
],
rows: [{ A: "row", B: "value" }],
});
expect(out).toContain("+");
expect(out).not.toContain("β");
});
it("keeps unicode borders on modern Windows terminals", () => {
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
vi.stubEnv("WT_SESSION", "1");
vi.stubEnv("TERM", "");
vi.stubEnv("TERM_PROGRAM", "");
const out = renderTable({
columns: [
{ key: "A", header: "A", minWidth: 6 },
{ key: "B", header: "B", minWidth: 10, flex: true },
],
rows: [{ A: "row", B: "value" }],
});
expect(out).toContain("β");
expect(out).not.toContain("+");
});
});
describe("wrapNoteMessage", () => {
it("preserves long filesystem paths without inserting spaces/newlines", () => {
const input =
"/Users/user/Documents/Github/impact-signals-pipeline/with/really/long/segments/file.txt";
const wrapped = wrapNoteMessage(input, { maxWidth: 22, columns: 80 });
expect(wrapped).toBe(input);
});
it("preserves long urls without inserting spaces/newlines", () => {
const input =
"https://example.com/this/is/a/very/long/url/segment/that/should/not/be/split/for-copy";
const wrapped = wrapNoteMessage(input, { maxWidth: 24, columns: 80 });
expect(wrapped).toBe(input);
});
it("preserves long file-like underscore tokens for copy safety", () => {
const input = "administrators_authorized_keys_with_extra_suffix";
const wrapped = wrapNoteMessage(input, { maxWidth: 14, columns: 80 });
expect(wrapped).toBe(input);
});
it("still chunks generic long opaque tokens to avoid pathological line width", () => {
const input = "x".repeat(70);
const wrapped = wrapNoteMessage(input, { maxWidth: 20, columns: 80 });
expect(wrapped).toContain("\n");
expect(wrapped.replace(/\n/g, "")).toBe(input);
});
it("wraps bullet lines while preserving bullet indentation", () => {
const input = "- one two three four five six seven eight nine ten";
const wrapped = wrapNoteMessage(input, { maxWidth: 18, columns: 80 });
const lines = wrapped.split("\n");
expect(lines.length).toBeGreaterThan(1);
expect(lines[0]?.startsWith("- ")).toBe(true);
expect(lines.slice(1).every((line) => line.startsWith(" "))).toBe(true);
});
it("preserves long Windows paths without inserting spaces/newlines", () => {
// No spaces: wrapNoteMessage splits on whitespace, so a "Program Files" style path would wrap.
const input = "C:\\\\State\\\\OpenClaw\\\\bin\\\\openclaw.exe";
const wrapped = wrapNoteMessage(input, { maxWidth: 10, columns: 80 });
expect(wrapped).toBe(input);
});
it("preserves UNC paths without inserting spaces/newlines", () => {
const input = "\\\\\\\\server\\\\share\\\\some\\\\really\\\\long\\\\path\\\\file.txt";
const wrapped = wrapNoteMessage(input, { maxWidth: 12, columns: 80 });
expect(wrapped).toBe(input);
});
});
|