Files
Docwell/Packages/api-client/src/index.test.ts
T
tajniak81andClaude Opus 5.5 ba394b8fac Finish Phase 0: lint, codegen, contributor docs and the two remaining spikes
Repository basics:
- CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI
- golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI

API codegen from api/openapi.yaml (npm run generate, drift fails CI):
- Go types via oapi-codegen in Server/internal/api
- a contract test that the router and the spec list the same routes
- Dart models via Phone-App/tool/generate_api.dart; the app uses them

Spike: shared Tiptap editor in a Flutter WebView (ADR-4)
- @docwell/editor-bridge builds the editor into one HTML page with a
  versioned JSON protocol; Phone-App hosts it with webview_flutter and a
  native toolbar. Integration test passes on an Android API 35 emulator.
- flutter_inappwebview dropped: its Android plugin fails on AGP 9.

Spike: Wails v3 desktop (ADR-11)
- system tray, docwell:// deep links (page and space only) with
  single-instance hand-off, and the built-in updater with a required
  ed25519 signing key. Verified on Windows.
- regenerated build assets register the scheme and drop template
  placeholders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:11:07 +01:00

88 lines
3.8 KiB
TypeScript

// SPDX-License-Identifier: AGPL-3.0-or-later
import { describe, expect, it, vi } from "vitest";
import { ApiError, createClient, isManager } from "./index";
function jsonResponse(status: number, body: unknown) {
return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
}
describe("createClient", () => {
it("sends the token and parses JSON", async () => {
const fetch = vi.fn(async () => jsonResponse(200, { name: "docwell-server", version: "1.2.3" }));
const client = createClient({ baseUrl: "https://api.docwell.test/", token: () => "abc", fetch });
await expect(client.version()).resolves.toEqual({ name: "docwell-server", version: "1.2.3" });
const [url, init] = fetch.mock.calls[0] as unknown as [string, RequestInit];
expect(url).toBe("https://api.docwell.test/api/v1/version");
expect((init.headers as Record<string, string>).Authorization).toBe("Bearer abc");
});
it("throws ApiError on non-2xx", async () => {
const fetch = vi.fn(async () => jsonResponse(503, { status: "unavailable" }));
const client = createClient({ baseUrl: "", fetch });
await expect(client.readiness()).rejects.toBeInstanceOf(ApiError);
});
it("exposes error code, fields and mfaId", async () => {
const fetch = vi.fn(async () => jsonResponse(400, { error: "validation", fields: { email: "taken" } }));
const err = await createClient({ baseUrl: "", fetch })
.auth.signup({ email: "a@b.c", password: "x", name: "A" })
.catch((e) => e);
expect(err).toBeInstanceOf(ApiError);
expect(err.code).toBe("validation");
expect(err.fields).toEqual({ email: "taken" });
const mfa = await createClient({
baseUrl: "",
fetch: vi.fn(async () => jsonResponse(401, { error: "mfa_required", mfaId: "m1" })),
})
.auth.login("a@b.c", "pw")
.catch((e) => e);
expect(mfa.mfaId).toBe("m1");
});
it("reports a rejected token", async () => {
const onUnauthorized = vi.fn();
const fetch = vi.fn(async () => jsonResponse(401, { error: "unauthorized" }));
const client = createClient({ baseUrl: "", fetch, token: () => "old", onUnauthorized });
await expect(client.auth.me()).rejects.toBeInstanceOf(ApiError);
expect(onUnauthorized).toHaveBeenCalledOnce();
});
it("builds the user list query", async () => {
const fetch = vi.fn(async () => jsonResponse(200, { items: [], page: 1, perPage: 20, totalItems: 0 }));
await createClient({ baseUrl: "", fetch }).users.list({ page: 2, q: "ann", organization: "" });
expect((fetch.mock.calls[0] as unknown as [string])[0]).toBe("/api/v1/users?page=2&q=ann");
});
it("sends workspace membership changes", async () => {
const fetch = vi.fn(async () =>
jsonResponse(200, { user: { id: "u", email: "a@b.c", name: "A" }, role: "member" }),
);
await createClient({ baseUrl: "", fetch }).workspaces.setMember("w 1", "a@b.c", "member");
const [url, init] = fetch.mock.calls[0] as unknown as [string, RequestInit];
expect(url).toBe("/api/v1/workspaces/w%201/members");
expect(init.method).toBe("PUT");
expect(JSON.parse(init.body as string)).toEqual({ email: "a@b.c", role: "member" });
});
it("keeps the server's message", async () => {
const fetch = vi.fn(async () =>
jsonResponse(409, { error: "conflict", message: "organization still has members" }),
);
const err = await createClient({ baseUrl: "", fetch })
.orgs.remove("o1")
.catch((e) => e);
expect(err.code).toBe("conflict");
expect(err.serverMessage).toBe("organization still has members");
});
it("isManager", () => {
expect(isManager({ role: "admin" })).toBe(true);
expect(isManager({ role: "superadmin" })).toBe(true);
expect(isManager({ role: "user" })).toBe(false);
expect(isManager(null)).toBe(false);
});
});