| 1 | const assert = require("node:assert/strict"); |
| 2 | const fs = require("node:fs"); |
| 3 | const os = require("node:os"); |
| 4 | const path = require("node:path"); |
| 5 | const { test } = require("node:test"); |
| 6 | const vm = require("node:vm"); |
| 7 | |
| 8 | const source = fs.readFileSync(path.join(__dirname, "agent-linear.js"), "utf8"); |
| 9 | |
| 10 | async function run(t, { prefixes, text, command = "prompt", xdg = false, assigned = false }) { |
| 11 | const home = fs.mkdtempSync(path.join(os.tmpdir(), "agent-linear-test-")); |
| 12 | t.after(() => fs.rmSync(home, { recursive: true, force: true })); |
| 13 | const config = xdg ? path.join(home, "custom-config") : path.join(home, ".config"); |
| 14 | if (prefixes !== undefined) { |
| 15 | fs.mkdirSync(path.join(config, "agent-linear"), { recursive: true }); |
| 16 | fs.writeFileSync(path.join(config, "agent-linear", "prefixes"), prefixes); |
| 17 | } |
| 18 | if (xdg) { |
| 19 | fs.mkdirSync(path.join(home, ".config", "agent-linear"), { recursive: true }); |
| 20 | fs.writeFileSync(path.join(home, ".config", "agent-linear", "prefixes"), "WRONG\n"); |
| 21 | } |
| 22 | |
| 23 | const requests = []; |
| 24 | const output = []; |
| 25 | const context = vm.createContext({ |
| 26 | require(name) { |
| 27 | if (name === "node:fs") { |
| 28 | return { |
| 29 | ...fs, |
| 30 | readFileSync(file, encoding) { |
| 31 | return file === 0 |
| 32 | ? JSON.stringify({ hook_event_name: "UserPromptSubmit", prompt: text }) |
| 33 | : fs.readFileSync(file, encoding); |
| 34 | }, |
| 35 | }; |
| 36 | } |
| 37 | if (name === "node:child_process") { |
| 38 | return { |
| 39 | spawnSync(executable, argv) { |
| 40 | assert.equal(executable, "jj"); |
| 41 | const stdout = argv.includes("log") |
| 42 | ? argv.at(-1) === "description" ? text : "change-id\n" |
| 43 | : ""; |
| 44 | return { status: 0, stdout }; |
| 45 | }, |
| 46 | }; |
| 47 | } |
| 48 | if (name === "node:process") { |
| 49 | return { |
| 50 | argv: ["node", "agent-linear", ...( |
| 51 | command === "jj" ? ["jj", "jj", "desc", "-m", text] : ["prompt"] |
| 52 | )], |
| 53 | env: { HOME: home, LINEAR_API_KEY: "test", ...(xdg ? { XDG_CONFIG_HOME: config } : {}) }, |
| 54 | stdout: { write: (value) => output.push(value) }, |
| 55 | exit: (code) => assert.fail(`unexpected exit ${code}`), |
| 56 | }; |
| 57 | } |
| 58 | return require(name); |
| 59 | }, |
| 60 | console: { error: (value) => output.push(value) }, |
| 61 | AbortSignal, |
| 62 | async fetch(url, options) { |
| 63 | assert.equal(url, "https://api.linear.app/graphql"); |
| 64 | const { query, variables } = JSON.parse(options.body); |
| 65 | requests.push({ query, variables }); |
| 66 | const data = query.includes("IssueProgress") |
| 67 | ? { issue: { |
| 68 | id: variables.id, |
| 69 | state: { name: "Todo" }, |
| 70 | assignee: assigned ? { id: "someone" } : null, |
| 71 | team: { states: { nodes: [{ id: "started", name: "In Progress", type: "started" }] } }, |
| 72 | } } |
| 73 | : query.includes("issueUpdate") |
| 74 | ? { issueUpdate: { success: true } } |
| 75 | : { viewer: { id: "viewer", name: "Clover" } }; |
| 76 | return { ok: true, text: async () => JSON.stringify({ data }) }; |
| 77 | }, |
| 78 | }); |
| 79 | |
| 80 | await vm.runInContext(source, context); |
| 81 | return { requests, output: output.join("") }; |
| 82 | } |
| 83 | |
| 84 | for (const command of ["prompt", "jj"]) { |
| 85 | test(`${command}: only configured prefixes start issues`, async (t) => { |
| 86 | const { requests, output } = await run(t, { |
| 87 | command, |
| 88 | prefixes: " alpha \r\nBETA\r\n\r\n", |
| 89 | text: "ALPHA-12 alpha-12 beta-3 GAMMA-4 XALPHA-5 ALPHA-6suffix ALPHA-7-8 ALPHA-8.1 _ALPHA-9 https://linear.app/team/issue/ALPHA-10/title", |
| 90 | }); |
| 91 | assert.deepEqual( |
| 92 | requests.filter(({ query }) => query.includes("IssueProgress")).map(({ variables }) => variables.id), |
| 93 | ["ALPHA-12", "BETA-3", "ALPHA-10"], |
| 94 | ); |
| 95 | assert.equal(requests.filter(({ query }) => query.includes("issueUpdate")).length, 3); |
| 96 | assert.match(output, /ALPHA-12 → In Progress/); |
| 97 | }); |
| 98 | |
| 99 | for (const prefixes of [undefined, "", "\n \r\n"]) { |
| 100 | test(`${command}: ${JSON.stringify(prefixes)} configuration disables automatic requests`, async (t) => { |
| 101 | const { requests, output } = await run(t, { command, prefixes, text: "ALPHA-123 OTHER-456" }); |
| 102 | assert.deepEqual(requests, []); |
| 103 | assert.equal(output, ""); |
| 104 | }); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | test("XDG_CONFIG_HOME overrides the default directory; prefixes are literal", async (t) => { |
| 109 | const { requests } = await run(t, { |
| 110 | xdg: true, |
| 111 | prefixes: "ALPHA\nB.*\n", |
| 112 | text: "ALPHA-1 BETA-2 WRONG-3", |
| 113 | }); |
| 114 | assert.deepEqual( |
| 115 | requests.filter(({ query }) => query.includes("IssueProgress")).map(({ variables }) => variables.id), |
| 116 | ["ALPHA-1"], |
| 117 | ); |
| 118 | }); |
| 119 | |
| 120 | test("prompts leave assigned issues alone; jj descriptions still start them", async (t) => { |
| 121 | for (const command of ["prompt", "jj"]) { |
| 122 | const { requests } = await run(t, { command, prefixes: "ALPHA", text: "ALPHA-1", assigned: true }); |
| 123 | assert.equal(requests.some(({ query }) => query.includes("issueUpdate")), command === "jj"); |
| 124 | } |
| 125 | }); |