Skip to content

Commit e4b102b

Browse files
test(activate): assert the stranded creation settles instead of leaving it parked
The row on this file flagged a leak, and the finally did already fix the override it named. It did not fix the other half. All three gates sit inside the try, so a failing assertion or a waitFor timeout leaves a gate parked, the creation awaiting it never settles, and registerCommands.ts clears apiTabPanelCreation only when that creation settles (:448-451) while the reuse paths keep awaiting that chain (:529-530, guard at :601-602) - so one stranded creation occupies the queue for the rest of the file. Each gate now carries a release count, the finally releases any gate the test did not already let go, both creations are marked rejection-handled at creation so a failure here cannot surface as an unhandled rejection in the next test, and the drain is awaited in the finally: were a gate still parked, that await would not return and this file would report the timeout instead of the next test breaking. The green path asserts the terminal states directly: three gates released once, the API creation rejected by the lock failure, the ordinary creation fulfilling with its own provider. Controls under node 22.23.4 (the .nvmrc pin; PATH carries v25.9.0): with the release loop removed and the park condition tampered, the knock-on the row described reproduces - this test and 23 later tests in the file each print Test timed out in 20000ms; with the release loop present the same tamper fails only this test with "condition never held" (1 failed | 81 passed); swapping the expected outcome pair fails with an AssertionError naming the two statuses. Untampered: 82 passed (82) three consecutive runs, tsc --noEmit -p tsconfig.json exit 0, prettier --check clean, eslint --max-warnings=0 exit 0 with src/eslint-suppressions.json byte-identically restored at 30,910 B and its count unchanged. Test-only: src/activate/ registerCommands.ts stays blob ac81bfa.
1 parent 8936544 commit e4b102b

1 file changed

Lines changed: 74 additions & 16 deletions

File tree

‎src/activate/__tests__/registerCommands.spec.ts‎

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,12 +1482,23 @@ describe("openClineInNewTab", () => {
14821482
// Each gate is a promise whose resolver a promise executor installs. TS cannot see an
14831483
// assignment made inside the executor from a sibling `let`, so the settle function starts as a
14841484
// no-op and the executor always replaces it.
1485-
const makeGate = (): { wait: Promise<void>; settle: (error?: Error) => void } => {
1485+
const makeGate = (): { wait: Promise<void>; settle: (error?: Error) => void; releases: number } => {
14861486
let settle: (error?: Error) => void = () => undefined
1487-
const wait = new Promise<void>((resolve, reject) => {
1488-
settle = (error) => (error === undefined ? resolve() : reject(error))
1487+
// releases counts how many times the gate was actually let go, which is what the leak
1488+
// assertions below read: a gate settled twice means the test asked twice, and a gate at
1489+
// zero at the end of the test means a creation is still parked on it.
1490+
const gate = { wait: Promise.resolve(), settle: (error?: Error) => settle(error), releases: 0 }
1491+
gate.wait = new Promise<void>((resolve, reject) => {
1492+
settle = (error) => {
1493+
gate.releases += 1
1494+
if (error === undefined) {
1495+
resolve()
1496+
} else {
1497+
reject(error)
1498+
}
1499+
}
14891500
})
1490-
return { wait, settle }
1501+
return gate
14911502
}
14921503
const ordinaryGroup = makeGate()
14931504
const apiGroup = makeGate()
@@ -1516,17 +1527,31 @@ describe("openClineInNewTab", () => {
15161527
return previousExecute?.(command)
15171528
})
15181529

1530+
// Both creations are marked handled at creation. A rejection nobody awaits fires into the
1531+
// next test as an unhandled rejection, which is how a stranded creation in this one shows up
1532+
// as a failure somewhere else; marking handled does not change what awaiting them sees.
1533+
const markHandled = <T>(promise: Promise<T>): Promise<T> => {
1534+
void promise.catch(() => undefined)
1535+
return promise
1536+
}
1537+
let apiAttempt: Promise<unknown> | undefined
1538+
let ordinaryAttempt: Promise<unknown> | undefined
1539+
let reachedEnd = false
15191540
try {
1520-
const apiAttempt = openClineInNewTabForApi({
1521-
context: mockContext,
1522-
outputChannel: mockOutputChannel,
1523-
webviewFocusTracker: new WebviewFocusTracker(),
1524-
})
1525-
const ordinaryAttempt = openClineInNewTab({
1526-
context: mockContext,
1527-
outputChannel: mockOutputChannel,
1528-
webviewFocusTracker: new WebviewFocusTracker(),
1529-
})
1541+
const apiPromise = (apiAttempt = markHandled(
1542+
openClineInNewTabForApi({
1543+
context: mockContext,
1544+
outputChannel: mockOutputChannel,
1545+
webviewFocusTracker: new WebviewFocusTracker(),
1546+
}),
1547+
))
1548+
const ordinaryPromise = (ordinaryAttempt = markHandled(
1549+
openClineInNewTab({
1550+
context: mockContext,
1551+
outputChannel: mockOutputChannel,
1552+
webviewFocusTracker: new WebviewFocusTracker(),
1553+
}),
1554+
))
15301555
await waitFor("both creations parked on their group command", () => groupCalls === 2)
15311556
apiGroup.settle()
15321557
// Published, registered, creation still unsettled: the dispose listener is installed after
@@ -1545,8 +1570,8 @@ describe("openClineInNewTab", () => {
15451570
// could adopt fails from here on.
15461571
apiLock.settle(new Error("api lock failed"))
15471572

1548-
await expect(apiAttempt).rejects.toThrow("api lock failed")
1549-
const returned = await ordinaryAttempt
1573+
await expect(apiPromise).rejects.toThrow("api lock failed")
1574+
const returned = await ordinaryPromise
15501575

15511576
// The caller keeps its own provider. Without the wait in the later reuse check the adoption
15521577
// happens first and this reddens on provider identity, naming the raced provider rather
@@ -1564,12 +1589,45 @@ describe("openClineInNewTab", () => {
15641589
// The ordinary creation still opened its own tab, and that tab is the tracked one.
15651590
expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(2)
15661591
expect(getPanel()).toBe(ordinaryPanel)
1592+
// The stranded creation is asserted, not tolerated. registerCommands.ts clears
1593+
// apiTabPanelCreation only when the creation settles (:448-451), and the reuse paths await
1594+
// that chain (:529-530, and the guard at :601-602), so the queue is only empty again once
1595+
// this creation has reached a terminal state — which is what these two assertions prove.
1596+
expect([ordinaryGroup.releases, apiGroup.releases, apiLock.releases]).toEqual([1, 1, 1])
1597+
const outcomes = await Promise.allSettled([apiPromise, ordinaryPromise])
1598+
expect(outcomes.map((outcome) => outcome.status)).toEqual(["rejected", "fulfilled"])
1599+
expect((outcomes[0] as PromiseRejectedResult).reason).toBeInstanceOf(Error)
1600+
expect((outcomes[0] as PromiseRejectedResult).reason).toHaveProperty("message", "api lock failed")
1601+
expect((outcomes[1] as PromiseFulfilledResult<unknown>).value).toBe(ordinaryProvider)
1602+
reachedEnd = true
15671603
} finally {
1604+
// Release every gate the test did not already let go. A failing assertion or a waitFor
1605+
// timeout used to leave a gate parked, and a parked gate is the stranding: the creation
1606+
// awaiting it never settles, so apiTabPanelCreation (registerCommands.ts:448-451) stays
1607+
// occupied and every later reuse path in this file awaits a creation that already failed
1608+
// (:529-530, :601-602). Releasing here is what makes the stranded creation terminate.
1609+
for (const gate of [ordinaryGroup, apiGroup, apiLock]) {
1610+
if (gate.releases === 0) {
1611+
gate.settle()
1612+
}
1613+
}
15681614
// Restore the saved implementation rather than mockRestore(): this file's beforeEach only
15691615
// clears call history, and mockRestore() on a bare vi.fn() leaves executeCommand with no
15701616
// implementation at all, so every later tab creation in this file breaks. In a finally so a
15711617
// failing assertion cannot leak the override.
15721618
executeCommandMock.mockImplementation(previousExecute ?? (async () => undefined))
1619+
// The drain is awaited, not assumed. If a gate were still parked this await would never
1620+
// return and this file would report the timeout, naming this test rather than the next one.
1621+
const drained = apiAttempt && ordinaryAttempt ? await Promise.allSettled([apiAttempt, ordinaryAttempt]) : []
1622+
if (!reachedEnd && drained.length === 2) {
1623+
// The leak assertion for the failure path: no gate left parked, both creations terminal,
1624+
// so nothing is chained into whatever test runs next in this file.
1625+
expect([ordinaryGroup.releases, apiGroup.releases, apiLock.releases]).toEqual([1, 1, 1])
1626+
expect(drained.map((outcome) => outcome.status)).toEqual([
1627+
expect.stringMatching(/^(fulfilled|rejected)$/),
1628+
expect.stringMatching(/^(fulfilled|rejected)$/),
1629+
])
1630+
}
15731631
}
15741632
})
15751633

0 commit comments

Comments
 (0)