|
| 1 | +import { test, expect } from "bun:test" |
| 2 | +import { Auth } from "../../src/auth" |
| 3 | + |
| 4 | +test("set normalizes trailing slashes in keys", async () => { |
| 5 | + await Auth.set("https://example.com/", { |
| 6 | + type: "wellknown", |
| 7 | + key: "TOKEN", |
| 8 | + token: "abc", |
| 9 | + }) |
| 10 | + const data = await Auth.all() |
| 11 | + expect(data["https://example.com"]).toBeDefined() |
| 12 | + expect(data["https://example.com/"]).toBeUndefined() |
| 13 | +}) |
| 14 | + |
| 15 | +test("set cleans up pre-existing trailing-slash entry", async () => { |
| 16 | + // Simulate a pre-fix entry with trailing slash |
| 17 | + await Auth.set("https://example.com/", { |
| 18 | + type: "wellknown", |
| 19 | + key: "TOKEN", |
| 20 | + token: "old", |
| 21 | + }) |
| 22 | + // Re-login with normalized key (as the CLI does post-fix) |
| 23 | + await Auth.set("https://example.com", { |
| 24 | + type: "wellknown", |
| 25 | + key: "TOKEN", |
| 26 | + token: "new", |
| 27 | + }) |
| 28 | + const data = await Auth.all() |
| 29 | + const keys = Object.keys(data).filter((k) => k.includes("example.com")) |
| 30 | + expect(keys).toEqual(["https://example.com"]) |
| 31 | + const entry = data["https://example.com"]! |
| 32 | + expect(entry.type).toBe("wellknown") |
| 33 | + if (entry.type === "wellknown") expect(entry.token).toBe("new") |
| 34 | +}) |
| 35 | + |
| 36 | +test("remove deletes both trailing-slash and normalized keys", async () => { |
| 37 | + await Auth.set("https://example.com", { |
| 38 | + type: "wellknown", |
| 39 | + key: "TOKEN", |
| 40 | + token: "abc", |
| 41 | + }) |
| 42 | + await Auth.remove("https://example.com/") |
| 43 | + const data = await Auth.all() |
| 44 | + expect(data["https://example.com"]).toBeUndefined() |
| 45 | + expect(data["https://example.com/"]).toBeUndefined() |
| 46 | +}) |
| 47 | + |
| 48 | +test("set and remove are no-ops on keys without trailing slashes", async () => { |
| 49 | + await Auth.set("anthropic", { |
| 50 | + type: "api", |
| 51 | + key: "sk-test", |
| 52 | + }) |
| 53 | + const data = await Auth.all() |
| 54 | + expect(data["anthropic"]).toBeDefined() |
| 55 | + await Auth.remove("anthropic") |
| 56 | + const after = await Auth.all() |
| 57 | + expect(after["anthropic"]).toBeUndefined() |
| 58 | +}) |
0 commit comments