|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { mkdtemp, realpath, rm } from "node:fs/promises"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { setImmediate } from "node:timers/promises"; |
| 7 | +import type { Plugin } from "@opencode/plugin"; |
| 8 | +import { cleanup, heartbeat, touchOwner, type OwnerClient } from "../src/lifecycle.js"; |
| 9 | +import github from "../src/plugins/github.js"; |
| 10 | +import scheduler from "../src/plugins/scheduler.js"; |
| 11 | +import { acquire } from "../src/state.js"; |
| 12 | +import { runtimeBridge } from "../src/bridge.js"; |
| 13 | + |
| 14 | +test("cleanup settles work and releases ownership even after disposal failures", async () => { |
| 15 | + const steps: string[] = []; |
| 16 | + const failure = new Error("SDK scope was evicted"); |
| 17 | + await assert.rejects(cleanup( |
| 18 | + async () => { steps.push("settled"); }, |
| 19 | + () => { steps.push("dispose"); throw failure; }, |
| 20 | + () => { steps.push("bridge removed"); }, |
| 21 | + () => { steps.push("lock released"); }, |
| 22 | + ), (error: AggregateError) => error.errors[0] === failure); |
| 23 | + assert.deepEqual(steps, ["settled", "dispose", "bridge removed", "lock released"]); |
| 24 | +}); |
| 25 | + |
| 26 | +for (const kind of ["github", "scheduler"] as const) { |
| 27 | + test(`${kind} plugin releases its actual lock when RPC disposal rejects`, async () => { |
| 28 | + const directory = await realpath(await mkdtemp(join(tmpdir(), "oc2-lifecycle-"))); |
| 29 | + const tokenName = "OC2_LIFECYCLE_TEST_TOKEN"; |
| 30 | + process.env[tokenName] = "test-token"; |
| 31 | + const rpc = Object.assign(() => ({ scan: async () => ({}) }), { |
| 32 | + register: async () => ({ dispose: async () => { throw new Error("evicted registration"); }, events: { emit: async () => {} } }), |
| 33 | + }); |
| 34 | + const options = kind === "github" ? { |
| 35 | + tokenEnv: tokenName, ownerDirectory: directory, stateDirectory: directory, |
| 36 | + autoMerge: { enabled: false }, |
| 37 | + repositories: [{ repo: "owner/repo", directory, baseBranch: "main", allowedAuthors: ["alice"], checks: [] }], |
| 38 | + routes: { "@bot": { model: { providerID: "test", id: "model" } } }, |
| 39 | + } : { ownerDirectory: directory, stateDirectory: directory, jobs: [{ id: "scan", everySeconds: 60 }] }; |
| 40 | + try { |
| 41 | + const plugin = kind === "github" ? github : scheduler; |
| 42 | + const stop = await plugin.setup({ location: { directory }, options, rpc } as unknown as Plugin.Context); |
| 43 | + assert.ok(stop); |
| 44 | + await assert.rejects(async () => stop(), /Automation cleanup failed/); |
| 45 | + const release = await acquire(directory, kind, () => {}); |
| 46 | + await release(); |
| 47 | + assert.equal(runtimeBridge(directory), undefined); |
| 48 | + } finally { |
| 49 | + delete process.env[tokenName]; |
| 50 | + await rm(directory, { recursive: true, force: true }); |
| 51 | + } |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +test("owner heartbeat only touches the matching service process and owner directory", async () => { |
| 56 | + const calls: string[] = []; |
| 57 | + let pid = process.pid + 1; |
| 58 | + const client: OwnerClient = { |
| 59 | + health: { get: async () => ({ pid }) }, |
| 60 | + plugin: { list: async ({ location }) => { calls.push(location.directory); } }, |
| 61 | + }; |
| 62 | + const signal = new AbortController().signal; |
| 63 | + assert.equal(await touchOwner("/owner", signal, async () => undefined), false); |
| 64 | + assert.equal(await touchOwner("/owner", signal, async () => client), false); |
| 65 | + assert.deepEqual(calls, []); |
| 66 | + pid = process.pid; |
| 67 | + assert.equal(await touchOwner("/owner", signal, async () => client), true); |
| 68 | + assert.deepEqual(calls, ["/owner"]); |
| 69 | +}); |
| 70 | + |
| 71 | +test("heartbeat does not overlap requests and aborts pending work on cleanup", async t => { |
| 72 | + t.mock.timers.enable({ apis: ["setInterval"] }); |
| 73 | + let calls = 0; |
| 74 | + let pendingSignal: AbortSignal | undefined; |
| 75 | + const errors: unknown[] = []; |
| 76 | + const stop = heartbeat(signal => { |
| 77 | + calls++; |
| 78 | + pendingSignal = signal; |
| 79 | + return new Promise((_, reject) => signal.addEventListener("abort", () => reject(signal.reason), { once: true })); |
| 80 | + }, error => errors.push(error)); |
| 81 | + t.mock.timers.tick(30_000); |
| 82 | + await setImmediate(); |
| 83 | + t.mock.timers.tick(60_000); |
| 84 | + await setImmediate(); |
| 85 | + assert.equal(calls, 1); |
| 86 | + await stop(); |
| 87 | + assert.equal(pendingSignal?.aborted, true); |
| 88 | + assert.deepEqual(errors, []); |
| 89 | + t.mock.timers.tick(60_000); |
| 90 | + await setImmediate(); |
| 91 | + assert.equal(calls, 1); |
| 92 | +}); |
| 93 | + |
| 94 | +test("heartbeat retries after a transient failure", async t => { |
| 95 | + t.mock.timers.enable({ apis: ["setInterval"] }); |
| 96 | + let calls = 0; |
| 97 | + const errors: unknown[] = []; |
| 98 | + const stop = heartbeat(async () => { if (++calls === 1) throw new Error("connection lost"); }, error => errors.push(error)); |
| 99 | + try { |
| 100 | + t.mock.timers.tick(30_000); |
| 101 | + await setImmediate(); |
| 102 | + t.mock.timers.tick(30_000); |
| 103 | + await setImmediate(); |
| 104 | + assert.equal(calls, 2); |
| 105 | + assert.equal(errors.length, 1); |
| 106 | + } finally { await stop(); } |
| 107 | +}); |
0 commit comments