| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | |
| 4 | import { ProviderAuthResponse, ProviderAuthState } from "./providerSetup.ts"; |
| 5 | |
| 6 | const decodeResponse = Schema.decodeUnknownSync(ProviderAuthResponse); |
| 7 | const decodeState = Schema.decodeUnknownSync(ProviderAuthState); |
| 8 | |
| 9 | describe("provider credential responses", () => { |
| 10 | it("accepts the advertised field limit and rejects oversized or invalid fields", () => { |
| 11 | const values = Object.fromEntries( |
| 12 | Array.from({ length: 16 }, (_, i) => [`field_${i}`, "value"]), |
| 13 | ); |
| 14 | expect(decodeResponse({ type: "credentials", values })).toEqual({ |
| 15 | type: "credentials", |
| 16 | values, |
| 17 | }); |
| 18 | expect(() => |
| 19 | decodeResponse({ type: "credentials", values: { ...values, extra: "value" } }), |
| 20 | ).toThrow(); |
| 21 | expect(() => decodeResponse({ type: "credentials", values: { "": "value" } })).toThrow(); |
| 22 | expect(() => |
| 23 | decodeResponse({ type: "credentials", values: { token: "x".repeat(16_385) } }), |
| 24 | ).toThrow(); |
| 25 | }); |
| 26 | }); |
| 27 | |
| 28 | describe("provider auth state", () => { |
| 29 | it("drops auth variants from newer servers instead of rejecting the state", () => { |
| 30 | const method = { id: "browser", name: "Browser", description: null, type: "agent" }; |
| 31 | expect( |
| 32 | decodeState({ |
| 33 | instanceId: "cursor", |
| 34 | phase: "waiting", |
| 35 | flowId: null, |
| 36 | authorizationUrl: null, |
| 37 | expiresAt: null, |
| 38 | message: null, |
| 39 | methods: [method, { ...method, id: "passkey", type: "passkey" }], |
| 40 | interaction: { type: "passkey", id: "passkey" }, |
| 41 | credentialOwner: "keychain", |
| 42 | }), |
| 43 | ).toEqual({ |
| 44 | instanceId: "cursor", |
| 45 | phase: "waiting", |
| 46 | flowId: null, |
| 47 | authorizationUrl: null, |
| 48 | expiresAt: null, |
| 49 | message: null, |
| 50 | methods: [method], |
| 51 | }); |
| 52 | }); |
| 53 | }); |