diff --git a/.github/workflows/v2-lifecycle-contract.yml b/.github/workflows/v2-lifecycle-contract.yml index bc13910..554a17c 100644 --- a/.github/workflows/v2-lifecycle-contract.yml +++ b/.github/workflows/v2-lifecycle-contract.yml @@ -6,6 +6,7 @@ on: - "src/source/opencode2/**" - "src/source/runtime/**" - "scripts/v2-event-bridge-test.mjs" + - "scripts/v2-event-stream-contract-test.mjs" - "scripts/v2-plugin-sentinel-test.mjs" - ".github/workflows/v2-lifecycle-contract.yml" push: @@ -14,6 +15,7 @@ on: - "src/source/opencode2/**" - "src/source/runtime/**" - "scripts/v2-event-bridge-test.mjs" + - "scripts/v2-event-stream-contract-test.mjs" - "scripts/v2-plugin-sentinel-test.mjs" - ".github/workflows/v2-lifecycle-contract.yml" @@ -40,5 +42,7 @@ jobs: - run: node --check src/source/opencode2/event-bridge.js - run: node --check src/source/opencode2/experimental.js - run: node --check scripts/v2-event-bridge-test.mjs + - run: node --check scripts/v2-event-stream-contract-test.mjs - run: node scripts/v2-event-bridge-test.mjs + - run: node scripts/v2-event-stream-contract-test.mjs - run: node scripts/v2-plugin-sentinel-test.mjs diff --git a/scripts/v2-event-stream-contract-test.mjs b/scripts/v2-event-stream-contract-test.mjs new file mode 100644 index 0000000..21a984b --- /dev/null +++ b/scripts/v2-event-stream-contract-test.mjs @@ -0,0 +1,38 @@ +import assert from "node:assert/strict" +import { createOpenCode2EventBridge } from "../src/source/opencode2/event-bridge.js" + +const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) +const seen = [] +let subscribeArgs = -1 +let cleanupCalls = 0 + +async function* stream() { + yield { + directory: "/project", + payload: { + type: "session.created", + properties: { info: { id: "ses_stream", directory: "/project" } }, + }, + } +} + +const bridge = createOpenCode2EventBridge({ + directory: "/project", + onEvent: async (event) => seen.push(event), +}) + +await bridge.attach(async (...args) => { + subscribeArgs = args.length + return { stream: stream(), dispose: async () => { cleanupCalls += 1 } } +}) + +for (let attempt = 0; attempt < 50 && seen.length === 0; attempt++) await delay(5) +assert.equal(subscribeArgs, 0) +assert.equal(seen[0]?.action, "created") +assert.ok(bridge.runtimeManager.peek("ses_stream")) + +assert.equal(await bridge.dispose(), true) +assert.equal(cleanupCalls, 1) +assert.equal(bridge.runtimeManager.peek("ses_stream"), undefined) + +console.log("OpenCode 2 event stream contract passed")