|
| 1 | +import { beforeAll, describe, expect, it, vi } from "vitest" |
| 2 | + |
| 3 | +type ExtractAccessBlock = (body: string) => { |
| 4 | + validationRequired: boolean |
| 5 | + accountIneligible: boolean |
| 6 | + message?: string |
| 7 | + verifyUrl?: string |
| 8 | +} |
| 9 | + |
| 10 | +let extractAccessBlock: ExtractAccessBlock | undefined |
| 11 | +let buildProbeRequest: ((projectId: string) => Record<string, unknown>) | undefined |
| 12 | +let interpretProbeResponse: ((response: Response) => Promise<{ |
| 13 | + status: "ok" | "verification-required" | "ineligible" | "error" |
| 14 | + message: string |
| 15 | +}>) | undefined |
| 16 | + |
| 17 | +beforeAll(async () => { |
| 18 | + vi.mock("@opencode-ai/plugin", () => ({ tool: vi.fn() })) |
| 19 | + const { __testExports } = await import("../plugin") |
| 20 | + const exports = __testExports as { |
| 21 | + buildAccountAccessProbeRequest?: (projectId: string) => Record<string, unknown> |
| 22 | + extractAccountAccessErrorDetails?: ExtractAccessBlock |
| 23 | + interpretAccountAccessProbeResponse?: (response: Response) => Promise<{ |
| 24 | + status: "ok" | "verification-required" | "ineligible" | "error" |
| 25 | + message: string |
| 26 | + }> |
| 27 | + } |
| 28 | + buildProbeRequest = exports.buildAccountAccessProbeRequest |
| 29 | + extractAccessBlock = exports.extractAccountAccessErrorDetails |
| 30 | + interpretProbeResponse = exports.interpretAccountAccessProbeResponse |
| 31 | +}) |
| 32 | + |
| 33 | +describe("account eligibility recovery", () => { |
| 34 | + it("finishes a successful probe without waiting for an open SSE body", async () => { |
| 35 | + let cancelled = false |
| 36 | + const body = new ReadableStream({ |
| 37 | + pull(controller) { |
| 38 | + controller.enqueue(new TextEncoder().encode("data: still-open\n\n")) |
| 39 | + }, |
| 40 | + cancel() { |
| 41 | + cancelled = true |
| 42 | + }, |
| 43 | + }) |
| 44 | + |
| 45 | + await expect(interpretProbeResponse?.(new Response(body, { status: 200 }))).resolves.toMatchObject({ |
| 46 | + status: "ok", |
| 47 | + }) |
| 48 | + expect(cancelled).toBe(true) |
| 49 | + }) |
| 50 | + |
| 51 | + it("classifies ineligibility only on HTTP 403", async () => { |
| 52 | + const body = JSON.stringify({ error: { reason: "ACCOUNT_INELIGIBLE" } }) |
| 53 | + |
| 54 | + await expect(interpretProbeResponse?.(new Response(body, { status: 403 }))).resolves.toMatchObject({ |
| 55 | + status: "ineligible", |
| 56 | + }) |
| 57 | + await expect(interpretProbeResponse?.(new Response(body, { status: 500 }))).resolves.toMatchObject({ |
| 58 | + status: "error", |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + it("uses the current AGY request metadata contract for access probes", () => { |
| 63 | + const body = buildProbeRequest?.("project-a") as { |
| 64 | + project: string |
| 65 | + requestId: string |
| 66 | + model: string |
| 67 | + request: { |
| 68 | + sessionId: string |
| 69 | + labels: Record<string, string> |
| 70 | + contents: unknown[] |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + expect(body).toMatchObject({ |
| 75 | + project: "project-a", |
| 76 | + model: "gemini-3.5-flash-low", |
| 77 | + request: { |
| 78 | + sessionId: "-3750763034362895579", |
| 79 | + labels: { |
| 80 | + model_enum: "MODEL_PLACEHOLDER_M20", |
| 81 | + last_step_index: "1", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }) |
| 85 | + expect(body.requestId).toMatch(/^agent\/[0-9a-f-]+\/\d+\/[0-9a-f-]+\/2$/) |
| 86 | + }) |
| 87 | +}) |
| 88 | + |
| 89 | +describe("account ineligibility classification", () => { |
| 90 | + it("recognizes the exact structured ACCOUNT_INELIGIBLE reason", () => { |
| 91 | + const result = extractAccessBlock?.(JSON.stringify({ |
| 92 | + error: { |
| 93 | + code: 403, |
| 94 | + status: "PERMISSION_DENIED", |
| 95 | + message: "This account cannot use Antigravity.", |
| 96 | + details: [{ reason: "ACCOUNT_INELIGIBLE" }], |
| 97 | + }, |
| 98 | + })) |
| 99 | + |
| 100 | + expect(result).toMatchObject({ |
| 101 | + accountIneligible: true, |
| 102 | + validationRequired: false, |
| 103 | + message: "This account cannot use Antigravity.", |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + it("recognizes ACCOUNT_INELIGIBLE inside an SSE error frame", () => { |
| 108 | + const result = extractAccessBlock?.( |
| 109 | + 'data: {"error":{"message":"Not eligible","metadata":{"reason":"ACCOUNT_INELIGIBLE"}}}\n\n', |
| 110 | + ) |
| 111 | + |
| 112 | + expect(result?.accountIneligible).toBe(true) |
| 113 | + expect(result?.message).toBe("Not eligible") |
| 114 | + }) |
| 115 | + |
| 116 | + it("does not disable accounts for generic access-denied text", () => { |
| 117 | + for (const message of [ |
| 118 | + "Access denied", |
| 119 | + "Permission denied", |
| 120 | + "Your account is not eligible for this feature", |
| 121 | + "An upstream service denied access", |
| 122 | + "ACCOUNT_INELIGIBLE_TEMPORARY", |
| 123 | + ]) { |
| 124 | + const result = extractAccessBlock?.(JSON.stringify({ error: { code: 403, message } })) |
| 125 | + expect(result?.accountIneligible, message).toBe(false) |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + it("keeps VALIDATION_REQUIRED separate from account ineligibility", () => { |
| 130 | + const result = extractAccessBlock?.(JSON.stringify({ |
| 131 | + error: { |
| 132 | + code: 403, |
| 133 | + message: "Verify your account", |
| 134 | + details: [{ reason: "VALIDATION_REQUIRED" }], |
| 135 | + }, |
| 136 | + })) |
| 137 | + |
| 138 | + expect(result?.validationRequired).toBe(true) |
| 139 | + expect(result?.accountIneligible).toBe(false) |
| 140 | + }) |
| 141 | +}) |
0 commit comments