|
| 1 | +// altimate_change - new file |
| 2 | +// Unit coverage for the pure-logic helpers in |
| 3 | +// packages/opencode/src/cli/cmd/link.ts that back the `altimate-code link` |
| 4 | +// picker's clickable-workspace-name affordance: control-char sanitization, |
| 5 | +// terminal capability detection, URL joining, and the OSC 8 wrapper itself. |
| 6 | +// The interactive `@clack/prompts` flow (LinkCommand.handler) needs a TTY |
| 7 | +// and is covered by manual verification (PR #1274), not here. |
| 8 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 9 | +import { buildManageUrl, hyperlink, stripControlChars, terminalSupportsHyperlinks } from "../../../src/cli/cmd/link" |
| 10 | + |
| 11 | +describe("stripControlChars", () => { |
| 12 | + test("removes C0 control bytes including ESC", () => { |
| 13 | + expect(stripControlChars("a\x1bb\x00c")).toBe("abc") |
| 14 | + }) |
| 15 | + |
| 16 | + test("removes DEL and C1 control bytes", () => { |
| 17 | + expect(stripControlChars("a\x7fb\x9fc\x80d")).toBe("abcd") |
| 18 | + }) |
| 19 | + |
| 20 | + test("neutralizes an embedded OSC 8 sequence into inert text", () => { |
| 21 | + const malicious = "name\x1b]8;;http://evil.example\x1b\\CLICK ME\x1b]8;;\x1b\\" |
| 22 | + const sanitized = stripControlChars(malicious) |
| 23 | + expect(sanitized).not.toContain("\x1b") |
| 24 | + // The literal (non-ESC) bytes survive as inert text — only the escape |
| 25 | + // bytes that would make it a live control sequence are stripped. |
| 26 | + expect(sanitized).toBe("name]8;;http://evil.example\\CLICK ME]8;;\\") |
| 27 | + }) |
| 28 | + |
| 29 | + test("leaves ordinary printable text untouched", () => { |
| 30 | + expect(stripControlChars("Rakuten Analytics Pipeline")).toBe("Rakuten Analytics Pipeline") |
| 31 | + }) |
| 32 | +}) |
| 33 | + |
| 34 | +describe("terminalSupportsHyperlinks", () => { |
| 35 | + const ORIGINAL_ENV = { ...process.env } |
| 36 | + const ORIGINAL_TTY = process.stdout.isTTY |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + for (const key of ["TERM", "TERM_PROGRAM", "WT_SESSION", "KONSOLE_VERSION", "VTE_VERSION"]) { |
| 40 | + delete process.env[key] |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + process.env = { ...ORIGINAL_ENV } |
| 46 | + Object.defineProperty(process.stdout, "isTTY", { value: ORIGINAL_TTY, configurable: true }) |
| 47 | + }) |
| 48 | + |
| 49 | + function setTTY(value: boolean) { |
| 50 | + Object.defineProperty(process.stdout, "isTTY", { value, configurable: true }) |
| 51 | + } |
| 52 | + |
| 53 | + test("false when stdout is not a TTY, regardless of TERM_PROGRAM", () => { |
| 54 | + setTTY(false) |
| 55 | + process.env.TERM_PROGRAM = "iTerm.app" |
| 56 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 57 | + }) |
| 58 | + |
| 59 | + test("false for TERM=dumb or TERM=linux even on a TTY", () => { |
| 60 | + setTTY(true) |
| 61 | + process.env.TERM = "dumb" |
| 62 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 63 | + process.env.TERM = "linux" |
| 64 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 65 | + }) |
| 66 | + |
| 67 | + test("true for known-supporting TERM_PROGRAM values", () => { |
| 68 | + setTTY(true) |
| 69 | + for (const program of ["iTerm.app", "WezTerm", "Hyper", "vscode", "ghostty", "Tabby", "rio"]) { |
| 70 | + process.env.TERM_PROGRAM = program |
| 71 | + expect(terminalSupportsHyperlinks()).toBe(true) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + test("false for Apple_Terminal — OSC 8 support can't be inferred from TERM_PROGRAM alone", () => { |
| 76 | + setTTY(true) |
| 77 | + process.env.TERM_PROGRAM = "Apple_Terminal" |
| 78 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 79 | + }) |
| 80 | + |
| 81 | + test("true when WT_SESSION is set (Windows Terminal)", () => { |
| 82 | + setTTY(true) |
| 83 | + process.env.WT_SESSION = "some-guid" |
| 84 | + expect(terminalSupportsHyperlinks()).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + test("true when KONSOLE_VERSION is set", () => { |
| 88 | + setTTY(true) |
| 89 | + process.env.KONSOLE_VERSION = "220400" |
| 90 | + expect(terminalSupportsHyperlinks()).toBe(true) |
| 91 | + }) |
| 92 | + |
| 93 | + test("VTE_VERSION >= 5000 (>= 0.50.0) is supported, below it is not", () => { |
| 94 | + setTTY(true) |
| 95 | + process.env.VTE_VERSION = "5000" |
| 96 | + expect(terminalSupportsHyperlinks()).toBe(true) |
| 97 | + process.env.VTE_VERSION = "4800" |
| 98 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 99 | + }) |
| 100 | + |
| 101 | + test("false with no recognized signal at all", () => { |
| 102 | + setTTY(true) |
| 103 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe("buildManageUrl", () => { |
| 108 | + test("appends /w/<id> to a bare origin", () => { |
| 109 | + expect(buildManageUrl(new URL("https://tenant.ws.myaltimate.com"), 4242)).toBe( |
| 110 | + "https://tenant.ws.myaltimate.com/w/4242", |
| 111 | + ) |
| 112 | + }) |
| 113 | + |
| 114 | + test("joins via pathname, not string concatenation, when the base carries a query/fragment", () => { |
| 115 | + // The dev-only ALTIMATE_WORKSPACE_WEB_URL override can be an arbitrary |
| 116 | + // URL (e.g. a local dev server) — naive `toString() + "/w/id"` |
| 117 | + // concatenation would land the path inside the query string instead. |
| 118 | + const url = buildManageUrl(new URL("http://localhost:3003/base?x=1#frag"), 42) |
| 119 | + expect(url).toBe("http://localhost:3003/base/w/42") |
| 120 | + }) |
| 121 | + |
| 122 | + test("normalizes a trailing slash on the base path", () => { |
| 123 | + expect(buildManageUrl(new URL("https://host/base/"), 7)).toBe("https://host/base/w/7") |
| 124 | + }) |
| 125 | +}) |
| 126 | + |
| 127 | +describe("hyperlink", () => { |
| 128 | + const ORIGINAL_ENV = { ...process.env } |
| 129 | + const ORIGINAL_TTY = process.stdout.isTTY |
| 130 | + |
| 131 | + afterEach(() => { |
| 132 | + process.env = { ...ORIGINAL_ENV } |
| 133 | + Object.defineProperty(process.stdout, "isTTY", { value: ORIGINAL_TTY, configurable: true }) |
| 134 | + }) |
| 135 | + |
| 136 | + test("returns text unchanged when url is null", () => { |
| 137 | + expect(hyperlink("anas-skill-test", null)).toBe("anas-skill-test") |
| 138 | + }) |
| 139 | + |
| 140 | + test("wraps text in OSC 8 with no underline when the terminal isn't recognized", () => { |
| 141 | + Object.defineProperty(process.stdout, "isTTY", { value: false, configurable: true }) |
| 142 | + const out = hyperlink("anas-skill-test", "https://tenant.ws.myaltimate.com/w/4242") |
| 143 | + expect(out).toBe("\x1b]8;;https://tenant.ws.myaltimate.com/w/4242\x1b\\anas-skill-test\x1b]8;;\x1b\\") |
| 144 | + expect(out).not.toContain("\x1b[4m") |
| 145 | + }) |
| 146 | + |
| 147 | + test("wraps text in OSC 8 plus underline when the terminal is recognized as supporting", () => { |
| 148 | + Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true }) |
| 149 | + process.env.TERM_PROGRAM = "iTerm.app" |
| 150 | + const out = hyperlink("anas-skill-test", "https://tenant.ws.myaltimate.com/w/4242") |
| 151 | + expect(out).toBe( |
| 152 | + "\x1b]8;;https://tenant.ws.myaltimate.com/w/4242\x1b\\\x1b[4manas-skill-test\x1b[24m\x1b]8;;\x1b\\", |
| 153 | + ) |
| 154 | + }) |
| 155 | + |
| 156 | + test("sanitizes an adversarial name so it cannot open a second, spoofed link", () => { |
| 157 | + Object.defineProperty(process.stdout, "isTTY", { value: false, configurable: true }) |
| 158 | + const malicious = "name\x1b]8;;http://evil.example\x1b\\CLICK ME\x1b]8;;\x1b\\" |
| 159 | + const out = hyperlink(malicious, "https://tenant.ws.myaltimate.com/w/4242") |
| 160 | + // Exactly one real OSC 8 open + one real OSC 8 close — the malicious |
| 161 | + // payload's own OSC 8 bytes were stripped, leaving only inert text. |
| 162 | + expect(out.split("\x1b]8;;").length - 1).toBe(2) |
| 163 | + expect(out).toContain("name]8;;http://evil.example\\CLICK ME]8;;\\") |
| 164 | + }) |
| 165 | +}) |
0 commit comments