diff --git a/server/src/__tests__/issue-recovery-actions.test.ts b/server/src/__tests__/issue-recovery-actions.test.ts index cc9c89fe369..dd1228d49a7 100644 --- a/server/src/__tests__/issue-recovery-actions.test.ts +++ b/server/src/__tests__/issue-recovery-actions.test.ts @@ -2222,6 +2222,12 @@ describeEmbeddedPostgres("issue recovery actions", () => { await db.delete(issueRecoveryActions).where(eq(issueRecoveryActions.id, f.hold.id)); await seedLiveOwnerRun(f); await request(f.agent).patch(`/api/issues/${f.sourceIssueId}`).send({ status: "todo" }).expect(200); + await vi.waitFor(async () => { + const wakes = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.companyId, f.companyId)); + expect(wakes).toEqual(expect.arrayContaining([expect.objectContaining({ + agentId: f.coderId, payload: expect.objectContaining({ issueId: f.sourceIssueId }), + })])); + }, { timeout: 5_000 }); }); async function seedReconciledDelivery() { diff --git a/server/src/services/connectors/tailscale/device-login-network.test.ts b/server/src/services/connectors/tailscale/device-login-network.test.ts index 8b2a4f3a4b2..62e825a15f3 100644 --- a/server/src/services/connectors/tailscale/device-login-network.test.ts +++ b/server/src/services/connectors/tailscale/device-login-network.test.ts @@ -9,6 +9,8 @@ async function publishState(statePath: string, state: unknown) { const nextPath = `${statePath}.next`; await fs.writeFile(nextPath, JSON.stringify(state)); await fs.rename(nextPath, statePath); + const pid = Number(await fs.readFile(path.join(path.dirname(statePath), "up-pid"), "utf8")); + process.kill(pid, "SIGUSR2"); } describe("Tailscale interactive device enrollment", () => { @@ -36,10 +38,15 @@ if (args[1] === 'up') { if (args.some(arg => arg.startsWith('--auth-key'))) process.exit(5); if (state.BackendState === 'Running') process.exit(0); state = { MachineKey: state.MachineKey || path.basename(dir), BackendState: 'NeedsLogin', AuthURL: 'https://login.tailscale.com/a/fixture' }; - fs.writeFileSync(statePath, JSON.stringify(state)); - fs.watch(dir, () => { + const check = () => { if (JSON.parse(fs.readFileSync(statePath, 'utf8')).BackendState === 'Running') process.exit(0); - }); + }; + process.on('SIGUSR2', check); + fs.writeFileSync(path.join(dir, 'up-pid'), String(process.pid)); + fs.writeFileSync(statePath + '.next', JSON.stringify(state)); + fs.renameSync(statePath + '.next', statePath); + check(); + setInterval(() => {}, 1000); } else if (args[1] === 'status') console.log(JSON.stringify(state)); `, { mode: 0o700 }); manager = createTailscaleNetworkManager({ daemon: path.join(dir, "daemon"), cli: path.join(dir, "cli") }); @@ -59,7 +66,8 @@ if (args[1] === 'up') { expect(credential).not.toBe(emptyState); expect(authorized).not.toHaveBeenCalled(); await publishState(statePath, { ...state, BackendState: "Running", AuthURL: "" }); - await vi.waitFor(() => expect(authorized).toHaveBeenCalledOnce()); + expect(await manager.loginStatus(input.identity)).toEqual({ status: "connected" }); + expect(authorized).toHaveBeenCalledOnce(); await manager.close(); const restored = await manager.connect({ identity: input.identity, credential, persist: input.persist }); expect(JSON.parse(await fs.readFile(path.join(path.dirname(restored), "state"), "utf8")).MachineKey).toBe(state.MachineKey); @@ -83,12 +91,19 @@ if (args[1] === 'up') { it("stops an expired sign-in without accepting late consent", async () => { const authorized = vi.fn(); const failure = vi.fn().mockResolvedValue(undefined); + const expiresAt = Date.now() + 60_000; const socket = await manager.connect({ identity: "expires", credential: emptyState, persist: vi.fn(), - login: { expiresAt: Date.now() + 1200, onAuthorized: authorized, onFailure: failure } }); - await vi.waitFor(() => expect(failure).toHaveBeenCalledOnce(), { timeout: 4000 }); - await vi.waitFor(async () => expect(await manager.loginStatus("expires")).toBeNull()); - expect(authorized).not.toHaveBeenCalled(); - await expect(fs.stat(path.dirname(socket))).rejects.toMatchObject({ code: "ENOENT" }); + login: { expiresAt, onAuthorized: authorized, onFailure: failure } }); + const now = Date.now.bind(Date); + const clock = vi.spyOn(Date, "now").mockImplementation(() => now() + 60_000); + try { + await publishState(path.join(path.dirname(socket), "state"), { BackendState: "Running" }); + await manager.loginStatus("expires").catch(() => undefined); + expect(failure).toHaveBeenCalledOnce(); + await vi.waitFor(async () => expect(await manager.loginStatus("expires")).toBeNull(), { timeout: 5_000 }); + expect(authorized).not.toHaveBeenCalled(); + await expect(fs.stat(path.dirname(socket))).rejects.toMatchObject({ code: "ENOENT" }); + } finally { clock.mockRestore(); } }); it("lets secret deletion cancel a blocked credential save", async () => { @@ -100,7 +115,7 @@ if (args[1] === 'up') { login: { expiresAt: Date.now() + 60_000, onAuthorized: vi.fn(), onFailure: vi.fn() } }); const settled = connecting.catch(() => undefined); try { - await entered; + await Promise.race([entered, connecting.then(() => { throw new Error("Credential save did not block"); })]); await manager.disconnect("deleted-secret", { discardState: true }); await expect(connecting).rejects.toThrow("Tailscale"); expect(await manager.loginStatus("deleted-secret")).toBeNull();