|
| 1 | +import { describe, expect, it, vi } from "bun:test"; |
| 2 | +import { loginUmans } from "@oh-my-pi/pi-ai/registry/umans"; |
| 3 | +import type { FetchImpl } from "@oh-my-pi/pi-ai/types"; |
| 4 | + |
| 5 | +describe("umans login", () => { |
| 6 | + it("validates pasted keys against the Anthropic messages endpoint", async () => { |
| 7 | + let authUrl: string | undefined; |
| 8 | + let authInstructions: string | undefined; |
| 9 | + let promptMessage: string | undefined; |
| 10 | + let promptPlaceholder: string | undefined; |
| 11 | + const fetchMock: FetchImpl = vi.fn(async (input: string | URL | Request, init?: RequestInit) => { |
| 12 | + const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; |
| 13 | + const headers = new Headers(init?.headers); |
| 14 | + const body = JSON.parse(String(init?.body)) as { model?: string; max_tokens?: number }; |
| 15 | + |
| 16 | + expect(url).toBe("https://api.code.umans.ai/v1/messages"); |
| 17 | + expect(init?.method).toBe("POST"); |
| 18 | + expect(headers.get("content-type")).toBe("application/json"); |
| 19 | + expect(headers.get("anthropic-version")).toBe("2023-06-01"); |
| 20 | + expect(headers.get("x-api-key")).toBe("sk-umans-valid"); |
| 21 | + expect(headers.get("authorization")).toBeNull(); |
| 22 | + expect(body.model).toBe("umans-coder"); |
| 23 | + expect(body.max_tokens).toBe(1); |
| 24 | + |
| 25 | + return new Response(JSON.stringify({ id: "msg_test", type: "message" }), { |
| 26 | + status: 200, |
| 27 | + headers: { "Content-Type": "application/json" }, |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + const apiKey = await loginUmans({ |
| 32 | + onAuth: info => { |
| 33 | + authUrl = info.url; |
| 34 | + authInstructions = info.instructions; |
| 35 | + }, |
| 36 | + onPrompt: async prompt => { |
| 37 | + promptMessage = prompt.message; |
| 38 | + promptPlaceholder = prompt.placeholder; |
| 39 | + return " sk-umans-valid "; |
| 40 | + }, |
| 41 | + fetch: fetchMock, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(apiKey).toBe("sk-umans-valid"); |
| 45 | + expect(authUrl).toBe("https://app.umans.ai/billing"); |
| 46 | + expect(authInstructions).toContain("Dashboard → API Keys"); |
| 47 | + expect(promptMessage).toBe("Paste your Umans API key"); |
| 48 | + expect(promptPlaceholder).toBe("sk-..."); |
| 49 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 50 | + }); |
| 51 | + |
| 52 | + it("surfaces validation errors from the Anthropic messages endpoint", async () => { |
| 53 | + const fetchMock: FetchImpl = vi.fn( |
| 54 | + async () => |
| 55 | + new Response("invalid key", { |
| 56 | + status: 401, |
| 57 | + headers: { "Content-Type": "text/plain" }, |
| 58 | + }), |
| 59 | + ); |
| 60 | + |
| 61 | + await expect( |
| 62 | + loginUmans({ |
| 63 | + onPrompt: async () => "sk-umans-bad", |
| 64 | + fetch: fetchMock, |
| 65 | + }), |
| 66 | + ).rejects.toThrow("Umans AI Coding Plan API key validation failed (401): invalid key"); |
| 67 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 68 | + }); |
| 69 | +}); |
0 commit comments