Skip to content

Commit b324131

Browse files
sahrizviclaude
andcommitted
test(workspace): stop create-then-rebind flaking on stray lookups, and opening a browser
`bun test` runs every file in one process, so a background skill or memory sync still in flight from an earlier file can land its binding lookup (`GET /datamate-project-bindings/by-path`) in this file's fetch stub. The endpoint-sequence filter kept it, and "unlinked project uses the atomic create-and-bind" failed at random on CI — on `main` as well. Every request these tests assert is a write, so lookups are now ignored. A new test replays the stray lookup; removing the filter reproduces CI's exact failure. A create also opens the new workspace's manage URL with `open`, which the test never stubbed, so a local run opened `https://x.test/w/7` in the real browser. It is now stubbed before the modules load (the same pattern as `oauth-browser.test.ts`) and recorded, so the test still asserts the open. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7ed6e16 commit b324131

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

‎packages/opencode/test/altimate/workspace/create-then-rebind.test.ts‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// linked, so the rebind that was meant to follow was unreachable and the row
88
// always failed. A test that only checks payloads cannot see that, which is why
99
// these assert the *sequence of endpoints* instead.
10-
import { afterAll, afterEach, beforeEach, describe, expect, test } from "bun:test"
10+
import { afterAll, afterEach, beforeEach, describe, expect, mock, test } from "bun:test"
1111
import { mkdirSync, writeFileSync } from "node:fs"
1212
import path from "node:path"
1313
import os from "node:os"
@@ -29,6 +29,16 @@ writeFileSync(
2929
JSON.stringify({ altimateUrl: API_URL, altimateInstanceName: "acme", altimateApiKey: "test-key" }),
3030
)
3131

32+
// A create opens the new workspace's manage URL in the browser. Stubbed before
33+
// the modules under test load, so a local run does not open real tabs; recorded,
34+
// so the tests still prove the open happens.
35+
const opened: string[] = []
36+
mock.module("open", () => ({
37+
default: async (url: string) => {
38+
opened.push(url)
39+
},
40+
}))
41+
3242
const { createThenBindOrRebind } = await import("@/cli/cmd/link")
3343
const { createAndBindInline } = await import("@/plugin/tui/altimate/workspace")
3444

@@ -63,10 +73,16 @@ function stubFetch() {
6373
}
6474

6575
/** Endpoint sequence, ignoring the best-effort memory/skill traffic that
66-
* `recordApprovedBinding` kicks off — this is about which create ran. */
76+
* `recordApprovedBinding` kicks off — this is about which create ran.
77+
*
78+
* Lookups (GET) are ignored too. `bun test` runs every file in one process, so a
79+
* background sync still in flight from an earlier file can land its binding
80+
* lookup (`GET /datamate-project-bindings/by-path`) in this file's stub, which
81+
* failed this at random on CI. Every request these tests assert is a write. */
6782
const sequence = () =>
6883
calls
6984
.filter((c) => c.path.includes("/datamates") || c.path.includes("/datamate-project-bindings"))
85+
.filter((c) => c.method !== "GET")
7086
.map((c) => `${c.method} ${c.path}`)
7187

7288
const BINDING = {
@@ -87,6 +103,7 @@ const EXISTING = {
87103
beforeEach(() => {
88104
calls = []
89105
routes = []
106+
opened.length = 0
90107
stubFetch()
91108
})
92109
afterEach(() => {
@@ -113,9 +130,26 @@ describe("CLI: createThenBindOrRebind", () => {
113130
await createThenBindOrRebind(IDENTIFIER, "proj", "/tmp/proj", null)
114131

115132
expect(sequence()).toEqual(["POST /datamate-project-bindings/"])
133+
expect(opened).toEqual(["https://x.test/w/7"])
116134
expect(process.exitCode ?? 0).toBe(0)
117135
})
118136

137+
test("a background lookup landing mid-flow does not change which create ran", async () => {
138+
routes = [
139+
{
140+
match: /datamate-project-bindings\/$/,
141+
method: "POST",
142+
status: 200,
143+
body: { datamate: { id: 7, name: "proj" }, binding: BINDING, manage_url: "https://x.test/w/7" },
144+
},
145+
]
146+
// What CI hit: a sync from another file resolving its binding through this stub.
147+
await fetch(`${API_URL}/datamate-project-bindings/by-path?project_path=%2Felsewhere`)
148+
await createThenBindOrRebind(IDENTIFIER, "proj", "/tmp/proj", null)
149+
150+
expect(sequence()).toEqual(["POST /datamate-project-bindings/"])
151+
})
152+
119153
test("already-linked project creates UNBOUND, then rebinds", async () => {
120154
routes = [
121155
{ match: /\/datamates\/$/, method: "POST", status: 200, body: { id: 7 } },

0 commit comments

Comments
 (0)