Spaces:
Paused
Paused
File size: 2,829 Bytes
488aedf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | /**
* Tests for credential types and auth manager.
*/
import { describe, it, expect } from "bun:test";
import { credentialString, isExpired } from "./types.js";
import { AuthManager } from "./manager.js";
describe("credentialString", () => {
it("returns apiKey.secret when secret present", () => {
expect(credentialString({ apiKey: "a", secret: "b", provider: "zai" })).toBe("a.b");
});
it("returns apiKey only when secret absent", () => {
expect(credentialString({ apiKey: "abc", provider: "bigmodel" })).toBe("abc");
});
});
describe("isExpired", () => {
it("returns false when expiresAt is undefined", () => {
expect(isExpired({ apiKey: "x", provider: "zai" })).toBe(false);
});
it("returns true when past expiry", () => {
const cred = { apiKey: "x", provider: "zai" as const, expiresAt: 1000 };
expect(isExpired(cred, 2000)).toBe(true);
});
it("returns false when before expiry", () => {
const cred = { apiKey: "x", provider: "zai" as const, expiresAt: 3000 };
expect(isExpired(cred, 2000)).toBe(false);
});
});
describe("AuthManager", () => {
it("throws without a credential", async () => {
const mgr = new AuthManager();
expect(mgr.getCredential()).rejects.toThrow(/not available/);
});
it("returns the credential set via setOAuthCredential", async () => {
const mgr = new AuthManager();
mgr.setOAuthCredential({ apiKey: "oa", secret: "sc", provider: "zai" });
const cred = await mgr.getCredential();
expect(cred.apiKey).toBe("oa");
expect(cred.secret).toBe("sc");
});
it("returns the latest credential after re-set", async () => {
const mgr = new AuthManager();
mgr.setOAuthCredential({ apiKey: "old", provider: "zai" });
mgr.setOAuthCredential({ apiKey: "new", provider: "zai" });
const cred = await mgr.getCredential();
expect(cred.apiKey).toBe("new");
});
it("throws on an expired credential and clears it", async () => {
const mgr = new AuthManager();
mgr.setOAuthCredential({ apiKey: "x", provider: "zai", expiresAt: 1000 });
await expect(mgr.getCredential()).rejects.toThrow(/expired/);
await expect(mgr.getCredential()).rejects.toThrow(/not available/);
});
it("drops the credential on clearOAuthCredential", async () => {
const mgr = new AuthManager();
mgr.setOAuthCredential({ apiKey: "oa", provider: "zai" });
mgr.clearOAuthCredential();
await expect(mgr.getCredential()).rejects.toThrow(/not available/);
});
it("accepts a fresh credential after clearOAuthCredential", async () => {
const mgr = new AuthManager();
mgr.setOAuthCredential({ apiKey: "old", provider: "zai" });
mgr.clearOAuthCredential();
mgr.setOAuthCredential({ apiKey: "new", provider: "zai" });
expect((await mgr.getCredential()).apiKey).toBe("new");
});
});
|