|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises" |
| 3 | +import os from "node:os" |
| 4 | +import path from "node:path" |
| 5 | +import test from "node:test" |
| 6 | + |
| 7 | +import { main, MAX_PROMPT_LENGTH, isAllowedAssociation, parseCommand } from "./opencode-command.mjs" |
| 8 | + |
| 9 | +test("uses separate defaults and preserves command aliases", () => { |
| 10 | + assert.deepEqual(parseCommand("/oc plan inspect the role"), { |
| 11 | + mode: "plan", |
| 12 | + model: "openai/gpt-5.6-sol", |
| 13 | + prompt: "inspect the role", |
| 14 | + }) |
| 15 | + assert.deepEqual(parseCommand("/opencode build implement the role"), { |
| 16 | + mode: "build", |
| 17 | + model: "openai/gpt-5.6-terra", |
| 18 | + prompt: "implement the role", |
| 19 | + }) |
| 20 | +}) |
| 21 | + |
| 22 | +test("accepts only allowlisted model aliases", () => { |
| 23 | + assert.equal(parseCommand("/oc build --model luna make the change").model, "openai/gpt-5.6-luna") |
| 24 | + assert.throws(() => parseCommand("/oc build --model openai/gpt-5.6 make the change")) |
| 25 | +}) |
| 26 | + |
| 27 | +test("requires an exact command at the beginning", () => { |
| 28 | + assert.throws(() => parseCommand("please /oc plan inspect this")) |
| 29 | + assert.throws(() => parseCommand("/oc build")) |
| 30 | + assert.throws(() => parseCommand(" /oc plan inspect this")) |
| 31 | +}) |
| 32 | + |
| 33 | +test("normalizes CRLF and preserves multiline prompts", () => { |
| 34 | + assert.equal(parseCommand("/oc plan inspect this\r\nand include tests").prompt, "inspect this\nand include tests") |
| 35 | +}) |
| 36 | + |
| 37 | +test("enforces the prompt length limit", () => { |
| 38 | + assert.equal(parseCommand(`/oc plan ${"a".repeat(MAX_PROMPT_LENGTH)}`).prompt.length, MAX_PROMPT_LENGTH) |
| 39 | + assert.throws(() => parseCommand(`/oc plan ${"a".repeat(MAX_PROMPT_LENGTH + 1)}`)) |
| 40 | +}) |
| 41 | + |
| 42 | +test("rejects unsupported control characters but permits tab and newline", () => { |
| 43 | + assert.equal(parseCommand("/oc plan line\n\tmore").prompt, "line\n\tmore") |
| 44 | + assert.throws(() => parseCommand("/oc plan nul\0value")) |
| 45 | + assert.throws(() => parseCommand("/oc plan escape\x1bvalue")) |
| 46 | + assert.throws(() => parseCommand("/oc plan delete\x7fvalue")) |
| 47 | +}) |
| 48 | + |
| 49 | +test("rejects GitHub attachment URLs before OpenCode can download them", () => { |
| 50 | + assert.throws( |
| 51 | + () => parseCommand("/oc plan inspect https://github.com/user-attachments/assets/unbounded"), |
| 52 | + /attachment URLs/, |
| 53 | + ) |
| 54 | +}) |
| 55 | + |
| 56 | +test("allows only trusted repository associations", () => { |
| 57 | + assert.equal(isAllowedAssociation("OWNER"), true) |
| 58 | + assert.equal(isAllowedAssociation("MEMBER"), true) |
| 59 | + assert.equal(isAllowedAssociation("COLLABORATOR"), true) |
| 60 | + assert.equal(isAllowedAssociation("CONTRIBUTOR"), false) |
| 61 | +}) |
| 62 | + |
| 63 | +test("main writes multiline outputs and validates its environment", async () => { |
| 64 | + const directory = await mkdtemp(path.join(os.tmpdir(), "opencode-command-")) |
| 65 | + const eventPath = path.join(directory, "event.json") |
| 66 | + const outputPath = path.join(directory, "output") |
| 67 | + try { |
| 68 | + await writeFile(eventPath, JSON.stringify({ comment: { author_association: "OWNER", body: "/oc plan first\nsecond" } })) |
| 69 | + main({ GITHUB_EVENT_PATH: eventPath, GITHUB_OUTPUT: outputPath }) |
| 70 | + const output = await readFile(outputPath, "utf8") |
| 71 | + assert.match(output, /prompt<<opencode_[^\n]+\nfirst\nsecond\nopencode_/) |
| 72 | + assert.throws(() => main({ GITHUB_OUTPUT: outputPath }), /GITHUB_EVENT_PATH/) |
| 73 | + assert.throws(() => main({ GITHUB_EVENT_PATH: eventPath }), /GITHUB_OUTPUT/) |
| 74 | + await writeFile(eventPath, "{") |
| 75 | + assert.throws(() => main({ GITHUB_EVENT_PATH: eventPath, GITHUB_OUTPUT: outputPath }), SyntaxError) |
| 76 | + } finally { |
| 77 | + await rm(directory, { recursive: true, force: true }) |
| 78 | + } |
| 79 | +}) |
0 commit comments