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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import { http, HttpResponse } from "msw";
import { AgentServerClient } from "@openhands/typescript-client/clients";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import LLMSubscriptionService from "#/api/llm-subscription-service";
import {
  getActiveSelection,
  getRegisteredBackends,
  setActiveSelection,
  setRegisteredBackends,
} from "#/api/backend-registry/active-store";
import {
  OPENAI_SUBSCRIPTION_DEVICE_POLL_PATH,
  OPENAI_SUBSCRIPTION_DEVICE_START_PATH,
  OPENAI_SUBSCRIPTION_MODELS_PATH,
  OPENAI_SUBSCRIPTION_STATUS_PATH,
} from "#/constants/llm-subscription";
import { server } from "#/mocks/node";
import { resetTestHandlersMockSettings } from "#/mocks/settings-handlers";

describe("LLMSubscriptionService", () => {
  beforeEach(() => {
    resetTestHandlersMockSettings();
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it("uses an explicit GET request and closes the client after fetching models", async () => {
    const request = vi
      .spyOn(AgentServerClient.prototype, "request")
      .mockResolvedValue({ models: [] });
    const close = vi.spyOn(AgentServerClient.prototype, "close");

    await expect(LLMSubscriptionService.getOpenAIModels()).resolves.toEqual([]);

    expect(request).toHaveBeenCalledWith({
      method: "GET",
      path: OPENAI_SUBSCRIPTION_MODELS_PATH,
      body: undefined,
      responseType: "json",
    });
    expect(close).toHaveBeenCalledOnce();
  });

  it("fetches OpenAI subscription models from the agent-server endpoint", async () => {
    server.use(
      http.get(`*${OPENAI_SUBSCRIPTION_MODELS_PATH}`, ({ request }) => {
        expect(request.headers.get("Accept")).toBeNull();
        expect(request.headers.get("Content-Type")).toBe("application/json");
        expect(request.headers.get("X-Session-API-Key")).toBe(
          "test-session-key",
        );
        return HttpResponse.json({
          models: ["gpt-5.2", "gpt-5.3-codex"],
        });
      }),
    );

    await expect(LLMSubscriptionService.getOpenAIModels()).resolves.toEqual([
      "gpt-5.2",
      "gpt-5.3-codex",
    ]);
  });

  it("normalizes a top-level model list and excludes non-string entries", async () => {
    server.use(
      http.get(`*${OPENAI_SUBSCRIPTION_MODELS_PATH}`, () =>
        HttpResponse.json(["gpt-direct", 42, null]),
      ),
    );

    await expect(LLMSubscriptionService.getOpenAIModels()).resolves.toEqual([
      "gpt-direct",
    ]);
  });

  it("filters non-string entries from a nested model list", async () => {
    server.use(
      http.get(`*${OPENAI_SUBSCRIPTION_MODELS_PATH}`, () =>
        HttpResponse.json({ models: ["gpt-nested", 42, null] }),
      ),
    );

    await expect(LLMSubscriptionService.getOpenAIModels()).resolves.toEqual([
      "gpt-nested",
    ]);
  });

  it.each([
    ["null", null],
    ["a primitive string", "gpt-not-a-list"],
    ["a primitive number", 42],
    ["an object whose models field is not an array", { models: "gpt" }],
  ])("returns no models when the payload is %s", async (_label, payload) => {
    server.use(
      http.get(`*${OPENAI_SUBSCRIPTION_MODELS_PATH}`, () =>
        HttpResponse.json(payload),
      ),
    );

    await expect(LLMSubscriptionService.getOpenAIModels()).resolves.toEqual([]);
  });

  it("omits session authentication when the local backend has no API key", async () => {
    const originalBackends = getRegisteredBackends();
    const originalSelection = getActiveSelection();
    const backendWithoutKey = {
      id: "local-without-key",
      name: "Local without key",
      host: "http://localhost",
      apiKey: "",
      kind: "local" as const,
    };

    try {
      setRegisteredBackends([backendWithoutKey]);
      setActiveSelection({ backendId: backendWithoutKey.id });
      server.use(
        http.get(`*${OPENAI_SUBSCRIPTION_MODELS_PATH}`, ({ request }) => {
          expect(request.headers.has("X-Session-API-Key")).toBe(false);
          return HttpResponse.json([]);
        }),
      );

      await expect(LLMSubscriptionService.getOpenAIModels()).resolves.toEqual(
        [],
      );
    } finally {
      setRegisteredBackends(originalBackends);
      setActiveSelection(originalSelection);
    }
  });

  it("normalizes OpenAI subscription status from MSW handlers", async () => {
    await expect(LLMSubscriptionService.getOpenAIStatus()).resolves.toEqual({
      vendor: "openai",
      connected: false,
      accountEmail: null,
      expiresAt: null,
    });
  });

  it.each([
    {
      label: "primary snake-case fields",
      payload: {
        connected: true,
        account_email: " primary@example.com ",
        expires_at: " 2030-01-01T00:00:00Z ",
      },
      expected: {
        connected: true,
        accountEmail: "primary@example.com",
        expiresAt: "2030-01-01T00:00:00Z",
      },
    },
    {
      label: "secondary aliases",
      payload: {
        authenticated: true,
        email: " secondary@example.com ",
        expiresAt: " 2031-01-01T00:00:00Z ",
      },
      expected: {
        connected: true,
        accountEmail: "secondary@example.com",
        expiresAt: "2031-01-01T00:00:00Z",
      },
    },
    {
      label: "tertiary boolean and account aliases",
      payload: {
        is_connected: true,
        account: " tertiary@example.com ",
        expires_at: 101,
      },
      expected: {
        connected: true,
        accountEmail: "tertiary@example.com",
        expiresAt: 101,
      },
    },
    {
      label: "numeric camel-case expiry",
      payload: { connected: false, expiresAt: 202 },
      expected: {
        connected: false,
        accountEmail: null,
        expiresAt: 202,
      },
    },
    {
      label: "blank and invalid primary aliases",
      payload: {
        connected: "not-a-boolean",
        authenticated: false,
        account_email: "   ",
        email: " fallback@example.com ",
        expires_at: "   ",
        expiresAt: " fallback-expiry ",
      },
      expected: {
        connected: false,
        accountEmail: "fallback@example.com",
        expiresAt: "fallback-expiry",
      },
    },
  ])("normalizes $label", async ({ payload, expected }) => {
    server.use(
      http.get(`*${OPENAI_SUBSCRIPTION_STATUS_PATH}`, () =>
        HttpResponse.json(payload),
      ),
    );

    await expect(LLMSubscriptionService.getOpenAIStatus()).resolves.toEqual({
      vendor: "openai",
      ...expected,
    });
  });

  it("skips non-finite numeric status values in favor of later aliases", async () => {
    server.use(
      http.get(
        `*${OPENAI_SUBSCRIPTION_STATUS_PATH}`,
        () =>
          new HttpResponse(
            '{"connected":true,"expires_at":1e400,"expiresAt":303}',
            { headers: { "Content-Type": "application/json" } },
          ),
      ),
    );

    await expect(LLMSubscriptionService.getOpenAIStatus()).resolves.toEqual({
      vendor: "openai",
      connected: true,
      accountEmail: null,
      expiresAt: 303,
    });
  });

  it("defaults to disconnected when status flags are not booleans", async () => {
    server.use(
      http.get(`*${OPENAI_SUBSCRIPTION_STATUS_PATH}`, () =>
        HttpResponse.json({
          connected: "yes",
          authenticated: 1,
          is_connected: null,
        }),
      ),
    );

    await expect(LLMSubscriptionService.getOpenAIStatus()).resolves.toEqual({
      vendor: "openai",
      connected: false,
      accountEmail: null,
      expiresAt: null,
    });
  });

  it("normalizes device login challenge responses", async () => {
    await expect(
      LLMSubscriptionService.startOpenAIDeviceLogin(),
    ).resolves.toEqual({
      deviceCode: "mock-device-code",
      userCode: "MOCK-CODE",
      verificationUri: "https://auth.openai.com/activate",
      verificationUriComplete:
        "https://auth.openai.com/activate?user_code=MOCK-CODE",
      expiresAt: 900,
      intervalSeconds: 1,
    });
  });

  it.each([
    {
      label: "snake-case string fields",
      payload: {
        device_code: " device-snake ",
        user_code: " user-snake ",
        verification_uri: " https://verify.example/snake ",
        verification_uri_complete:
          " https://verify.example/snake?code=user-snake ",
        expires_at: " 2030-01-01T00:00:00Z ",
        interval: 1,
      },
      expected: {
        deviceCode: "device-snake",
        userCode: "user-snake",
        verificationUri: "https://verify.example/snake",
        verificationUriComplete: "https://verify.example/snake?code=user-snake",
        expiresAt: "2030-01-01T00:00:00Z",
        intervalSeconds: 1,
      },
    },
    {
      label: "camel-case fields after blank or invalid primary aliases",
      payload: {
        device_code: "   ",
        deviceCode: " device-camel ",
        user_code: 42,
        userCode: " user-camel ",
        verification_uri: "   ",
        verificationUri: " https://verify.example/camel ",
        verificationUriComplete:
          " https://verify.example/camel?code=user-camel ",
        expiresAt: " 2031-01-01T00:00:00Z ",
        interval_seconds: 2,
      },
      expected: {
        deviceCode: "device-camel",
        userCode: "user-camel",
        verificationUri: "https://verify.example/camel",
        verificationUriComplete: "https://verify.example/camel?code=user-camel",
        expiresAt: "2031-01-01T00:00:00Z",
        intervalSeconds: 2,
      },
    },
    {
      label: "snake-case URL aliases and numeric snake-case expiry",
      payload: {
        device_code: "device-url-snake",
        user_code: "user-url-snake",
        verification_url: "https://verify.example/url-snake",
        verification_url_complete:
          "https://verify.example/url-snake?code=user-url-snake",
        expires_at: 303,
        intervalSeconds: 3,
      },
      expected: {
        deviceCode: "device-url-snake",
        userCode: "user-url-snake",
        verificationUri: "https://verify.example/url-snake",
        verificationUriComplete:
          "https://verify.example/url-snake?code=user-url-snake",
        expiresAt: 303,
        intervalSeconds: 3,
      },
    },
    {
      label: "camel-case URL aliases and numeric camel-case expiry",
      payload: {
        device_code: "device-url-camel",
        user_code: "user-url-camel",
        verificationUrl: "https://verify.example/url-camel",
        verificationUrlComplete:
          "https://verify.example/url-camel?code=user-url-camel",
        expiresAt: 404,
      },
      expected: {
        deviceCode: "device-url-camel",
        userCode: "user-url-camel",
        verificationUri: "https://verify.example/url-camel",
        verificationUriComplete:
          "https://verify.example/url-camel?code=user-url-camel",
        expiresAt: 404,
        intervalSeconds: null,
      },
    },
    {
      label: "relative snake-case expiry",
      payload: {
        device_code: "device-expires-in",
        user_code: "user-expires-in",
        verification_uri: "https://verify.example/expires-in",
        expires_in: 505,
      },
      expected: {
        deviceCode: "device-expires-in",
        userCode: "user-expires-in",
        verificationUri: "https://verify.example/expires-in",
        verificationUriComplete: null,
        expiresAt: 505,
        intervalSeconds: null,
      },
    },
    {
      label: "relative camel-case expiry",
      payload: {
        device_code: "device-expires-camel",
        user_code: "user-expires-camel",
        verification_uri: "https://verify.example/expires-camel",
        expiresIn: 606,
      },
      expected: {
        deviceCode: "device-expires-camel",
        userCode: "user-expires-camel",
        verificationUri: "https://verify.example/expires-camel",
        verificationUriComplete: null,
        expiresAt: 606,
        intervalSeconds: null,
      },
    },
  ])("normalizes device challenge $label", async ({ payload, expected }) => {
    server.use(
      http.post(`*${OPENAI_SUBSCRIPTION_DEVICE_START_PATH}`, () =>
        HttpResponse.json(payload),
      ),
    );

    await expect(
      LLMSubscriptionService.startOpenAIDeviceLogin(),
    ).resolves.toEqual(expected);
  });

  it("skips non-finite numeric device values in favor of later aliases", async () => {
    server.use(
      http.post(
        `*${OPENAI_SUBSCRIPTION_DEVICE_START_PATH}`,
        () =>
          new HttpResponse(
            '{"device_code":"device","user_code":"user","verification_uri":"https://verify.example","expires_at":1e400,"expiresAt":707,"interval":1e400,"interval_seconds":7}',
            { headers: { "Content-Type": "application/json" } },
          ),
      ),
    );

    await expect(
      LLMSubscriptionService.startOpenAIDeviceLogin(),
    ).resolves.toEqual({
      deviceCode: "device",
      userCode: "user",
      verificationUri: "https://verify.example",
      verificationUriComplete: null,
      expiresAt: 707,
      intervalSeconds: 7,
    });
  });

  it("sends the exact polling body and JSON request headers", async () => {
    server.use(
      http.post(
        `*${OPENAI_SUBSCRIPTION_DEVICE_POLL_PATH}`,
        async ({ request }) => {
          expect(request.headers.get("Accept")).toBeNull();
          expect(request.headers.get("Content-Type")).toBe("application/json");
          expect(request.headers.get("X-Session-API-Key")).toBe(
            "test-session-key",
          );
          await expect(request.text()).resolves.toBe(
            '{"device_code":"exact-device-code"}',
          );
          return HttpResponse.json({ connected: true });
        },
      ),
    );

    await expect(
      LLMSubscriptionService.pollOpenAIDeviceLogin("exact-device-code"),
    ).resolves.toEqual({
      vendor: "openai",
      connected: true,
      accountEmail: null,
      expiresAt: null,
    });
  });

  it("posts the device code when polling login", async () => {
    await expect(
      LLMSubscriptionService.pollOpenAIDeviceLogin("mock-device-code"),
    ).resolves.toMatchObject({ connected: true });

    await expect(
      LLMSubscriptionService.getOpenAIStatus(),
    ).resolves.toMatchObject({
      connected: true,
      accountEmail: "mock-chatgpt@example.com",
    });
  });

  it("calls the logout endpoint", async () => {
    await LLMSubscriptionService.pollOpenAIDeviceLogin("mock-device-code");

    await expect(LLMSubscriptionService.logoutOpenAI()).resolves.toMatchObject({
      connected: false,
    });
    await expect(
      LLMSubscriptionService.getOpenAIStatus(),
    ).resolves.toMatchObject({ connected: false });
  });

  it.each([
    {
      label: "device code",
      payload: {
        device_code: "   ",
        user_code: "MOCK-CODE",
        verification_uri: "https://auth.openai.com/activate",
      },
    },
    {
      label: "user code",
      payload: {
        device_code: "mock-device-code",
        user_code: "   ",
        verification_uri: "https://auth.openai.com/activate",
      },
    },
    {
      label: "verification URI",
      payload: {
        device_code: "mock-device-code",
        user_code: "MOCK-CODE",
        verification_uri: "   ",
      },
    },
  ])("rejects a challenge without a valid $label", async ({ payload }) => {
    server.use(
      http.post(`*${OPENAI_SUBSCRIPTION_DEVICE_START_PATH}`, () =>
        HttpResponse.json(payload),
      ),
    );

    await expect(
      LLMSubscriptionService.startOpenAIDeviceLogin(),
    ).rejects.toThrowError(
      new Error("Subscription device login response is incomplete"),
    );
  });

  it("surfaces agent-server errors", async () => {
    server.use(
      http.get(`*${OPENAI_SUBSCRIPTION_STATUS_PATH}`, () =>
        HttpResponse.json({ detail: "unauthorized" }, { status: 401 }),
      ),
    );

    await expect(LLMSubscriptionService.getOpenAIStatus()).rejects.toThrowError(
      "HTTP request failed (401 Unauthorized)",
    );
  });
});