Files
llmwiki-cli/test/auth.test.ts
doum1004 58a1261610 Phase 5: GitHub Auth + Repo Management
Add GitHub integration for backup and multi-device sync:
- wiki auth login/status/logout with PAT-based authentication
- wiki repo list/create/clone/connect for GitHub repo management
- wiki push/pull/sync for remote operations
- GitHub API client and token storage
- 6 new tests (93 total)

All 5 phases complete.
2026-04-10 02:21:04 -04:00

74 lines
1.9 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "bun:test";
import { mkdtemp, rm } from "fs/promises";
import { join } from "path";
import { tmpdir } from "os";
import {
loadAuth,
saveAuth,
clearAuth,
isAuthenticated,
} from "../src/lib/auth.ts";
let configDir: string;
const origConfigDir = process.env.LLMWIKI_CONFIG_DIR;
beforeEach(async () => {
configDir = await mkdtemp(join(tmpdir(), "llmwiki-auth-"));
process.env.LLMWIKI_CONFIG_DIR = configDir;
});
afterEach(async () => {
await rm(configDir, { recursive: true, force: true });
if (origConfigDir) {
process.env.LLMWIKI_CONFIG_DIR = origConfigDir;
} else {
delete process.env.LLMWIKI_CONFIG_DIR;
}
});
describe("auth", () => {
it("loadAuth returns null when not authenticated", async () => {
const auth = await loadAuth();
expect(auth).toBeNull();
});
it("isAuthenticated returns false when not authenticated", async () => {
expect(await isAuthenticated()).toBe(false);
});
it("saveAuth and loadAuth roundtrip", async () => {
const authData = {
token: "ghp_test123",
username: "testuser",
created: "2026-01-01T00:00:00.000Z",
};
await saveAuth(authData);
const loaded = await loadAuth();
expect(loaded).toEqual(authData);
});
it("isAuthenticated returns true after saveAuth", async () => {
await saveAuth({
token: "ghp_test",
username: "user",
created: new Date().toISOString(),
});
expect(await isAuthenticated()).toBe(true);
});
it("clearAuth removes credentials", async () => {
await saveAuth({
token: "ghp_test",
username: "user",
created: new Date().toISOString(),
});
await clearAuth();
expect(await isAuthenticated()).toBe(false);
});
it("clearAuth on empty config does not throw", async () => {
await clearAuth(); // should not throw
expect(await isAuthenticated()).toBe(false);
});
});