|
1 | 1 | import { afterEach, beforeEach, describe, expect, mock, setDefaultTimeout, test } from "bun:test"; |
2 | 2 | import { logsFromApiBody } from "./helpers/logs-api"; |
3 | 3 | import { managementFetch as fetch, ManagementRequest as Request } from "./helpers/management-auth"; |
4 | | -import { mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 4 | +import { mkdtempSync, readFileSync } from "node:fs"; |
5 | 5 | import { tmpdir } from "node:os"; |
6 | 6 | import { join } from "node:path"; |
7 | 7 | import { |
@@ -29,6 +29,13 @@ import { |
29 | 29 | import { startServer } from "../src/server"; |
30 | 30 | import { fakeChatGptJwt } from "./helpers/fake-chatgpt-jwt"; |
31 | 31 | import { catalogConvergenceFactory } from "./helpers/catalog-convergence"; |
| 32 | +import { removeTreeWithRetry } from "./helpers/remove-tree"; |
| 33 | +import { |
| 34 | + clearResponseStateForTests, |
| 35 | + flushResponseState, |
| 36 | + responseStatePersistPendingForTests, |
| 37 | +} from "../src/responses/state"; |
| 38 | +import { clearCursorThreadContinuityForTests } from "../src/adapters/cursor/thread-continuity"; |
32 | 39 |
|
33 | 40 | // Full-suite Windows load: startServer + combo rename/delete management flows exceed the |
34 | 41 | // default 5s per-test budget (same flake class as 810fa115 / claude-management-api). |
@@ -139,19 +146,25 @@ beforeEach(() => { |
139 | 146 | customUsageEstimate = undefined; |
140 | 147 | customCursorTransportFactory = undefined; |
141 | 148 | clearRequestLogsForTests(); |
| 149 | + clearResponseStateForTests(); |
| 150 | + clearCursorThreadContinuityForTests(); |
142 | 151 | }); |
143 | 152 |
|
144 | 153 | afterEach(async () => { |
| 154 | + for (const server of servers.splice(0)) await server.stop(true); |
| 155 | + await flushResponseState(); |
| 156 | + expect(responseStatePersistPendingForTests()).toBe(false); |
| 157 | + clearResponseStateForTests(); |
| 158 | + clearCursorThreadContinuityForTests(); |
145 | 159 | globalThis.fetch = originalFetch; |
146 | 160 | Date.now = originalNow; |
147 | | - for (const server of servers.splice(0)) await server.stop(true); |
148 | 161 | if (previousHome === undefined) delete process.env.OPENCODEX_HOME; |
149 | 162 | else process.env.OPENCODEX_HOME = previousHome; |
150 | 163 | if (previousCursorToken === undefined) delete process.env.OPENCODEX_CURSOR_TEST_TOKEN; |
151 | 164 | else process.env.OPENCODEX_CURSOR_TEST_TOKEN = previousCursorToken; |
152 | 165 | isolatedCodexHome?.restore(); |
153 | 166 | isolatedCodexHome = null; |
154 | | - if (testDir) rmSync(testDir, { recursive: true, force: true }); |
| 167 | + if (testDir) removeTreeWithRetry(testDir); |
155 | 168 | clearComboSelectionState(); |
156 | 169 | clearComboTargetCooldowns(); |
157 | 170 | clearCodexUpstreamHealth(); |
@@ -1980,6 +1993,53 @@ describe("server combo failover 030 activation matrix", () => { |
1980 | 1993 | expect(seen).toEqual([undefined, "kiro-oauth-conversation", undefined, undefined]); |
1981 | 1994 | }); |
1982 | 1995 |
|
| 1996 | + test("OAuth continuation owners distinguish exact stored account ids", async () => { |
| 1997 | + const seen: Array<string | undefined> = []; |
| 1998 | + customRunTurn = async (parsed, _incoming, emit) => { |
| 1999 | + const conversationId = parsed._providerContinuation?.kiro?.conversationId; |
| 2000 | + seen.push(conversationId); |
| 2001 | + emit({ type: "text_delta", text: "continued" }); |
| 2002 | + emit({ |
| 2003 | + type: "done", |
| 2004 | + providerState: { kiro: { conversationId: conversationId ?? "exact-oauth-subject" } }, |
| 2005 | + }); |
| 2006 | + }; |
| 2007 | + const config = comboConfig({ |
| 2008 | + xai: provider("openai-chat", "https://api.x.ai/v1", "unused", { |
| 2009 | + authMode: "oauth", |
| 2010 | + note: "test-kiro-oauth", |
| 2011 | + }), |
| 2012 | + }, [{ provider: "xai", model: "m1" }]); |
| 2013 | + |
| 2014 | + await saveCredential("xai", { |
| 2015 | + access: "exact-subject-access-one", |
| 2016 | + refresh: "exact-subject-refresh-one", |
| 2017 | + expires: Date.now() + 3_600_000, |
| 2018 | + accountId: "acct-exact-subject", |
| 2019 | + source: "oauth", |
| 2020 | + }); |
| 2021 | + const first = await post(config, { store: false, input: "first" }); |
| 2022 | + expect(first.status).toBe(200); |
| 2023 | + const firstJson = await first.json() as { id: string }; |
| 2024 | + |
| 2025 | + await saveCredential("xai", { |
| 2026 | + access: "exact-subject-access-two", |
| 2027 | + refresh: "exact-subject-refresh-two", |
| 2028 | + expires: Date.now() + 3_600_000, |
| 2029 | + accountId: " acct-exact-subject ", |
| 2030 | + source: "oauth", |
| 2031 | + }); |
| 2032 | + expect(getAccountSet("xai")?.accounts).toHaveLength(2); |
| 2033 | + const second = await post(config, { |
| 2034 | + store: false, |
| 2035 | + previous_response_id: firstJson.id, |
| 2036 | + input: "second", |
| 2037 | + }); |
| 2038 | + |
| 2039 | + expect(second.status).toBe(200); |
| 2040 | + expect(seen).toEqual([undefined, undefined]); |
| 2041 | + }); |
| 2042 | + |
1983 | 2043 | test("OAuth credentials without immutable account ids do not inherit provider continuation state", async () => { |
1984 | 2044 | const seen: Array<string | undefined> = []; |
1985 | 2045 | const { |
@@ -2649,10 +2709,19 @@ describe("cursor conversation continuity across store:false chains", () => { |
2649 | 2709 | }); |
2650 | 2710 | } |
2651 | 2711 |
|
2652 | | - async function postCursor(config: OcxConfig, raw: Record<string, unknown>): Promise<Response> { |
| 2712 | + async function postCursor( |
| 2713 | + config: OcxConfig, |
| 2714 | + raw: Record<string, unknown>, |
| 2715 | + clientThreadId?: string, |
| 2716 | + bearerToken?: string, |
| 2717 | + ): Promise<Response> { |
2653 | 2718 | return handleResponses(new Request("http://localhost/v1/responses", { |
2654 | 2719 | method: "POST", |
2655 | | - headers: { "content-type": "application/json" }, |
| 2720 | + headers: { |
| 2721 | + "content-type": "application/json", |
| 2722 | + ...(clientThreadId ? { "x-codex-parent-thread-id": clientThreadId } : {}), |
| 2723 | + ...(bearerToken ? { authorization: `Bearer ${bearerToken}` } : {}), |
| 2724 | + }, |
2656 | 2725 | body: JSON.stringify({ stream: false, store: false, ...raw }), |
2657 | 2726 | }), config, { model: "", provider: "" }, {}); |
2658 | 2727 | } |
@@ -2718,6 +2787,102 @@ describe("cursor conversation continuity across store:false chains", () => { |
2718 | 2787 | expect(seen[1]).toBe(seen[0]); |
2719 | 2788 | }); |
2720 | 2789 |
|
| 2790 | + test("owner mismatch rekeys Cursor parent-thread continuity to the new route", async () => { |
| 2791 | + const seen: string[] = []; |
| 2792 | + customCursorTransportFactory = fakeCursorTransportFactory(seen); |
| 2793 | + const config = cursorConfig(); |
| 2794 | + const threadId = "cursor-route-owner-thread"; |
| 2795 | + |
| 2796 | + const first = await postCursor(config, { |
| 2797 | + model: "cursortest/composer-2", |
| 2798 | + input: "first", |
| 2799 | + }, threadId); |
| 2800 | + expect(first.status).toBe(200); |
| 2801 | + const firstJson = await first.json() as { id: string }; |
| 2802 | + |
| 2803 | + config.providers.cursortest!.baseUrl = "https://cursor-other.example/v1"; |
| 2804 | + const second = await postCursor(config, { |
| 2805 | + model: "cursortest/composer-2", |
| 2806 | + previous_response_id: firstJson.id, |
| 2807 | + input: "second", |
| 2808 | + }, threadId); |
| 2809 | + expect(second.status).toBe(200); |
| 2810 | + |
| 2811 | + const third = await postCursor(config, { |
| 2812 | + model: "cursortest/composer-2", |
| 2813 | + input: [ |
| 2814 | + { role: "user", content: "first" }, |
| 2815 | + { role: "assistant", content: "cursor ok" }, |
| 2816 | + { role: "user", content: "third" }, |
| 2817 | + ], |
| 2818 | + }, threadId); |
| 2819 | + expect(third.status).toBe(200); |
| 2820 | + |
| 2821 | + expect(seen).toHaveLength(3); |
| 2822 | + expect(seen[1]).not.toBe(seen[0]); |
| 2823 | + expect(seen[2]).toBe(seen[1]); |
| 2824 | + }); |
| 2825 | + |
| 2826 | + test("ownerless local Cursor parent-thread continuity is scoped by route and bearer token", async () => { |
| 2827 | + const seen: string[] = []; |
| 2828 | + customCursorTransportFactory = fakeCursorTransportFactory(seen); |
| 2829 | + const config = cursorConfig(); |
| 2830 | + config.providers.cursortest = { |
| 2831 | + ...config.providers.cursortest!, |
| 2832 | + authMode: "local", |
| 2833 | + }; |
| 2834 | + delete config.providers.cursortest.apiKey; |
| 2835 | + const threadId = "cursor-ownerless-token-thread"; |
| 2836 | + |
| 2837 | + const first = await postCursor(config, { |
| 2838 | + model: "cursortest/composer-2", |
| 2839 | + input: "first", |
| 2840 | + }, threadId, "cursor-token-a"); |
| 2841 | + expect(first.status).toBe(200); |
| 2842 | + |
| 2843 | + config.providers.cursortest!.baseUrl = "https://cursor-local-other.example/v1"; |
| 2844 | + const second = await postCursor(config, { |
| 2845 | + model: "cursortest/composer-2", |
| 2846 | + input: [ |
| 2847 | + { role: "user", content: "first" }, |
| 2848 | + { role: "assistant", content: "cursor ok" }, |
| 2849 | + { role: "user", content: "second" }, |
| 2850 | + ], |
| 2851 | + }, threadId, "cursor-token-a"); |
| 2852 | + expect(second.status).toBe(200); |
| 2853 | + |
| 2854 | + const third = await postCursor(config, { |
| 2855 | + model: "cursortest/composer-2", |
| 2856 | + input: [ |
| 2857 | + { role: "user", content: "first" }, |
| 2858 | + { role: "assistant", content: "cursor ok" }, |
| 2859 | + { role: "user", content: "second" }, |
| 2860 | + { role: "assistant", content: "cursor ok" }, |
| 2861 | + { role: "user", content: "third" }, |
| 2862 | + ], |
| 2863 | + }, threadId, "cursor-token-b"); |
| 2864 | + expect(third.status).toBe(200); |
| 2865 | + |
| 2866 | + const fourth = await postCursor(config, { |
| 2867 | + model: "cursortest/composer-2", |
| 2868 | + input: [ |
| 2869 | + { role: "user", content: "first" }, |
| 2870 | + { role: "assistant", content: "cursor ok" }, |
| 2871 | + { role: "user", content: "second" }, |
| 2872 | + { role: "assistant", content: "cursor ok" }, |
| 2873 | + { role: "user", content: "third" }, |
| 2874 | + { role: "assistant", content: "cursor ok" }, |
| 2875 | + { role: "user", content: "fourth" }, |
| 2876 | + ], |
| 2877 | + }, threadId, "cursor-token-b"); |
| 2878 | + expect(fourth.status).toBe(200); |
| 2879 | + |
| 2880 | + expect(seen).toHaveLength(4); |
| 2881 | + expect(seen[1]).not.toBe(seen[0]); |
| 2882 | + expect(seen[2]).not.toBe(seen[1]); |
| 2883 | + expect(seen[3]).toBe(seen[2]); |
| 2884 | + }); |
| 2885 | + |
2721 | 2886 | test("external-model toolResult continuation preserves and persists the same id", async () => { |
2722 | 2887 | const seen: string[] = []; |
2723 | 2888 | customCursorTransportFactory = fakeCursorTransportFactory(seen); |
|
0 commit comments