File size: 6,817 Bytes
879c715 c6302b1 b260282 879c715 b260282 a1ab21c 879c715 492326f c6302b1 879c715 b260282 bf25558 a1ab21c c6302b1 879c715 c6302b1 879c715 c6302b1 879c715 c6302b1 879c715 b260282 879c715 b260282 879c715 492326f 879c715 492326f 879c715 492326f 879c715 a1ab21c 879c715 a1ab21c 879c715 a1ab21c 879c715 a1ab21c 879c715 a1ab21c 879c715 a1ab21c 879c715 b260282 879c715 b260282 879c715 b260282 879c715 b260282 c6302b1 | 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | /* eslint-disable complexity, @typescript-eslint/no-base-to-string */
import { afterEach, describe, expect, it, vi } from "vitest";
import {
defaultFilters,
addPoolAdmin,
addPoolMember,
addPoolMemberOrg,
fetchAdminEnrichment,
fetchAdminFreeLabels,
fetchAdminPool,
fetchAdminServiceAccounts,
fetchContributors,
fetchFreeLabel,
fetchFreeLabelGraph,
fetchFreeLabels,
fetchLabels,
fetchMe,
fetchTweets,
issueServiceAccount,
removePoolAdmin,
removePoolMember,
removePoolMemberOrg,
repairPoolConfig,
repairServiceAccounts,
revokeServiceAccount,
revokeServiceAccountKey,
rotateServiceAccount,
tweetsQueryString,
} from "../src/lib/api.js";
afterEach(() => vi.unstubAllGlobals());
describe("tweetsQueryString", () => {
it("serializes the approved free-label contract and omits empty values", () => {
expect(tweetsQueryString(defaultFilters)).toBe("dedup=true");
expect(
tweetsQueryString(
{
...defaultFilters,
contributors: ["alice", "bob"],
labels: ["ai"],
freeLabel: "vllm",
q: "serve",
until: "2026-07-01",
hasMedia: true,
},
"next",
),
).toBe(
"contributors=alice%2Cbob&labels=ai&free_label=vllm&q=serve&until=2026-07-01T23%3A59%3A59.999Z&has_media=true&dedup=true&cursor=next",
);
});
});
describe("explorer API", () => {
it("maps public free-label endpoints and graph communities", async () => {
vi.stubGlobal(
"fetch",
vi.fn((input: RequestInfo | URL) => {
const path = String(input).split("?")[0];
if (path === "/api/free-labels")
return Response.json({ free_labels: [{ name: "vllm", count: 4 }] });
if (path === "/api/free-labels/vllm")
return Response.json({
name: "vllm",
tweet_count: 4,
related: [{ name: "sglang", shared_units: 2 }],
});
if (path === "/api/graph")
return Response.json({
nodes: [
{ name: "vllm", unit_count: 4 },
{ name: "sglang", unit_count: 2 },
],
links: [{ source: "vllm", target: "sglang", weight: 2 }],
});
return new Response("missing", { status: 404 });
}),
);
await expect(fetchFreeLabels()).resolves.toEqual([{ name: "vllm", post_count: 4 }]);
await expect(fetchFreeLabel("vllm")).resolves.toEqual({
name: "vllm",
post_count: 4,
related: [{ name: "sglang", shared: 2 }],
});
await expect(fetchFreeLabelGraph(300)).resolves.toEqual({
nodes: [
{ id: "vllm", name: "vllm", docs: 4, group: 0 },
{ id: "sglang", name: "sglang", docs: 2, group: 0 },
],
links: [{ source: "vllm", target: "sglang", weight: 2 }],
});
});
it("maps authenticated viewer, filter, taxonomy, and contributor reads", async () => {
vi.stubGlobal(
"fetch",
vi.fn((input: RequestInfo | URL) => {
const path = String(input).split("?")[0];
if (path === "/api/me") return Response.json({ username: "alice", isAdmin: true });
if (path === "/api/tweets") return Response.json({ records: [], nextCursor: "next" });
if (path === "/api/contributors")
return Response.json({
contributors: [{ username: "alice", tweetCount: 1, lastPooledAt: "now" }],
});
if (path === "/api/labels")
return Response.json({ labels: [{ name: "ai", description: "AI", count: 2 }] });
return new Response("missing", { status: 404 });
}),
);
await expect(fetchMe()).resolves.toEqual({ username: "alice", isAdmin: true });
await expect(fetchTweets(defaultFilters)).resolves.toEqual({ records: [], nextCursor: "next" });
await expect(fetchContributors()).resolves.toEqual([
{ username: "alice", tweetCount: 1, lastPooledAt: "now" },
]);
await expect(fetchLabels()).resolves.toEqual([{ name: "ai", description: "AI", count: 2 }]);
});
it("turns unauthenticated reads into their documented states", async () => {
vi.stubGlobal(
"fetch",
vi.fn(() => new Response("no", { status: 401 })),
);
await expect(fetchMe()).resolves.toBeUndefined();
await expect(fetchFreeLabels()).rejects.toThrow("session expired");
});
it("uses the authenticated administration endpoints with encoded identifiers", async () => {
const account = {
id: "reader",
name: "reader",
status: "active",
scopes: [],
created_at: "now",
updated_at: "now",
keys: [],
};
const pool = {
version: 1 as const,
admins: [],
members: [],
member_orgs: [],
bootstrap_admins: [],
updated_at: "now",
source: "dataset" as const,
};
vi.stubGlobal(
"fetch",
vi.fn((input: RequestInfo | URL) => {
const path = String(input);
if (path === "/api/admin/pool")
return Response.json({ pool, viewer: { username: "root" } });
if (path === "/api/admin/enrichment")
return Response.json({
contract_hash: "hash",
totals: { pending: 0, running: 0, retrying: 0, blocked: 0, completed: 0 },
worker_recently_completed: false,
recent_errors: [],
});
if (path === "/api/admin/free-labels")
return Response.json({ registry_revision: 1, labels: [], candidates: [] });
if (path === "/api/admin/service-accounts")
return Response.json({
service_accounts: { version: 1, accounts: [], source: "dataset" },
});
if (path === "/api/admin/service-accounts/repair")
return Response.json({
service_accounts: { version: 1, accounts: [], source: "dataset" },
});
if (path.includes("/keys") && !path.endsWith("/key%201"))
return Response.json({ account, token: "token" });
if (path === "/api/admin/service-accounts")
return Response.json({ account, token: "token" });
if (path.includes("service-accounts")) return Response.json({ account });
return Response.json({ pool });
}),
);
await Promise.all([
fetchAdminPool(),
fetchAdminEnrichment(),
fetchAdminFreeLabels(),
fetchAdminServiceAccounts(),
repairPoolConfig(),
repairServiceAccounts(),
issueServiceAccount("reader", ["units:read"]),
rotateServiceAccount("reader id"),
revokeServiceAccount("reader id"),
revokeServiceAccountKey("reader id", "key 1"),
addPoolMember("alice bob"),
removePoolMember("alice bob"),
addPoolAdmin("alice bob"),
removePoolAdmin("alice bob"),
addPoolMemberOrg("my org"),
removePoolMemberOrg("my org"),
]);
});
});
|