|
| 1 | +import { describe, test, expect, beforeEach, afterEach } from "bun:test"; |
| 2 | +import { startDeviceAuth } from "../src/auth/device-auth"; |
| 3 | + |
| 4 | +let originalFetch: typeof globalThis.fetch; |
| 5 | + |
| 6 | +beforeEach(() => { |
| 7 | + originalFetch = globalThis.fetch; |
| 8 | +}); |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + globalThis.fetch = originalFetch; |
| 12 | +}); |
| 13 | + |
| 14 | +function stubFetch(response: unknown, { ok = true, status = 200 } = {}): void { |
| 15 | + const stub: typeof fetch = async () => |
| 16 | + new Response(JSON.stringify(response), { |
| 17 | + status, |
| 18 | + headers: { "Content-Type": "application/json" }, |
| 19 | + }); |
| 20 | + // ok is derived from status in Response, so we rely on status here. |
| 21 | + // Allow callers to force ok=false by passing status>=400. |
| 22 | + void ok; |
| 23 | + globalThis.fetch = stub; |
| 24 | +} |
| 25 | + |
| 26 | +describe("startDeviceAuth", () => { |
| 27 | + test("constructs the openclaw-advisor verification URL from the returned code, ignoring the server-returned verificationUrl", async () => { |
| 28 | + // Server returns a legacy /device-auth?code=... URL — plugin should ignore it. |
| 29 | + stubFetch({ |
| 30 | + code: "ABCD-1234", |
| 31 | + verificationUrl: "https://app.kilo.ai/device-auth?code=ABCD-1234", |
| 32 | + expiresIn: 600, |
| 33 | + }); |
| 34 | + |
| 35 | + const result = await startDeviceAuth("https://app.kilo.ai"); |
| 36 | + |
| 37 | + expect(result.kind).toBe("started"); |
| 38 | + expect(result.code).toBe("ABCD-1234"); |
| 39 | + expect(result.verificationUrl).toBe( |
| 40 | + "https://app.kilo.ai/openclaw-advisor?code=ABCD-1234", |
| 41 | + ); |
| 42 | + expect(result.expiresIn).toBe(600); |
| 43 | + }); |
| 44 | + |
| 45 | + test("uses the caller-provided apiBase for the verification URL (dev loop)", async () => { |
| 46 | + stubFetch({ |
| 47 | + code: "WXYZ-5678", |
| 48 | + verificationUrl: |
| 49 | + "http://host.docker.internal:3000/device-auth?code=WXYZ-5678", |
| 50 | + expiresIn: 600, |
| 51 | + }); |
| 52 | + |
| 53 | + const result = await startDeviceAuth("http://host.docker.internal:3000"); |
| 54 | + |
| 55 | + expect(result.verificationUrl).toBe( |
| 56 | + "http://host.docker.internal:3000/openclaw-advisor?code=WXYZ-5678", |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + test("url-encodes the code to defend against unexpected server responses", async () => { |
| 61 | + // Defense-in-depth: even if the server returned a malformed code, we must |
| 62 | + // not inject unescaped query chars into the verification URL. |
| 63 | + stubFetch({ |
| 64 | + code: "A&B=C D", |
| 65 | + verificationUrl: "ignored", |
| 66 | + expiresIn: 600, |
| 67 | + }); |
| 68 | + |
| 69 | + const result = await startDeviceAuth("https://app.kilo.ai"); |
| 70 | + |
| 71 | + expect(result.verificationUrl).toBe( |
| 72 | + "https://app.kilo.ai/openclaw-advisor?code=A%26B%3DC%20D", |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + test("throws a descriptive error when the server rejects the request", async () => { |
| 77 | + stubFetch({}, { status: 500 }); |
| 78 | + |
| 79 | + await expect(startDeviceAuth("https://app.kilo.ai")).rejects.toThrow( |
| 80 | + /HTTP 500/, |
| 81 | + ); |
| 82 | + }); |
| 83 | +}); |
0 commit comments