mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
fix(security): harden webhook delivery pipeline and group storage (#11)
* fix(log): preserve structured error details Error objects lose their useful details when serialized as plain JSON. Keep the message, stack and cause in structured log output. * fix(runtime): bound shared in-flight waits A canceled request can leave its shared promise pending forever. Bound joins and coordinate a single replacement so later requests make progress without multiplying upstream work. * fix(bot): reject oversized callback bodies Reject declared oversized bot callbacks before reading their bodies. This keeps unauthenticated requests from wasting worker resources. * test(d1): exercise real sqlite transactions Exercise real foreign keys, transactions and conditional statements instead of mocks that cannot reproduce lost updates or cascades. * fix(storage): preserve claims across kv fallback Only missing tables justify KV fallback; other failures must surface. Remember fallback writes so migration to D1 cannot forget claims and replay completed work. Share marker checks without extra writes. * fix(filter): use isolated linear-time patterns Backtracking patterns can exhaust worker CPU on untrusted payloads. Use linear-time matching and separate bounded cache namespaces so glob and regex entries cannot corrupt each other. * fix(filter): read each event branch correctly Deployment and check branches live in event-specific nested fields. Read those fields and remove only the actual leading ref prefix. * fix(filter): read only own payload properties Inherited object properties are not webhook payload data. Read own properties only so JSONPath cannot traverse prototypes. * fix(filter): validate executable condition trees Malformed trees and unsupported patterns must not silently turn an exclude rule into a pass. Validate the shape and only the patterns the evaluator actually executes. * fix(i18n): bound and cache translation loads Repeated misses and simultaneous override loads spend KV reads for the same language. Cache confirmed misses and share bounded loads without caching read failures as missing translations. * i18n(bot): explain command and account failures Explain account linking, command failures and skipped delivery outcomes consistently in both supported languages. * fix(github): bound and reuse app token fetches Repeated token exchanges waste requests and JWT work for every event. Reuse valid installation tokens, share refreshes and back off briefly after failures while keeping every shared wait bounded. * fix(github): restrict enrichment request urls Webhook-provided URLs must never receive an installation token on another host. Restrict enrichment to GitHub repository API paths, reject redirects and bound optional requests. * fix(formatter): preserve invalid code points Out-of-range numeric entities can throw while formatting a delivery. Keep invalid code points verbatim instead of aborting the message. * fix(formatter): preserve bounded markdown text Code spans, fences, indentation and autolinks are Markdown content, not HTML tags to strip. Preserve them while bounding cleanup input and reusing results across routes for the same event. * fix(formatter): strip only leading ref prefixes A short branch or tag may itself contain refs/heads or refs/tags. Strip one leading full-ref prefix at the caller and keep short names unchanged when generating labels and links. * fix(gitea): link push authors to their forge A Gitea author is not the same-named GitHub account. Build author profile links from the originating forge. * fix(formatter): render deployment state values The deployment status object stringifies as [object Object]. Render its actual state in the status field. * fix(formatter): read security severity levels The scanner severity is not the security severity of an alert. Prefer the security level so critical and high alerts get the right label and color. * fix(formatter): hide actions on other forges Action buttons call the GitHub API as the linked user. Hide them for other forges to avoid operating on a same-named GitHub repository. * fix(check): isolate updates by check suite Same-named checks from different suites must not edit one message. Include the suite or app identity and retain the former key for controlled adoption of existing tracked messages. * fix(logs): scope queries before limiting results Filter authorized groups in SQL before LIMIT and aggregate only visible rows. Preserve current group attribution for late writes, surface read failures and combine related totals in one scan. * perf(d1): index pruning and failed send queries Pruning and failure views should not scan all historical rows. Index the high-value access paths without adding a write-heavy index for infrequent delivery-detail lookups. * fix(discord): bound individual api requests An unbounded API request can outlive an update-chain lock. Bound each request so another holder cannot race a late edit. * fix(discord): defer long rate-limit waits Long rate-limit sleeps block delivery progress inside a worker. Return a retryable 429 for the queue when the wait exceeds the bound. * fix(discord): encode channel path segments Stored channel and message ids are path segments, not URL paths. Encode them so aliases cannot address another Discord resource. * fix(discord): reuse nonces after lost responses A successful create may lose its response. Reuse a stable nonce with enforce_nonce so retries recover that message instead of posting duplicates or accepting an unreadable success. * fix(discord): constrain embeds and role mentions Respect combined embed and component limits as well as field caps. Restrict allowed mentions to validated route roles so payload text cannot notify everyone. * fix(discord): resume bounded command syncs Application changes, command rollbacks and large guild lists can leave commands stale indefinitely. Track the current hash per app and resume bounded paginated work without needless KV writes. * fix(github): verify installation management An installation id is user-controlled, and repository access is not installation management. Verify the owning account or an active organization admin using the user token and read:org scope. * fix(github): resolve immutable member ids A login can be renamed and reclaimed before its first sign-in. Resolve the current immutable user id when membership is granted so a future holder of the login cannot inherit access. * fix(auth): revoke stale sessions and tokens Revoked or rotated credentials must stop working despite stale KV replicas. Track the live token and revocations in D1 and preserve immutable identity bindings rather than trusting a recycled login. * fix(auth): bind sessions and tokens to identity Automatic retries can repeat non-idempotent user actions. Disable those retries and expose a write hook so bot handlers can record completion immediately before the GitHub write. * fix(discord): defer and deduplicate interactions Acknowledge slow actions before Discord times out and deduplicate interaction writes. Preserve comment text and reject edits whose source changed after the modal opened. * fix(telegram): always verify webhook secrets Webhook verification must not silently disable itself when the optional secret is absent. Derive a stable secret from the bot token and reject callbacks when verification is unavailable. * fix(telegram): bound individual api requests Bound individual platform calls so they cannot outlive the lock protecting a message update or stall a whole delivery invocation. * fix(telegram): honor nested rate-limit delays Telegram places retry_after inside parameters, not at the top level. Read that value and defer long waits to the delivery queue. * fix(telegram): report unreadable send results An unreadable successful response may already have sent the message. Report an explicit uncertain result instead of retrying a create without an idempotency key or pretending it succeeded. * fix(telegram): keep editable messages as text Guessing photo versus text from message content makes later edits use the wrong API and a smaller caption budget. Always send text and use the avatar as a link preview. * fix(telegram): render hard breaks as newlines Telegram HTML does not support br tags. Render Markdown hard breaks as plain newlines so otherwise valid messages are accepted. * fix(telegram): clamp visible html characters Telegram counts visible characters, not HTML source bytes. Truncate without splitting tags, entities or surrogate pairs. * fix(telegram): honor the author emoji toggle The author marker must obey the group emoji preference just like formatter-added decoration. * fix(telegram): keep completed commands claimed Redelivery after a completed write must never post another comment. Keep completion claims despite reply failures, scope updates per bot and only issue account-login links in private chats. * fix(feishu): bound individual api requests Bound individual platform calls so they cannot outlive the lock protecting a message update or stall a whole delivery invocation. * fix(feishu): classify retryable api failures Business error codes can be transient even in HTTP 200 responses. Classify token and rate-limit errors for retry and defer long waits. * fix(feishu): retry sends with stable uuids An unreadable response does not prove a send failed. Reuse the same uuid for retries so recovering the result cannot create a second Feishu message. * fix(feishu): refresh and evict tokens safely Separate app token caches, stop reusing refused tokens and share refreshes. Cache failures briefly and keep valid tokens in memory when a best-effort KV cache write fails. * fix(feishu): use the card update endpoint Delayed card updates use the card update endpoint; the config endpoint cannot update an interaction response. * fix(feishu): neutralize untrusted mentions Forge text is untrusted and can contain at-all tags. Neutralize mentions in card bodies and footers before rendering. * fix(feishu): authenticate and dedupe callbacks Authenticate signed or token-verified callbacks before dispatching commands. Decode encrypted envelopes and handle card actions using the correct fields without claiming irrelevant chat events. * fix(target): normalize actual destinations Different target spellings can name the same actual destination. Normalize channel, thread and topic identities for ownership checks and per-route duplicate suppression. * fix(config): transact per-group configuration Whole-table writes and cached snapshots can overwrite other groups or resurrect removed configuration. Use per-group guarded writes, one-time identity migration and explicit unreadable-row failures. * fix(group): preserve identity through lifecycle Group names are mutable and reusable, so they cannot own durable state. Preserve immutable identities across lifecycle operations and commit invite use with membership under transaction-time guards. * fix(oauth): confirm account links and installs A login link or installation id is not proof of account ownership. Verify installation management, bind browser state and require an explicit account-link confirmation before granting chat access. * fix(queue): fence leases and record target outcomes Retries must remember completed targets and distinguish active lease holders. Fence state updates, preserve permanent outcomes and reject missing parked payloads instead of dispatching empty data. * fix(dispatch): isolate and resume tenant sends Check tenant sources before filters and never dispatch orphan routes. Record each target outcome, isolate update chains by forge and group identity, and keep stale events from overwriting newer messages. * fix(queue): bound batches and resume deliveries A queue invocation has shared query and subrequest limits. Use a small batch and shared send budget, renew fenced leases and resume only unfinished work without exhausting unstarted messages. * fix(admin): guard revisions and tenant targets Read-time authorization alone cannot protect concurrent edits. Guard revisions, current roles and destination ownership in the write transaction, and use per-group endpoints for isolated changes. * fix(webhook): confirm only accepted deliveries Acknowledging before queue acceptance permanently loses deliveries. Confirm only accepted work, release failed claims, scope replay identities and strip tenant-supplied installation authority. * fix(storage): prune abandoned parked payloads Oversized payloads can remain after interrupted enqueue attempts. Prune objects beyond their retry lifetime without deleting payloads that a current delivery may still need. * fix(console): expose api conflict statuses Callers need the HTTP status to distinguish stale edits from other failures and reload current data after a conflict. * refactor(console): model group revisions Carry server revisions explicitly so console writes can reject changes based on an outdated group snapshot. * fix(console): send loaded route revisions Send the revision actually loaded with the route list and adopt the server response after a save. Reload on conflict instead of silently overwriting another editor. * fix(console): guard fragment saves by revision Fragments share a group version with other configuration writes. Submit the loaded revision and reload on conflict to preserve concurrent edits. * fix(console): save groups with guarded writes Whole-list saves can overwrite unrelated groups. Use per-group revision-aware writes, retain rename results and confirm deletion on the exact group, routes and version shown to the user. * fix(console): reject edits of stale routes A conflict reload can leave an older route editor open. Compare its original value before saving so it cannot overwrite the newly loaded route. * fix(console): show overview loading failures Failed overview requests must not look like an empty dashboard or a successful refresh. Show the reason while retaining prior data. * fix(console): explain rejected install bindings Explain why an installation binding was refused instead of silently returning to the console with no visible outcome. * fix(console): allow selecting a group named all The all sentinel collides with a valid group id. Use a value that cannot be a group id so that group stays selectable. * fix(console): discard stale log detail responses An older detail request can finish after a newer selection or close. Discard it so the dialog never shows the previously clicked record. * fix(console): retain legacy group owners Groups predating member roles store owners in adminIds. Load those as owners so adding a member never drops all old owners. * fix(console): lock member edits during saves Edits made during a save are overwritten by its result. Disable member controls and guard handlers until the write settles. * fix(console): show invite loading failures Failed invite loads must not look like no pending invites. Show the error and discard responses from a previously shown group. * fix(console): retain legacy discord log targets A stored log target without platform means Discord. Keep that meaning in the editor instead of clearing the target. * fix(console): retain undisplayed forge sources An editor that only displays some providers must preserve the others. Keep stored custom sources when saving unrelated group settings. * fix(console): lock the group editor while saving Prevent edits while a group save is in flight and make super-only installation settings readonly instead of silently ignoring edits. * fix(console): lock route edits and show failures Lock the route editor while saving and show fragment load failures with a retry action rather than presenting a false empty list. * fix(config): expose the legal contact override Declare the public runtime key so the documented environment override reaches legal pages in deployed Nuxt builds. * build(deploy): migrate d1 before deployment Apply pending D1 migrations before deploying code that requires them. Stop deployment when migration fails instead of running against an incomplete schema. * docs(worker): align guides with runtime behavior Synchronize both language guides, API references and examples with the implemented authorization, storage and delivery behavior. Document migration requirements and accepted operational limits. * fix(storage): repair orphaned KV routes Repair orphaned KV routes before serving KV-only configuration. * fix(security): fully escape forge HTML --------- Co-authored-by: RhenCloud <i@rhen.cloud>
This commit is contained in:
1 parent
dbf162bf74
commit
978c53eeb3
163 files changed
+18459
-3607
No files matched your search
@@ -9,7 +9,7 @@ import { createAdminSession, adminCookie } from "../server/lib/web/session";
|
||||
import { loadGroups, invalidateGroupsCache } from "../server/lib/web/groups";
|
||||
import { loadRoutes } from "../server/lib/config";
|
||||
import { createInvite, listInvites } from "../server/lib/web/invites";
|
||||
import { getTenantSecret, setTenantSecret } from "../server/lib/web/tenants";
|
||||
import { getTenantSecret } from "../server/lib/web/tenants";
|
||||
import { makeEvent, responseStatus } from "./helpers";
|
||||
import type { Env, Route } from "../server/lib/types";
|
||||
|
||||
@@ -85,8 +85,9 @@ describe("admin handlers (h3)", () => {
|
||||
},
|
||||
] as Route[]),
|
||||
);
|
||||
await setTenantSecret(kv, "old-team");
|
||||
const originalSecret = (await getTenantSecret(kv, "old-team"))!;
|
||||
// A secret stored before group instances existed (keyed by group id).
|
||||
const originalSecret = "legacy-secret";
|
||||
await kv.put("tenant:old-team", originalSecret);
|
||||
await createInvite(kv, {
|
||||
groupId: "old-team",
|
||||
role: "viewer",
|
||||
@@ -114,8 +115,8 @@ describe("admin handlers (h3)", () => {
|
||||
const routes = await loadRoutes(kv);
|
||||
expect(routes[0]!.groupId).toBe("new-team");
|
||||
|
||||
expect(await getTenantSecret(kv, "new-team")).toBe(originalSecret);
|
||||
expect(await getTenantSecret(kv, "old-team")).toBeNull();
|
||||
// The secret follows the group (it is keyed by the group's instance).
|
||||
expect(await getTenantSecret(kv, groups[0]!)).toBe(originalSecret);
|
||||
|
||||
const invites = await listInvites(kv, "new-team");
|
||||
expect(invites).toHaveLength(1);
|
||||
|
||||
+22
-8
@@ -296,6 +296,20 @@ describe("ensureInstallationGroup", () => {
|
||||
});
|
||||
|
||||
describe("tenant webhook secrets", () => {
|
||||
const G1 = { id: "g1", instance: "inst-g1" };
|
||||
|
||||
it("never lets a group created later adopt a secret left under its id", async () => {
|
||||
const kv = createMockKV();
|
||||
await kv.put("tenant:g1", "old-secret");
|
||||
expect(await getTenantSecret(kv, G1)).toBeNull();
|
||||
// The group that had the id before instances still uses its old secret,
|
||||
// under whatever id it has now — and without copying it anywhere.
|
||||
expect(await getTenantSecret(kv, { ...G1, id: "moved", legacyId: "g1" })).toBe("old-secret");
|
||||
// A pre-instance group renamed to the id later never adopts it.
|
||||
expect(await getTenantSecret(kv, { ...G1, legacyId: "other" })).toBeNull();
|
||||
expect(await kv.get("tenant:i:" + G1.instance)).toBeNull();
|
||||
});
|
||||
|
||||
it("generates 64-char hex secrets", () => {
|
||||
const s = generateTenantSecret();
|
||||
expect(s).toMatch(/^[0-9a-f]{64}$/);
|
||||
@@ -303,19 +317,19 @@ describe("tenant webhook secrets", () => {
|
||||
|
||||
it("creates, reads, and deletes a tenant secret", async () => {
|
||||
const kv = createMockKV();
|
||||
expect(await getTenantSecret(kv, "g1")).toBeNull();
|
||||
const secret = await setTenantSecret(kv, "g1");
|
||||
expect(await getTenantSecret(kv, "g1")).toBe(secret);
|
||||
await deleteTenantSecret(kv, "g1");
|
||||
expect(await getTenantSecret(kv, "g1")).toBeNull();
|
||||
expect(await getTenantSecret(kv, G1)).toBeNull();
|
||||
const secret = await setTenantSecret(kv, G1);
|
||||
expect(await getTenantSecret(kv, G1)).toBe(secret);
|
||||
await deleteTenantSecret(kv, G1);
|
||||
expect(await getTenantSecret(kv, G1)).toBeNull();
|
||||
});
|
||||
|
||||
it("regenerating replaces the previous secret", async () => {
|
||||
const kv = createMockKV();
|
||||
const a = await setTenantSecret(kv, "g1");
|
||||
const b = await setTenantSecret(kv, "g1");
|
||||
const a = await setTenantSecret(kv, G1);
|
||||
const b = await setTenantSecret(kv, G1);
|
||||
expect(a).not.toBe(b);
|
||||
expect(await getTenantSecret(kv, "g1")).toBe(b);
|
||||
expect(await getTenantSecret(kv, G1)).toBe(b);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+34
-70
@@ -1,79 +1,33 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import { recordAudit, getAuditLog, pruneAuditLogs } from "../server/lib/lib/audit";
|
||||
|
||||
function createMockD1(): D1Database {
|
||||
let rows: Array<Record<string, unknown>> = [];
|
||||
let id = 1;
|
||||
|
||||
function queryAll(sql: string, args: unknown[]): Array<Record<string, unknown>> {
|
||||
if (sql.startsWith("INSERT")) return [];
|
||||
let filtered = rows;
|
||||
if (sql.includes("group_id = ?")) {
|
||||
const gid = args[0] as string;
|
||||
filtered = rows.filter((r) => r.group_id === gid);
|
||||
}
|
||||
const limit = args[args.length - 1] as number;
|
||||
return [...filtered]
|
||||
.sort((a, b) => (b.ts as number) - (a.ts as number))
|
||||
.slice(0, limit)
|
||||
.map((r) => ({
|
||||
id: r.id,
|
||||
ts: r.ts ?? 0,
|
||||
actor_id: r.actor_id ?? null,
|
||||
actor_login: r.actor_login ?? null,
|
||||
action: r.action ?? "",
|
||||
target_type: r.target_type ?? null,
|
||||
target_id: r.target_id ?? null,
|
||||
group_id: r.group_id ?? null,
|
||||
detail: r.detail ?? null,
|
||||
ip: r.ip ?? null,
|
||||
}));
|
||||
}
|
||||
|
||||
const db = {
|
||||
prepare: (sql: string): { bind: (args: unknown[]) => unknown } => ({
|
||||
bind: (...args: unknown[]) => ({
|
||||
run: async (): Promise<{ meta: { changes: number }; results?: unknown[] }> => {
|
||||
if (sql.startsWith("INSERT")) {
|
||||
const row: Record<string, unknown> = { id: id++ };
|
||||
for (const [key, value] of [
|
||||
["ts", args[0]],
|
||||
["actor_id", args[1]],
|
||||
["actor_login", args[2]],
|
||||
["action", args[3]],
|
||||
["target_type", args[4]],
|
||||
["target_id", args[5]],
|
||||
["group_id", args[6]],
|
||||
["detail", args[7]],
|
||||
["ip", args[8]],
|
||||
] as const) {
|
||||
if (value != null) row[key] = value;
|
||||
}
|
||||
rows.push(row);
|
||||
return { meta: { changes: 1 } };
|
||||
}
|
||||
if (sql.startsWith("DELETE")) {
|
||||
const cutoff = args[0] as number;
|
||||
const before = rows.length;
|
||||
rows = rows.filter((r) => (r.ts as number) >= cutoff);
|
||||
return { meta: { changes: before - rows.length } };
|
||||
}
|
||||
return { meta: { changes: 0 }, results: queryAll(sql, args) };
|
||||
},
|
||||
all: async (): Promise<{ results: Array<Record<string, unknown>> }> => ({
|
||||
results: queryAll(sql, args),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
} as unknown as D1Database;
|
||||
return db;
|
||||
}
|
||||
import { createSqliteD1 } from "./d1-sqlite";
|
||||
|
||||
describe("audit log", () => {
|
||||
let db: D1Database;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createMockD1();
|
||||
beforeEach(async () => {
|
||||
db = createSqliteD1().db;
|
||||
for (const id of ["g1", "g2", "g3"]) {
|
||||
await db
|
||||
.prepare(
|
||||
"INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES (?, ?, ?, 1, 0, 0)",
|
||||
)
|
||||
.bind(id, id, JSON.stringify({ id, name: id, instance: `inst-${id}` }))
|
||||
.run();
|
||||
}
|
||||
});
|
||||
|
||||
it("files a late entry under the group's current id, never a deleted one", async () => {
|
||||
// The group was renamed (same instance) before the entry was written.
|
||||
await db.prepare("UPDATE d1_groups SET id = 'g9' WHERE id = 'g1'").run();
|
||||
await recordAudit(db, { ts: 1, action: "renamed", groupId: "g1", groupInstance: "inst-g1" });
|
||||
// An entry for a group deleted meanwhile belongs to no group.
|
||||
await db.prepare("DELETE FROM d1_groups WHERE id = 'g2'").run();
|
||||
await recordAudit(db, { ts: 2, action: "deleted", groupId: "g2" });
|
||||
expect((await getAuditLog(db, { groupId: "g9" })).map((e) => e.action)).toEqual(["renamed"]);
|
||||
expect(await getAuditLog(db, { groupId: "g1" })).toEqual([]);
|
||||
expect(await getAuditLog(db, { groupId: "g2" })).toEqual([]);
|
||||
expect((await getAuditLog(db)).map((e) => e.groupId ?? null)).toEqual([null, "g9"]);
|
||||
});
|
||||
|
||||
it("records and loads entries newest-first", async () => {
|
||||
@@ -109,6 +63,16 @@ describe("audit log", () => {
|
||||
expect(entries[0]!.action).toBe("y");
|
||||
});
|
||||
|
||||
it("filters by a set of groups before applying the limit", async () => {
|
||||
await recordAudit(db, { ts: 1, actorLogin: "a", action: "mine", groupId: "g1" });
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await recordAudit(db, { ts: 10 + i, actorLogin: "b", action: "other", groupId: "g2" });
|
||||
}
|
||||
const entries = await getAuditLog(db, { groupId: ["g1", "g3"], limit: 2 });
|
||||
expect(entries.map((e) => e.action)).toEqual(["mine"]);
|
||||
expect(await getAuditLog(db, { groupId: [] })).toEqual([]);
|
||||
});
|
||||
|
||||
it("prunes entries older than the retention window", async () => {
|
||||
const now = Date.now();
|
||||
await recordAudit(db, { ts: now - 100 * 86400_000, actorLogin: "a", action: "old" });
|
||||
|
||||
+439
-259
@@ -1,33 +1,20 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { d1ConfigStore, type ConfigStore } from "../server/lib/storage/config-store";
|
||||
import {
|
||||
d1ConfigStore,
|
||||
kvConfigStore,
|
||||
ConfigConflictError,
|
||||
UNGROUPED_ID,
|
||||
} from "../server/lib/storage/config-store";
|
||||
import type { Route, Group } from "../server/lib/types";
|
||||
|
||||
interface Stmt {
|
||||
sql: string;
|
||||
bound: unknown[];
|
||||
bind: (...args: unknown[]) => Stmt;
|
||||
all: () => Promise<{ results: Array<Record<string, unknown>> }>;
|
||||
}
|
||||
|
||||
interface FakeDB {
|
||||
prepare: (sql: string) => Stmt;
|
||||
batch: (stmts: Stmt[]) => Promise<unknown[]>;
|
||||
}
|
||||
|
||||
interface FakeDBOptions {
|
||||
/** Whether `d1_routes` has the `position` column (migration 0012). */
|
||||
positionColumn?: boolean;
|
||||
/** How many batches fail with a transient (non-schema) error first. */
|
||||
batchFailures?: number;
|
||||
}
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
|
||||
function route(id: string, groupId: string, overrides: Partial<Route> = {}): Route {
|
||||
return {
|
||||
id,
|
||||
name: `route-${id}`,
|
||||
enabled: true,
|
||||
filters: [],
|
||||
targets: [],
|
||||
filters: [{ type: "event", match: "push" }],
|
||||
targets: [{ platform: "discord", channelId: "c1" }],
|
||||
groupId,
|
||||
stop: false,
|
||||
fallback: false,
|
||||
@@ -35,259 +22,452 @@ function route(id: string, groupId: string, overrides: Partial<Route> = {}): Rou
|
||||
};
|
||||
}
|
||||
|
||||
function group(id: string): Group {
|
||||
return { id, name: `group-${id}`, adminIds: [] };
|
||||
function group(id: string, overrides: Partial<Group> = {}): Group {
|
||||
return { id, name: `group-${id}`, adminIds: [], ...overrides };
|
||||
}
|
||||
|
||||
function createDB(options: FakeDBOptions = {}): {
|
||||
db: FakeDB & D1Database;
|
||||
routesTable: Route[];
|
||||
groupsTable: Group[];
|
||||
queries: string[];
|
||||
batches: Stmt[][];
|
||||
function setup(options: { position?: boolean } = {}): {
|
||||
db: D1Database;
|
||||
sqlite: ReturnType<typeof createSqliteD1>["sqlite"];
|
||||
kv: KVNamespace;
|
||||
kvStore: Map<string, string>;
|
||||
cfg: ReturnType<typeof d1ConfigStore>;
|
||||
} {
|
||||
const routesTable: Route[] = [];
|
||||
const groupsTable: Group[] = [];
|
||||
const queries: string[] = [];
|
||||
const batches: Stmt[][] = [];
|
||||
let batchFailures = options.batchFailures ?? 0;
|
||||
const db: FakeDB = {
|
||||
prepare(sql: string): Stmt {
|
||||
const stmt: Stmt = {
|
||||
sql,
|
||||
bound: [],
|
||||
bind(...args: unknown[]): Stmt {
|
||||
stmt.bound = args;
|
||||
return stmt;
|
||||
},
|
||||
async all(): Promise<{ results: Array<Record<string, unknown>> }> {
|
||||
queries.push(sql);
|
||||
if (sql.includes("position") && !options.positionColumn) {
|
||||
throw new Error("D1_ERROR: no such column: position at offset 33: SQLITE_ERROR");
|
||||
}
|
||||
if (sql.includes("FROM d1_routes")) {
|
||||
return {
|
||||
results: routesTable.map((r) => ({
|
||||
id: r.id,
|
||||
group_id: r.groupId ?? "",
|
||||
name: r.name,
|
||||
enabled: r.enabled ? 1 : 0,
|
||||
filters: JSON.stringify(r.filters),
|
||||
targets: JSON.stringify(r.targets),
|
||||
stop: r.stop ? 1 : 0,
|
||||
fallback: r.fallback ? 1 : 0,
|
||||
discord_role_ids: r.discordRoleIds ? JSON.stringify(r.discordRoleIds) : null,
|
||||
ast: r.ast ? JSON.stringify(r.ast) : null,
|
||||
})),
|
||||
};
|
||||
}
|
||||
if (sql.includes("FROM d1_groups")) {
|
||||
return {
|
||||
results: groupsTable.map((g) => ({
|
||||
id: g.id,
|
||||
name: g.name,
|
||||
data: JSON.stringify(g),
|
||||
version: 1,
|
||||
})),
|
||||
};
|
||||
}
|
||||
return { results: [] };
|
||||
},
|
||||
};
|
||||
return stmt;
|
||||
},
|
||||
async batch(stmts: Stmt[]): Promise<unknown[]> {
|
||||
batches.push(stmts);
|
||||
if (batchFailures > 0) {
|
||||
batchFailures--;
|
||||
throw new Error("D1_ERROR: network connection lost");
|
||||
}
|
||||
if (!options.positionColumn && stmts.some((st) => st.sql.includes("position,"))) {
|
||||
throw new Error("D1_ERROR: table d1_routes has no column named position: SQLITE_ERROR");
|
||||
}
|
||||
return [];
|
||||
},
|
||||
};
|
||||
return { db: db as FakeDB & D1Database, routesTable, groupsTable, queries, batches };
|
||||
}
|
||||
|
||||
/** Bound `(id, position)` pairs of every route upsert in a batch. */
|
||||
function upsertPositions(stmts: Stmt[]): Array<[unknown, unknown]> {
|
||||
return stmts
|
||||
.filter((s) => s.sql.includes("INSERT INTO d1_routes"))
|
||||
.map((s) => [s.bound[0], s.sql.includes("position") ? s.bound[10] : undefined]);
|
||||
}
|
||||
|
||||
function createKV(): { kv: KVNamespace; store: Map<string, string> } {
|
||||
const store = new Map<string, string>();
|
||||
const kv = {
|
||||
store,
|
||||
get: async <T>(key: string, type?: string): Promise<T | null> => {
|
||||
const v = store.get(key);
|
||||
if (v == null) return null;
|
||||
if (type === "json") return JSON.parse(v) as T;
|
||||
return v as unknown as T;
|
||||
},
|
||||
put: async (key: string, value: string): Promise<void> => {
|
||||
store.set(key, value);
|
||||
},
|
||||
delete: async (key: string): Promise<void> => {
|
||||
store.delete(key);
|
||||
},
|
||||
list: async (): Promise<{ keys: unknown[] }> => ({ keys: [] }),
|
||||
};
|
||||
return { kv: kv as unknown as KVNamespace, store };
|
||||
const { db, sqlite } = createSqliteD1({
|
||||
migrations: (name) => options.position !== false || !name.startsWith("0012"),
|
||||
});
|
||||
const { kv, store } = createMemoryKV();
|
||||
return { db, sqlite, kv, kvStore: store, cfg: d1ConfigStore(db, kv) };
|
||||
}
|
||||
|
||||
describe("d1ConfigStore", () => {
|
||||
it("loads empty when D1 and KV are empty", async () => {
|
||||
const { db } = createDB();
|
||||
const { kv } = createKV();
|
||||
const store: ConfigStore = d1ConfigStore(db, kv);
|
||||
expect(await store.loadRoutes()).toEqual([]);
|
||||
expect(await store.loadGroups()).toEqual([]);
|
||||
const { cfg } = setup();
|
||||
expect(await cfg.loadRoutes()).toEqual([]);
|
||||
expect(await cfg.loadGroups()).toEqual([]);
|
||||
});
|
||||
|
||||
it("seeds routes from KV into memory when D1 is empty and caches in KV", async () => {
|
||||
const { db } = createDB();
|
||||
const { kv, store } = createKV();
|
||||
const existing = [route("r1", "g1")];
|
||||
store.set("config:routes", JSON.stringify(existing));
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const loaded = await cfg.loadRoutes();
|
||||
expect(loaded).toHaveLength(1);
|
||||
expect(loaded[0].id).toBe("r1");
|
||||
});
|
||||
|
||||
it("reads routes directly from D1 when populated", async () => {
|
||||
const { db, routesTable } = createDB();
|
||||
const { kv } = createKV();
|
||||
routesTable.push(route("r1", "g1"));
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const loaded = await cfg.loadRoutes();
|
||||
expect(loaded).toHaveLength(1);
|
||||
expect(loaded[0].id).toBe("r1");
|
||||
expect(loaded[0].groupId).toBe("g1");
|
||||
});
|
||||
|
||||
it("round-trips routes through D1 + KV cache on save", async () => {
|
||||
const { db, routesTable } = createDB();
|
||||
const { kv, store } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const routes = [route("r1", "g1"), route("r2", "g2")];
|
||||
await cfg.saveRoutes(routes);
|
||||
expect(routesTable).toHaveLength(0);
|
||||
const cached = store.get("config:routes");
|
||||
expect(cached).toBe(JSON.stringify(routes));
|
||||
const reloaded = await cfg.loadRoutes();
|
||||
expect(reloaded).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("round-trips groups through D1 + KV cache on save", async () => {
|
||||
const { db } = createDB();
|
||||
const { kv, store } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const groups = [group("g1"), group("g2")];
|
||||
await cfg.saveGroups(groups);
|
||||
expect(store.get("config:groups")).toBe(JSON.stringify(groups));
|
||||
const reloaded = await cfg.loadGroups();
|
||||
expect(reloaded).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("invalidateCache forces a reload", async () => {
|
||||
const { db, routesTable } = createDB();
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
routesTable.push(route("r1", "g1"));
|
||||
const first = await cfg.loadRoutes();
|
||||
expect(first).toHaveLength(1);
|
||||
routesTable.push(route("r2", "g2"));
|
||||
const cached = await cfg.loadRoutes();
|
||||
expect(cached).toHaveLength(1);
|
||||
cfg.invalidateCache();
|
||||
const second = await cfg.loadRoutes();
|
||||
expect(second).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("persists the per-group route order when the position column exists", async () => {
|
||||
const { db, batches } = createDB({ positionColumn: true });
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
await cfg.saveRoutes([route("b", "g1"), route("x", "g2"), route("a", "g1")]);
|
||||
expect(upsertPositions(batches[0]!)).toEqual([
|
||||
["b", 0],
|
||||
["x", 0],
|
||||
["a", 1],
|
||||
]);
|
||||
});
|
||||
|
||||
it("loads routes ordered by group and position when the column exists", async () => {
|
||||
const { db, queries } = createDB({ positionColumn: true });
|
||||
const { kv } = createKV();
|
||||
await d1ConfigStore(db, kv).loadRoutes();
|
||||
expect(queries.some((q) => q.includes("FROM d1_routes ORDER BY group_id, position, id"))).toBe(
|
||||
true,
|
||||
it("seeds D1 once from the legacy KV configuration", async () => {
|
||||
const { cfg, kvStore, sqlite } = setup();
|
||||
kvStore.set("config:groups", JSON.stringify([group("g1")]));
|
||||
kvStore.set(
|
||||
"config:routes",
|
||||
JSON.stringify([
|
||||
route("r1", "g1"),
|
||||
// Legacy shape: a single `target` and no group.
|
||||
{ ...route("r2", ""), groupId: undefined, targets: undefined, target: { channelId: "x" } },
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to the legacy id order and inserts on an unmigrated database", async () => {
|
||||
const { db, queries, batches } = createDB();
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
await cfg.loadRoutes();
|
||||
expect(queries.at(-1)).toContain("FROM d1_routes ORDER BY id");
|
||||
await cfg.saveRoutes([route("r1", "g1")]);
|
||||
expect(upsertPositions(batches.at(-1)!)).toEqual([["r1", undefined]]);
|
||||
});
|
||||
|
||||
it("picks up the position column once the database is migrated", async () => {
|
||||
const options: FakeDBOptions = {};
|
||||
const { db, queries } = createDB(options);
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
await cfg.loadRoutes();
|
||||
expect(queries.at(-1)).toContain("ORDER BY id");
|
||||
options.positionColumn = true;
|
||||
cfg.invalidateCache();
|
||||
await cfg.loadRoutes();
|
||||
expect(queries.at(-1)).toContain("ORDER BY group_id, position, id");
|
||||
});
|
||||
|
||||
it("never retries without positions when D1 fails for another reason", async () => {
|
||||
const { db, batches } = createDB({ positionColumn: true, batchFailures: 1 });
|
||||
const { kv, store } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
const routes = [route("b", "g1"), route("a", "g1")];
|
||||
await cfg.saveRoutes(routes);
|
||||
// Only the positioned batch was attempted; the save took the existing
|
||||
// "D1 unavailable" path instead of writing rows that lose the order.
|
||||
expect(batches).toHaveLength(1);
|
||||
expect(upsertPositions(batches[0]!)).toEqual([
|
||||
["b", 0],
|
||||
["a", 1],
|
||||
const loaded = await cfg.loadRoutes();
|
||||
expect(loaded.map((r) => [r.id, r.groupId])).toEqual([
|
||||
["r1", "g1"],
|
||||
["r2", UNGROUPED_ID],
|
||||
]);
|
||||
expect(store.get("config:routes")).toBe(JSON.stringify(routes));
|
||||
// The legacy single target was migrated before it reached D1.
|
||||
expect(loaded[1]!.targets).toEqual([{ channelId: "x" }]);
|
||||
expect((await cfg.loadGroups()).map((g) => g.id)).toEqual(["g1", UNGROUPED_ID]);
|
||||
expect(kvStore.get("config:d1-seeded")).toBe("1");
|
||||
expect(sqlite.query("SELECT COUNT(*) AS n FROM d1_routes").get()).toEqual({ n: 2 });
|
||||
});
|
||||
|
||||
it("updating groups does not cascade-delete routes", async () => {
|
||||
const { db, routesTable, groupsTable } = createDB();
|
||||
const { kv } = createKV();
|
||||
const cfg: ConfigStore = d1ConfigStore(db, kv);
|
||||
it("never resurrects a configuration that was emptied on purpose", async () => {
|
||||
const { cfg, kvStore } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
// A stale legacy copy is still in KV.
|
||||
kvStore.set("config:routes", JSON.stringify([route("old", "g1")]));
|
||||
await cfg.saveGroupRoutes("g1", []);
|
||||
await cfg.deleteGroup("g1");
|
||||
expect(await cfg.loadRoutes({ fresh: true })).toEqual([]);
|
||||
expect(await cfg.loadGroups({ fresh: true })).toEqual([]);
|
||||
});
|
||||
|
||||
// Setup initial state with groups and routes
|
||||
groupsTable.push(group("g1"), group("g2"));
|
||||
routesTable.push(route("r1", "g1"), route("r2", "g2"));
|
||||
it("saving one group's routes leaves every other group untouched", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("a1", "a")]);
|
||||
await cfg.saveGroupRoutes("b", [route("b1", "b"), route("b2", "b")]);
|
||||
// An admin of "a" saves from a snapshot taken before "b" changed.
|
||||
await cfg.saveGroupRoutes("a", [route("a1", "a", { name: "renamed" })]);
|
||||
const all = await cfg.loadRoutes({ fresh: true });
|
||||
expect(all.map((r) => `${r.groupId}/${r.id}/${r.name}`)).toEqual([
|
||||
"a/a1/renamed",
|
||||
"b/b1/route-b1",
|
||||
"b/b2/route-b2",
|
||||
]);
|
||||
});
|
||||
|
||||
// Simulate updating groups (e.g., changing a group's name)
|
||||
const updatedGroups = [{ ...group("g1"), name: "Updated Group 1" }, group("g2")];
|
||||
it("saveRoutes rewrites only the groups whose list changed", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("a1", "a")]);
|
||||
await cfg.saveGroupRoutes("b", [route("b1", "b")]);
|
||||
sqlite.exec("UPDATE d1_routes SET updated_at = 1 WHERE group_id = 'b'");
|
||||
await cfg.saveRoutes([route("a1", "a", { enabled: false }), route("b1", "b")]);
|
||||
expect(sqlite.query("SELECT updated_at FROM d1_routes WHERE group_id = 'b'").get()).toEqual({
|
||||
updated_at: 1,
|
||||
});
|
||||
const a = (await cfg.loadRoutes({ fresh: true })).find((r) => r.id === "a1");
|
||||
expect(a!.enabled).toBe(false);
|
||||
});
|
||||
|
||||
// This should not delete routes
|
||||
await cfg.saveGroups(updatedGroups);
|
||||
it("persists and loads the per-group route order", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("b", "g1"), route("c", "g1"), route("a", "g1")]);
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.id)).toEqual(["b", "c", "a"]);
|
||||
});
|
||||
|
||||
// Routes should still exist in the table
|
||||
// Note: In the fake DB, batch() doesn't actually execute the statements,
|
||||
// so we can't verify the actual deletion behavior here.
|
||||
// This test mainly ensures saveGroups doesn't throw an error.
|
||||
expect(routesTable).toHaveLength(2);
|
||||
it("works on a database without the position column", async () => {
|
||||
const { cfg } = setup({ position: false });
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("b", "g1"), route("a", "g1")]);
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.id)).toEqual(["a", "b"]);
|
||||
});
|
||||
|
||||
it("serves cached routes until invalidated, fresh reads bypass the cache", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
expect(await cfg.loadRoutes()).toHaveLength(1);
|
||||
sqlite.exec("DELETE FROM d1_routes");
|
||||
expect(await cfg.loadRoutes()).toHaveLength(1);
|
||||
expect(await cfg.loadRoutes({ fresh: true })).toHaveLength(0);
|
||||
cfg.invalidateCache();
|
||||
expect(await cfg.loadRoutes()).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("refuses to create or rename onto an id retired after the caller checked it", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
// A concurrent delete/rename retired "x" between the check and the write.
|
||||
sqlite.exec(
|
||||
`INSERT INTO dedup_keys (key, claimed_at, expires_at) VALUES ('group-retired:x', 0, ${Date.now() + 60_000})`,
|
||||
);
|
||||
expect(await cfg.createGroup(group("x"))).toBe(false);
|
||||
await expect(cfg.renameGroup("a", "x")).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => g.id)).toEqual(["a"]);
|
||||
});
|
||||
|
||||
it("counts a group whose own row is unreadable — and has routes — as unreadable", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.createGroup(group("g2"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
sqlite.exec("UPDATE d1_groups SET data = 'null' WHERE id IN ('g1', 'g2')");
|
||||
const snapshot = await cfg.loadSnapshot({ fresh: true });
|
||||
expect(snapshot.unreadable).toEqual(["g1"]);
|
||||
});
|
||||
|
||||
it("counts a stored regex the engine cannot run as unreadable", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
// Valid before the switch to RE2: lookahead now fails to compile.
|
||||
sqlite.exec(
|
||||
`UPDATE d1_routes SET filters = '[{"type":"repo","match":"/^secret(?=/)/","exclude":true}]' WHERE id = 'r1'`,
|
||||
);
|
||||
const snapshot = await cfg.loadSnapshot({ fresh: true });
|
||||
expect(snapshot.unreadable).toEqual(["g1"]);
|
||||
});
|
||||
|
||||
it("keeps consulting KV state written before D1 when seeding from it", async () => {
|
||||
const { cfg, kvStore } = setup();
|
||||
kvStore.set("config:groups", JSON.stringify([group("g1")]));
|
||||
await cfg.loadGroups({ fresh: true });
|
||||
expect(kvStore.has("storage:kv-fallback")).toBe(true);
|
||||
});
|
||||
|
||||
it("counts a route with a filter of the wrong shape as unreadable", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1")]);
|
||||
sqlite.exec(`UPDATE d1_routes SET filters = '[{"type":"event","match":{}}]' WHERE id = 'r1'`);
|
||||
const snapshot = await cfg.loadSnapshot({ fresh: true });
|
||||
expect(snapshot.routes).toEqual([]);
|
||||
expect(snapshot.unreadable).toEqual(["g1"]);
|
||||
});
|
||||
|
||||
it("refuses to replace a group's routes while one of its rows is unreadable", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g1"));
|
||||
await cfg.saveGroupRoutes("g1", [route("r1", "g1"), route("r2", "g1")]);
|
||||
sqlite.exec("UPDATE d1_routes SET targets = '{broken' WHERE id = 'r2'");
|
||||
// Reads skip it, but a save would silently delete it: refused instead.
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.id)).toEqual(["r1"]);
|
||||
await expect(cfg.saveGroupRoutes("g1", [route("r1", "g1")])).rejects.toThrow(/unreadable/);
|
||||
await expect(cfg.saveRoutes([route("r1", "g1", { name: "renamed" })])).rejects.toThrow(
|
||||
/unreadable/,
|
||||
);
|
||||
expect(sqlite.query("SELECT COUNT(*) AS n FROM d1_routes").get()).toEqual({ n: 2 });
|
||||
});
|
||||
|
||||
it("renames a group with its routes and fragments in one transaction", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("old", { name: "Team" }));
|
||||
await cfg.saveGroupRoutes("old", [route("r1", "old"), route("r2", "old")]);
|
||||
sqlite.exec(
|
||||
"INSERT INTO d1_fragments (id, group_id, name, node, created_at, updated_at) VALUES ('f1', 'old', 'F', '{}', 1, 1)",
|
||||
);
|
||||
await cfg.renameGroup("old", "new");
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => [g.id, g.name])).toEqual([
|
||||
["new", "Team"],
|
||||
]);
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => `${r.groupId}/${r.id}`)).toEqual([
|
||||
"new/r1",
|
||||
"new/r2",
|
||||
]);
|
||||
expect(sqlite.query("SELECT group_id FROM d1_fragments").all()).toEqual([{ group_id: "new" }]);
|
||||
});
|
||||
|
||||
it("a failed rename changes nothing", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("r1", "a")]);
|
||||
await expect(cfg.renameGroup("a", "b")).rejects.toThrow();
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.groupId)).toEqual(["a"]);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => g.id)).toEqual(["a", "b"]);
|
||||
});
|
||||
|
||||
it("deleting a group removes its routes and fragments only", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("r1", "a")]);
|
||||
await cfg.saveGroupRoutes("b", [route("r1", "b")]);
|
||||
sqlite.exec(
|
||||
"INSERT INTO d1_fragments (id, group_id, name, node, created_at, updated_at) VALUES ('f1', 'a', 'F', '{}', 1, 1)",
|
||||
);
|
||||
await cfg.deleteGroup("a");
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.groupId)).toEqual(["b"]);
|
||||
expect(sqlite.query("SELECT COUNT(*) AS n FROM d1_fragments").get()).toEqual({ n: 0 });
|
||||
});
|
||||
|
||||
it("saveGroups writes only changed groups and deletes the missing ones", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.createGroup(group("b"));
|
||||
await cfg.saveGroupRoutes("a", [route("r1", "a")]);
|
||||
sqlite.exec("UPDATE d1_groups SET updated_at = 1");
|
||||
await cfg.saveGroups([group("a"), group("c")]);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => g.id)).toEqual(["a", "c"]);
|
||||
expect(sqlite.query("SELECT updated_at FROM d1_groups WHERE id = 'a'").get()).toEqual({
|
||||
updated_at: 1,
|
||||
});
|
||||
// Updating a group never touches its routes.
|
||||
expect(await cfg.loadRoutes({ fresh: true })).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("createGroup never overwrites an existing group", async () => {
|
||||
const { cfg } = setup();
|
||||
expect(await cfg.createGroup(group("g", { name: "first" }))).toBe(true);
|
||||
expect(await cfg.createGroup(group("g", { name: "second" }))).toBe(false);
|
||||
expect((await cfg.loadGroups({ fresh: true }))[0]!.name).toBe("first");
|
||||
});
|
||||
|
||||
it("updateGroup is a compare-and-swap that retries on contention", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g", { name: "v0" }));
|
||||
let calls = 0;
|
||||
const updated = await cfg.updateGroup("g", (g) => {
|
||||
calls++;
|
||||
// A concurrent writer bumps the row between our read and our write once.
|
||||
if (calls === 1)
|
||||
sqlite.exec(
|
||||
"UPDATE d1_groups SET version = version + 1, data = json_set(data, '$.name', 'other')",
|
||||
);
|
||||
return { ...g, name: `${g.name}+mine` };
|
||||
});
|
||||
expect(calls).toBe(2);
|
||||
expect(updated!.name).toBe("other+mine");
|
||||
expect((await cfg.loadGroups({ fresh: true }))[0]!.name).toBe("other+mine");
|
||||
});
|
||||
|
||||
it("updateGroup gives up with a conflict when contention never settles", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
await expect(
|
||||
cfg.updateGroup("g", (g) => {
|
||||
sqlite.exec("UPDATE d1_groups SET version = version + 1");
|
||||
return { ...g, name: "x" };
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
});
|
||||
|
||||
it("updateGroup returns null for a missing group and keeps a group on null", async () => {
|
||||
const { cfg } = setup();
|
||||
expect(await cfg.updateGroup("missing", (g) => g)).toBeNull();
|
||||
await cfg.createGroup(group("g"));
|
||||
expect((await cfg.updateGroup("g", () => null))!.id).toBe("g");
|
||||
});
|
||||
|
||||
it("never stores a client revision", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup({ ...group("g"), revision: "abc" });
|
||||
const row = sqlite.query("SELECT data FROM d1_groups").get() as { data: string };
|
||||
expect(JSON.parse(row.data).revision).toBeUndefined();
|
||||
});
|
||||
|
||||
it("propagates D1 write failures instead of pretending to save", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
sqlite.exec("DROP TABLE d1_routes");
|
||||
await expect(cfg.saveGroupRoutes("g", [route("r1", "g")])).rejects.toThrow();
|
||||
});
|
||||
|
||||
it("propagates D1 read failures instead of serving any stale copy", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
await cfg.saveGroupRoutes("g", [route("r1", "g")]);
|
||||
sqlite.exec("DROP TABLE d1_routes");
|
||||
cfg.invalidateCache();
|
||||
await expect(cfg.loadRoutes()).rejects.toThrow();
|
||||
await expect(cfg.loadGroups()).rejects.toThrow();
|
||||
});
|
||||
|
||||
it("gives every group an immutable instance id that survives renames", async () => {
|
||||
const { cfg, sqlite } = setup();
|
||||
sqlite.exec(
|
||||
'INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES (\'legacy\', \'L\', \'{"id":"legacy","name":"L","adminIds":[]}\', 1, 0, 0)',
|
||||
);
|
||||
await cfg.createGroup({ ...group("fresh"), instance: "client-chosen" });
|
||||
const loaded = await cfg.loadGroups({ fresh: true });
|
||||
const legacy = loaded.find((g) => g.id === "legacy");
|
||||
const fresh = loaded.find((g) => g.id === "fresh");
|
||||
expect(legacy!.instance).toMatch(/^[0-9a-f]{32}$/);
|
||||
expect(fresh!.instance).not.toBe("client-chosen");
|
||||
await cfg.updateGroup("fresh", (g) => ({ ...g, instance: "tampered" }));
|
||||
const renamed = await cfg.renameGroup("fresh", "moved");
|
||||
expect(renamed.instance).toBe(fresh!.instance);
|
||||
await cfg.deleteGroup("moved");
|
||||
await cfg.createGroup(group("moved"));
|
||||
const again = (await cfg.loadGroups({ fresh: true })).find((g) => g.id === "moved")!;
|
||||
expect(again.instance).not.toBe(fresh!.instance);
|
||||
});
|
||||
|
||||
it("refuses a delete when the group changed after it was checked", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
await expect(
|
||||
cfg.deleteGroup("g", {
|
||||
expect: async () => {
|
||||
await cfg.updateGroup("g", (x) => ({ ...x, name: "changed" }));
|
||||
},
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => g.name)).toEqual(["changed"]);
|
||||
});
|
||||
|
||||
it("refuses routes for a group that does not exist", async () => {
|
||||
const { cfg } = setup();
|
||||
await expect(cfg.saveGroupRoutes("ghost", [route("r1", "ghost")])).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("kvConfigStore", () => {
|
||||
it("keeps groups and routes in permanent KV keys", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
const cfg = kvConfigStore(kv);
|
||||
await cfg.createGroup(group("a"));
|
||||
await cfg.saveGroupRoutes("a", [route("r1", "a")]);
|
||||
await cfg.renameGroup("a", "b");
|
||||
expect((await cfg.loadGroups()).map((g) => g.id)).toEqual(["b"]);
|
||||
expect((await cfg.loadRoutes()).map((r) => r.groupId)).toEqual(["b"]);
|
||||
await cfg.deleteGroup("b");
|
||||
expect(JSON.parse(store.get("config:routes")!)).toEqual([]);
|
||||
});
|
||||
|
||||
it("repairs orphaned routes before a reused group id can inherit them", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:groups", JSON.stringify([group("current")]));
|
||||
store.set(
|
||||
"config:routes",
|
||||
JSON.stringify([route("orphan", "reused"), route("live", "current")]),
|
||||
);
|
||||
const cfg = kvConfigStore(kv);
|
||||
|
||||
expect((await cfg.loadRoutes()).map((r) => r.id)).toEqual(["live"]);
|
||||
expect(JSON.parse(store.get("config:routes")!)).toEqual([route("live", "current")]);
|
||||
|
||||
await expect(cfg.createGroup(group("reused"))).resolves.toBe(true);
|
||||
expect((await cfg.loadRoutes()).map((r) => r.id)).toEqual(["live"]);
|
||||
});
|
||||
|
||||
it("backfills the same instance whichever cold start wins the race", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:groups", JSON.stringify([group("legacy")]));
|
||||
// Two isolates read the legacy list before either wrote it back.
|
||||
const [a, b] = await Promise.all([
|
||||
kvConfigStore(kv).loadGroups(),
|
||||
kvConfigStore(kv).loadGroups(),
|
||||
]);
|
||||
expect(a[0]!.instance).toBeTruthy();
|
||||
expect(a[0]!.instance).toBe(b[0]!.instance);
|
||||
expect(a[0]!.instanceBackfilled).toBe(true);
|
||||
// A rename keeps the identity.
|
||||
const cfg = kvConfigStore(kv);
|
||||
const moved = await cfg.renameGroup("legacy", "renamed", { transform: (g) => ({ ...g }) });
|
||||
expect(moved.instance).toBe(a[0]!.instance);
|
||||
expect(moved.instanceBackfilled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("d1ConfigStore guarded writes", () => {
|
||||
it("lets only one of two writes from the same base commit", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("g"));
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((r) => (release = r));
|
||||
const slow = cfg.saveGroupRoutes("g", [route("a", "g")], { expect: () => gate });
|
||||
// A second write sneaks in while the first waits between check and write.
|
||||
await cfg.saveGroupRoutes("g", [route("b", "g")]);
|
||||
release();
|
||||
await expect(slow).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await cfg.loadRoutes({ fresh: true })).map((r) => r.id)).toEqual(["b"]);
|
||||
});
|
||||
|
||||
it("refuses a rename whose source changed after it was read", async () => {
|
||||
const { cfg } = setup();
|
||||
await cfg.createGroup(group("old", { name: "v1" }));
|
||||
await expect(
|
||||
cfg.renameGroup("old", "new", {
|
||||
expect: async () => {
|
||||
await cfg.updateGroup("old", (g) => ({ ...g, name: "v2" }));
|
||||
},
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await cfg.loadGroups({ fresh: true })).map((g) => [g.id, g.name])).toEqual([
|
||||
["old", "v2"],
|
||||
]);
|
||||
});
|
||||
|
||||
it("does not seed (nor mark seeded) when the legacy KV read fails", async () => {
|
||||
const { db } = createSqliteD1();
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:routes", JSON.stringify([route("r1", "g1")]));
|
||||
const failing = {
|
||||
...kv,
|
||||
get: async (key: string, type?: string) => {
|
||||
if (key === "config:groups") throw new Error("KV down");
|
||||
return kv.get(key, type as never);
|
||||
},
|
||||
} as unknown as KVNamespace;
|
||||
const cfg = d1ConfigStore(db, failing);
|
||||
await expect(cfg.loadRoutes({ fresh: true })).rejects.toThrow("KV down");
|
||||
expect(store.get("config:d1-seeded")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("fails instead of serving an empty configuration when D1 and the copy are missing", async () => {
|
||||
const { db, sqlite } = createSqliteD1();
|
||||
const { kv } = createMemoryKV();
|
||||
const cfg = d1ConfigStore(db, kv);
|
||||
await cfg.loadRoutes();
|
||||
sqlite.exec("DROP TABLE d1_routes");
|
||||
cfg.invalidateCache();
|
||||
await kv.delete("config:cache:routes");
|
||||
await expect(cfg.loadRoutes()).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { createHmac } from "crypto";
|
||||
import { customProvider } from "../server/lib/providers/custom";
|
||||
import { customProvider, ReplayPendingError, replayNonceKey } from "../server/lib/providers/custom";
|
||||
import { acceptedKey, idempotencyStore } from "../server/lib/lib/idempotency";
|
||||
import type { Env } from "../server/lib/types";
|
||||
|
||||
function createMockKV(): KVNamespace {
|
||||
@@ -60,9 +61,29 @@ describe("custom provider replay protection", () => {
|
||||
"x-webhooker-nonce": nonce,
|
||||
};
|
||||
expect(await customProvider.verify(body, headers, env)).toBe(true);
|
||||
// While the first request is not confirmed as accepted, the nonce is
|
||||
// neither free nor a proven replay.
|
||||
expect(customProvider.verify(body, headers, env)).rejects.toBeInstanceOf(ReplayPendingError);
|
||||
const key = await replayNonceKey(secret, nonce);
|
||||
await idempotencyStore(env.DB, env.KV).claim(acceptedKey(key), 600);
|
||||
expect(await customProvider.verify(body, headers, env)).toBe(false);
|
||||
});
|
||||
|
||||
it("scopes nonces to the signing secret", async () => {
|
||||
const kv = createMockKV();
|
||||
const body = JSON.stringify({ hello: "world" });
|
||||
const nonce = "shared-nonce";
|
||||
for (const secret of ["tenant-a", "tenant-b"]) {
|
||||
const env = { ...createEnv(secret), KV: kv };
|
||||
const headers = {
|
||||
"x-webhooker-signature": replaySignature(secret, now, nonce, body),
|
||||
"x-webhooker-timestamp": String(now),
|
||||
"x-webhooker-nonce": nonce,
|
||||
};
|
||||
expect(await customProvider.verify(body, headers, env)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects a timestamp outside the window", async () => {
|
||||
const secret = "s3cret";
|
||||
const env = createEnv(secret);
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import { readdirSync, readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
/**
|
||||
* An in-memory SQLite database behind the D1 API surface the worker uses
|
||||
* (`prepare` → `bind` → `all`/`first`/`run`, plus `batch`). Every migration
|
||||
* under `migrations/` is applied, and foreign keys are enforced like on D1, so
|
||||
* tests exercise the real SQL instead of a string-matching fake.
|
||||
*/
|
||||
export function createSqliteD1(options: { migrations?: (name: string) => boolean } = {}): {
|
||||
db: D1Database;
|
||||
sqlite: Database;
|
||||
} {
|
||||
const sqlite = new Database(":memory:");
|
||||
sqlite.exec("PRAGMA foreign_keys = ON;");
|
||||
const dir = join(import.meta.dir, "..", "migrations");
|
||||
const files = readdirSync(dir)
|
||||
.filter((f) => f.endsWith(".sql"))
|
||||
.sort();
|
||||
for (const file of files) {
|
||||
if (options.migrations && !options.migrations(file)) continue;
|
||||
sqlite.exec(readFileSync(join(dir, file), "utf8"));
|
||||
}
|
||||
|
||||
interface Stmt {
|
||||
sql: string;
|
||||
args: unknown[];
|
||||
bind: (...args: unknown[]) => Stmt;
|
||||
all: <T>() => Promise<{ results: T[]; success: true; meta: Record<string, unknown> }>;
|
||||
first: <T>(column?: string) => Promise<T | null>;
|
||||
run: () => Promise<{ success: true; meta: { changes: number; last_row_id: number } }>;
|
||||
}
|
||||
|
||||
const normalize = (args: unknown[]): Array<string | number | null> =>
|
||||
args.map((a) =>
|
||||
a === undefined ? null : typeof a === "boolean" ? (a ? 1 : 0) : (a as string | number),
|
||||
);
|
||||
|
||||
function execute(sql: string, args: unknown[]): { rows: unknown[]; changes: number; id: number } {
|
||||
const query = sqlite.query(sql);
|
||||
const bound = normalize(args);
|
||||
if (/^\s*(SELECT|WITH)/i.test(sql) || /\bRETURNING\b/i.test(sql)) {
|
||||
const rows = query.all(...bound);
|
||||
return { rows, changes: 0, id: 0 };
|
||||
}
|
||||
const result = query.run(...bound);
|
||||
return { rows: [], changes: result.changes, id: Number(result.lastInsertRowid) };
|
||||
}
|
||||
|
||||
const prepare = (sql: string): Stmt => {
|
||||
const stmt: Stmt = {
|
||||
sql,
|
||||
args: [],
|
||||
bind(...args: unknown[]): Stmt {
|
||||
return { ...stmt, args };
|
||||
},
|
||||
async all<T>() {
|
||||
return { results: execute(sql, this.args).rows as T[], success: true, meta: {} };
|
||||
},
|
||||
async first<T>(column?: string) {
|
||||
const row = (execute(sql, this.args).rows[0] ?? null) as Record<string, unknown> | null;
|
||||
if (!row) return null;
|
||||
return (column ? row[column] : row) as T;
|
||||
},
|
||||
async run() {
|
||||
const r = execute(sql, this.args);
|
||||
return { success: true, meta: { changes: r.changes, last_row_id: r.id } };
|
||||
},
|
||||
};
|
||||
return stmt;
|
||||
};
|
||||
|
||||
const db = {
|
||||
prepare,
|
||||
async batch(stmts: Stmt[]): Promise<unknown[]> {
|
||||
const results: unknown[] = [];
|
||||
sqlite.exec("BEGIN");
|
||||
try {
|
||||
for (const s of stmts) {
|
||||
const r = execute(s.sql, s.args);
|
||||
results.push({ results: r.rows, success: true, meta: { changes: r.changes } });
|
||||
}
|
||||
sqlite.exec("COMMIT");
|
||||
} catch (err) {
|
||||
sqlite.exec("ROLLBACK");
|
||||
throw err;
|
||||
}
|
||||
return results;
|
||||
},
|
||||
async exec(sql: string): Promise<{ count: number; duration: number }> {
|
||||
sqlite.exec(sql);
|
||||
return { count: 0, duration: 0 };
|
||||
},
|
||||
};
|
||||
return { db: db as unknown as D1Database, sqlite };
|
||||
}
|
||||
|
||||
/** A Map-backed KV namespace with the subset of the API the worker uses. */
|
||||
export function createMemoryKV(): { kv: KVNamespace; store: Map<string, string> } {
|
||||
const store = new Map<string, string>();
|
||||
const kv = {
|
||||
async get(key: string, type?: string | { type?: string }): Promise<unknown> {
|
||||
const v = store.get(key);
|
||||
if (v == null) return null;
|
||||
const t = typeof type === "string" ? type : type?.type;
|
||||
return t === "json" ? JSON.parse(v) : v;
|
||||
},
|
||||
async put(key: string, value: string): Promise<void> {
|
||||
store.set(key, value);
|
||||
},
|
||||
async delete(key: string): Promise<void> {
|
||||
store.delete(key);
|
||||
},
|
||||
async list({ prefix = "" }: { prefix?: string } = {}): Promise<{
|
||||
keys: Array<{ name: string }>;
|
||||
list_complete: boolean;
|
||||
}> {
|
||||
return {
|
||||
keys: [...store.keys()].filter((k) => k.startsWith(prefix)).map((name) => ({ name })),
|
||||
list_complete: true,
|
||||
};
|
||||
},
|
||||
};
|
||||
return { kv: kv as unknown as KVNamespace, store };
|
||||
}
|
||||
+158
-10
@@ -9,7 +9,16 @@ import {
|
||||
kvMessageTracker,
|
||||
messageTracker,
|
||||
} from "../server/lib/lib/message-tracker";
|
||||
import { getDeliveryState, setDeliveryState } from "../server/lib/queue/delivery";
|
||||
import {
|
||||
acquireDelivery,
|
||||
finishLease,
|
||||
getDeliveryState,
|
||||
renewLease,
|
||||
setDeliveryState,
|
||||
} from "../server/lib/queue/delivery";
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
import { chainLock } from "../server/lib/lib/message-tracker";
|
||||
import { recordSendBatch } from "../server/lib/lib/send-log-batch";
|
||||
import { canUseD1 } from "../server/lib/storage/d1";
|
||||
import type { Env } from "../server/lib/types";
|
||||
|
||||
@@ -159,17 +168,20 @@ describe("d1MessageTracker", () => {
|
||||
it("round-trips a message id", async () => {
|
||||
const { db } = createMockD1();
|
||||
const tracker = d1MessageTracker(db);
|
||||
await tracker.set("evt-1", "discord:123", "message-42");
|
||||
await expect(tracker.get("evt-1", "discord:123")).resolves.toBe("message-42");
|
||||
await tracker.set("evt-1", "discord:123", { messageId: "message-42", version: 7 });
|
||||
await expect(tracker.get("evt-1", "discord:123")).resolves.toEqual({
|
||||
messageId: "message-42",
|
||||
version: 7,
|
||||
});
|
||||
await expect(tracker.get("evt-1", "discord:999")).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it("updates on re-set and deletes on delete", async () => {
|
||||
const { db } = createMockD1();
|
||||
const tracker = d1MessageTracker(db);
|
||||
await tracker.set("evt-1", "tg:9", "old");
|
||||
await tracker.set("evt-1", "tg:9", "new");
|
||||
await expect(tracker.get("evt-1", "tg:9")).resolves.toBe("new");
|
||||
await tracker.set("evt-1", "tg:9", { messageId: "old", version: 1 });
|
||||
await tracker.set("evt-1", "tg:9", { messageId: "new", version: 2 });
|
||||
await expect(tracker.get("evt-1", "tg:9")).resolves.toEqual({ messageId: "new", version: 2 });
|
||||
await tracker.delete("evt-1", "tg:9");
|
||||
await expect(tracker.get("evt-1", "tg:9")).resolves.toBeNull();
|
||||
});
|
||||
@@ -227,8 +239,11 @@ describe("factory fallback decision", () => {
|
||||
const db = {} as D1Database;
|
||||
const kv = createMockKV();
|
||||
const tracker = messageTracker(db, kv);
|
||||
await tracker.set("evt-1", "discord:1", "m1");
|
||||
await expect(tracker.get("evt-1", "discord:1")).resolves.toBe("m1");
|
||||
await tracker.set("evt-1", "discord:1", { messageId: "m1", version: 0 });
|
||||
await expect(tracker.get("evt-1", "discord:1")).resolves.toEqual({
|
||||
messageId: "m1",
|
||||
version: 0,
|
||||
});
|
||||
await tracker.delete("evt-1", "discord:1");
|
||||
await expect(tracker.get("evt-1", "discord:1")).resolves.toBeNull();
|
||||
});
|
||||
@@ -240,8 +255,92 @@ describe("factory fallback decision", () => {
|
||||
await expect(idem.has("delivery:github:g1:e1")).resolves.toBe(true);
|
||||
|
||||
const msg = kvMessageTracker(kv);
|
||||
await msg.set("evt-1", "discord:1", "m1");
|
||||
await expect(msg.get("evt-1", "discord:1")).resolves.toBe("m1");
|
||||
await msg.set("evt-1", "discord:1", { messageId: "m1", version: 3 });
|
||||
await expect(msg.get("evt-1", "discord:1")).resolves.toEqual({ messageId: "m1", version: 3 });
|
||||
});
|
||||
|
||||
it("reads message ids stored before versions existed", async () => {
|
||||
const kv = createMockKV();
|
||||
await kv.put("msg:evt-1:discord:1", "legacy-id");
|
||||
await expect(kvMessageTracker(kv).get("evt-1", "discord:1")).resolves.toEqual({
|
||||
messageId: "legacy-id",
|
||||
version: 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("D1 failures degrade to KV", () => {
|
||||
it("idempotency claims fall back to KV when the D1 table is missing", async () => {
|
||||
const { db } = createSqliteD1({
|
||||
migrations: (f) => !f.startsWith("0008") && !f.startsWith("0013"),
|
||||
});
|
||||
const { kv } = createMemoryKV();
|
||||
const store = idempotencyStore(db, kv);
|
||||
await expect(store.claim("k", 120)).resolves.toBe(true);
|
||||
await expect(store.claim("k", 120)).resolves.toBe(false);
|
||||
await store.release("k");
|
||||
await expect(store.claim("k", 120)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it("message tracking falls back to KV when the D1 table is missing", async () => {
|
||||
const { db } = createSqliteD1({
|
||||
migrations: (f) => !f.startsWith("0008") && !f.startsWith("0013"),
|
||||
});
|
||||
const { kv } = createMemoryKV();
|
||||
const tracker = messageTracker(db, kv);
|
||||
await tracker.set("e", "t", { messageId: "m", version: 1 });
|
||||
await expect(tracker.get("e", "t")).resolves.toEqual({ messageId: "m", version: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("acquireDelivery", () => {
|
||||
function env(): Env {
|
||||
return { KV: createMemoryKV().kv, DB: createSqliteD1().db } as Env;
|
||||
}
|
||||
|
||||
it("lets exactly one consumer take a delivery", async () => {
|
||||
const e = env();
|
||||
expect((await acquireDelivery(e, "k")).status).toBe("acquired");
|
||||
expect((await acquireDelivery(e, "k")).status).toBe("busy");
|
||||
});
|
||||
|
||||
it("hands a waiting retry to the next consumer", async () => {
|
||||
const e = env();
|
||||
await acquireDelivery(e, "k");
|
||||
await setDeliveryState(e, "k", "retrying");
|
||||
expect((await acquireDelivery(e, "k")).status).toBe("acquired");
|
||||
});
|
||||
|
||||
it("reports finished deliveries", async () => {
|
||||
const e = env();
|
||||
for (const status of ["delivered", "failed", "dead"] as const) {
|
||||
await setDeliveryState(e, `k-${status}`, status);
|
||||
expect((await acquireDelivery(e, `k-${status}`)).status).toBe("finished");
|
||||
}
|
||||
});
|
||||
|
||||
it("fences a holder whose lease was taken over", async () => {
|
||||
const { db, sqlite } = createSqliteD1();
|
||||
const e = { KV: createMemoryKV().kv, DB: db } as Env;
|
||||
const first = await acquireDelivery(e, "k");
|
||||
sqlite.exec("UPDATE delivery_state SET updated_at = 0");
|
||||
// A takeover happens minutes later in practice; never in the same ms.
|
||||
await Bun.sleep(2);
|
||||
const second = await acquireDelivery(e, "k");
|
||||
expect(second.status).toBe("acquired");
|
||||
// The old holder can neither renew nor overwrite the new holder's outcome.
|
||||
expect(await renewLease(e, "k", first.token)).toBeNull();
|
||||
expect(await finishLease(e, "k", first.token, "delivered")).toBe(false);
|
||||
expect(await finishLease(e, "k", second.token, "delivered")).toBe(true);
|
||||
expect(await getDeliveryState(e, "k")).toBe("delivered");
|
||||
});
|
||||
|
||||
it("takes over a lease whose holder went silent", async () => {
|
||||
const { db, sqlite } = createSqliteD1();
|
||||
const e = { KV: createMemoryKV().kv, DB: db } as Env;
|
||||
await acquireDelivery(e, "k");
|
||||
sqlite.exec("UPDATE delivery_state SET updated_at = 0");
|
||||
expect((await acquireDelivery(e, "k")).status).toBe("acquired");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -259,3 +358,52 @@ describe("delivery state falls back to KV without batch", () => {
|
||||
await expect(getDeliveryState(env, "delivery-state:github:global:nope")).resolves.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("KV fallback transition", () => {
|
||||
it("keeps honoring and releasing claims made in KV before the table existed", async () => {
|
||||
const { db: noTable } = createSqliteD1({
|
||||
migrations: (f) => !f.startsWith("0008") && !f.startsWith("0013"),
|
||||
});
|
||||
const { kv } = createMemoryKV();
|
||||
await idempotencyStore(noTable, kv).claim("k", 600);
|
||||
// The migration is applied afterwards (same KV, a database with the table).
|
||||
const { db } = createSqliteD1();
|
||||
const store = idempotencyStore(db, kv);
|
||||
expect(await store.has("k")).toBe(true);
|
||||
expect(await store.claim("k", 600)).toBe(false);
|
||||
await store.release("k");
|
||||
expect(await store.claim("k", 600)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("chainLock", () => {
|
||||
it("admits one holder at a time and only the holder can release", async () => {
|
||||
const { db } = createSqliteD1();
|
||||
const lock = chainLock(db, createMemoryKV().kv);
|
||||
const token = await lock.acquire("chain", 60);
|
||||
expect(token).not.toBeNull();
|
||||
expect(await lock.acquire("chain", 60)).toBeNull();
|
||||
await lock.release("chain", "999");
|
||||
expect(await lock.acquire("chain", 60)).toBeNull();
|
||||
await lock.release("chain", token!);
|
||||
expect(await lock.acquire("chain", 60)).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("send log group attribution", () => {
|
||||
it("files rows under the id the group instance has at insert time", async () => {
|
||||
const { db, sqlite } = createSqliteD1();
|
||||
sqlite.exec(
|
||||
`INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES ('renamed', 'R', '{"id":"renamed","name":"R","adminIds":[],"instance":"abc"}', 1, 0, 0)`,
|
||||
);
|
||||
const base = { ts: 1, routeId: "r", event: "push", target: "t", ok: true };
|
||||
await recordSendBatch(db, [
|
||||
{ ...base, groupId: "old-id", groupInstance: "abc" },
|
||||
{ ...base, groupId: "deleted", groupInstance: "gone" },
|
||||
]);
|
||||
expect(sqlite.query("SELECT group_id FROM send_logs ORDER BY id").all()).toEqual([
|
||||
{ group_id: "renamed" },
|
||||
{ group_id: null },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,931 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import type { H3Event } from "h3";
|
||||
import {
|
||||
adminApiGroupsGet,
|
||||
adminApiGroupsPut,
|
||||
adminGroupCreate,
|
||||
adminGroupDelete,
|
||||
adminGroupFragmentsGet,
|
||||
adminGroupFragmentsPut,
|
||||
adminGroupInvitesPost,
|
||||
adminInviteDelete,
|
||||
adminGroupRename,
|
||||
adminGroupRoutesGet,
|
||||
adminGroupRoutesPut,
|
||||
adminGroupUpdate,
|
||||
adminGroupWebhookRegenerate,
|
||||
channelFreeGuard,
|
||||
roleGuard,
|
||||
} from "../server/lib/web/admin";
|
||||
import { ConfigConflictError } from "../server/lib/storage/config-store";
|
||||
import { createAdminSession, adminCookie } from "../server/lib/web/session";
|
||||
import {
|
||||
createGroup,
|
||||
ensureInstallationGroup,
|
||||
loadGroups,
|
||||
updateGroup,
|
||||
} from "../server/lib/web/groups";
|
||||
import {
|
||||
initConfigStore,
|
||||
loadRoutes,
|
||||
resetConfigStore,
|
||||
saveGroupRoutes,
|
||||
} from "../server/lib/config";
|
||||
import { saveToken, findUserIdByToken } from "../server/lib/github/store";
|
||||
import { makeEvent, responseStatus } from "./helpers";
|
||||
import { resolveGroupAlias } from "../server/lib/web/group-lifecycle";
|
||||
import { acceptInvite, getInvite } from "../server/lib/web/invites";
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
import type { Env, Group, Route } from "../server/lib/types";
|
||||
import type { Database } from "bun:sqlite";
|
||||
|
||||
let env: Env;
|
||||
let sqlite: Database;
|
||||
|
||||
function group(id: string, owner: string, extra: Partial<Group> = {}): Group {
|
||||
return { id, name: id, adminIds: [owner], members: [{ login: owner, role: "owner" }], ...extra };
|
||||
}
|
||||
|
||||
function route(id: string, extra: Partial<Route> = {}): Route {
|
||||
return {
|
||||
id,
|
||||
name: id,
|
||||
enabled: true,
|
||||
filters: [{ type: "event", match: "push" }],
|
||||
targets: [{ platform: "discord", channelId: "1" }],
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
async function cookieFor(userId: string, login: string): Promise<string> {
|
||||
return adminCookie(await createAdminSession(env.KV, userId, login));
|
||||
}
|
||||
|
||||
/** Delete a group the way the console does: confirmed on its shown revisions. */
|
||||
async function deleteAs(cookie: string, id: string): Promise<void> {
|
||||
const routes = (await adminGroupRoutesGet(
|
||||
call(`/admin/api/groups/${id}/routes`, cookie),
|
||||
id,
|
||||
)) as {
|
||||
revision: string;
|
||||
version: number;
|
||||
};
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Array<{ id: string; revision: string }>;
|
||||
};
|
||||
const rev = listed.groups.find((g) => g.id === id)!.revision;
|
||||
const del = call(
|
||||
`/admin/api/groups/${id}?revision=${rev}&routesRevision=${routes.revision}&version=${routes.version}`,
|
||||
cookie,
|
||||
"DELETE",
|
||||
);
|
||||
await adminGroupDelete(del, id);
|
||||
expect(responseStatus(del)).toBe(200);
|
||||
}
|
||||
|
||||
function call(path: string, cookie: string, method = "GET", body?: unknown): H3Event {
|
||||
return makeEvent(path, {
|
||||
method,
|
||||
headers: { cookie, "content-type": "application/json" },
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
env,
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
const d1 = createSqliteD1();
|
||||
sqlite = d1.sqlite;
|
||||
const { kv } = createMemoryKV();
|
||||
env = { GITHUB_WEBHOOK_SECRET: "s", KV: kv, DB: d1.db, ADMIN_USER_IDS: "1" };
|
||||
resetConfigStore(kv);
|
||||
initConfigStore(env);
|
||||
await createGroup(kv, group("alpha", "alice"));
|
||||
await createGroup(
|
||||
kv,
|
||||
group("beta", "bob", {
|
||||
members: [
|
||||
{ login: "bob", role: "owner" },
|
||||
{ login: "alice", role: "viewer" },
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe("delivery targets", () => {
|
||||
it("never lets a tenant aim its routes at a channel another group delivers to", async () => {
|
||||
await saveGroupRoutes(env.KV, "beta", [
|
||||
route("b1", { groupId: "beta", targets: [{ platform: "discord", channelId: "beta-ch" }] }),
|
||||
]);
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const hijack = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "beta-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(hijack, "alpha");
|
||||
expect(responseStatus(hijack)).toBe(403);
|
||||
// Nor through another spelling of the same channel (a URL path alias).
|
||||
const alias = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "x/../beta-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(alias, "alpha");
|
||||
expect(responseStatus(alias)).toBe(400);
|
||||
for (const target of [
|
||||
{ platform: "telegram", chatId: "-0100" },
|
||||
// Wraps around to an existing chat in Telegram's 64-bit parsing.
|
||||
{ platform: "telegram", chatId: "-18446744073709652393" },
|
||||
{ platform: "discord", channelId: "0123" },
|
||||
]) {
|
||||
const padded = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [target as never] })],
|
||||
});
|
||||
await adminGroupRoutesPut(padded, "alpha");
|
||||
expect(responseStatus(padded)).toBe(400);
|
||||
}
|
||||
const username = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "telegram", chatId: "@beta_channel" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(username, "alpha");
|
||||
expect(responseStatus(username)).toBe(400);
|
||||
// A channel nobody else uses is fine; so is one the writer also manages.
|
||||
const own = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "alpha-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(own, "alpha");
|
||||
expect(responseStatus(own)).toBe(200);
|
||||
const root = await cookieFor("1", "root");
|
||||
const shared = call("/admin/api/groups/alpha/routes", root, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "beta-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(shared, "alpha");
|
||||
expect(responseStatus(shared)).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("shared and aliased targets", () => {
|
||||
it("compares a stored Telegram @username by the chat id it names", async () => {
|
||||
env.TELEGRAM_TOKEN = "bot";
|
||||
// Stored before only numeric ids were accepted.
|
||||
await saveGroupRoutes(env.KV, "beta", [
|
||||
route("b1", { groupId: "beta", targets: [{ platform: "telegram", chatId: "@beta_chat" }] }),
|
||||
]);
|
||||
const realFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async (input: unknown) => {
|
||||
expect(String(input)).toContain("getChat?chat_id=%40beta_chat");
|
||||
return new Response(JSON.stringify({ ok: true, result: { id: -100777 } }), { status: 200 });
|
||||
}) as unknown as typeof fetch;
|
||||
try {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const same = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "telegram", chatId: "-100777" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(same, "alpha");
|
||||
expect(responseStatus(same)).toBe(403);
|
||||
} finally {
|
||||
globalThis.fetch = realFetch;
|
||||
}
|
||||
});
|
||||
|
||||
it("looks up no Telegram usernames for a save without Telegram targets", async () => {
|
||||
env.TELEGRAM_TOKEN = "bot";
|
||||
await saveGroupRoutes(env.KV, "beta", [
|
||||
route("b1", { groupId: "beta", targets: [{ platform: "telegram", chatId: "@beta_chat" }] }),
|
||||
]);
|
||||
let lookups = 0;
|
||||
const realFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () => {
|
||||
lookups++;
|
||||
return new Response(JSON.stringify({ ok: true, result: { id: -100777 } }), { status: 200 });
|
||||
}) as unknown as typeof fetch;
|
||||
try {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const discord = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "alpha-ch" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(discord, "alpha");
|
||||
expect(responseStatus(discord)).toBe(200);
|
||||
} finally {
|
||||
globalThis.fetch = realFetch;
|
||||
}
|
||||
expect(lookups).toBe(0);
|
||||
});
|
||||
|
||||
it("keeps a channel shared from older data only where it already was", async () => {
|
||||
const shared = [{ platform: "discord" as const, channelId: "shared-ch" }];
|
||||
await saveGroupRoutes(env.KV, "alpha", [route("a1", { groupId: "alpha", targets: shared })]);
|
||||
await saveGroupRoutes(env.KV, "beta", [route("b1", { groupId: "beta", targets: shared })]);
|
||||
const alice = await cookieFor("2", "alice");
|
||||
// Unchanged: fine.
|
||||
const same = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: shared })],
|
||||
});
|
||||
await adminGroupRoutesPut(same, "alpha");
|
||||
expect(responseStatus(same)).toBe(200);
|
||||
// Another topic/thread of the shared channel on the same route: refused.
|
||||
const thread = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [
|
||||
route("a1", {
|
||||
targets: [...shared, { platform: "discord", channelId: "shared-ch", threadId: "t2" }],
|
||||
}),
|
||||
],
|
||||
});
|
||||
await adminGroupRoutesPut(thread, "alpha");
|
||||
expect(responseStatus(thread)).toBe(403);
|
||||
// A new route sending more into the shared channel: refused.
|
||||
const more = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: shared }), route("a2", { targets: shared })],
|
||||
});
|
||||
await adminGroupRoutesPut(more, "alpha");
|
||||
expect(responseStatus(more)).toBe(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe("webhook secrets", () => {
|
||||
it("keeps the secret with the group, re-checked at commit and never sent to clients", async () => {
|
||||
const { rotateTenantSecret, getTenantSecret } = await import("../server/lib/web/tenants");
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const rotate = call("/admin/api/groups/alpha/webhook/regenerate", alice, "POST");
|
||||
const { secret } = (await adminGroupWebhookRegenerate(rotate, "alpha")) as { secret: string };
|
||||
expect(secret).toMatch(/^[0-9a-f]{64}$/);
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(await getTenantSecret(env.KV, stored)).toBe(secret);
|
||||
// Never in what clients read.
|
||||
const listed = await adminApiGroupsGet(call("/admin/api/groups", alice));
|
||||
expect(JSON.stringify(listed)).not.toContain(secret);
|
||||
// A rotation whose writer lost the right meanwhile changes nothing.
|
||||
await expect(
|
||||
rotateTenantSecret(env.KV, "alpha", () => {
|
||||
throw new Error("no longer an owner");
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
const after = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(after.webhookSecret).toBe(secret);
|
||||
// A plain group edit keeps it.
|
||||
await updateGroup(env.KV, "alpha", (g) => ({
|
||||
...g,
|
||||
name: "Renamed",
|
||||
webhookSecret: undefined,
|
||||
}));
|
||||
const edited = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(edited.webhookSecret).toBe(secret);
|
||||
});
|
||||
|
||||
it("creates groups with their members bound to their accounts", async () => {
|
||||
await saveToken(env.KV, "1", "root-token", 3600);
|
||||
const realFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async () =>
|
||||
new Response(JSON.stringify({ id: 77, login: "bob" }), {
|
||||
status: 200,
|
||||
})) as unknown as typeof fetch;
|
||||
try {
|
||||
const root = await cookieFor("1", "root");
|
||||
const create = call("/admin/api/groups", root, "POST", { group: group("delta", "bob") });
|
||||
await adminGroupCreate(create);
|
||||
expect(responseStatus(create)).toBe(200);
|
||||
} finally {
|
||||
globalThis.fetch = realFetch;
|
||||
}
|
||||
const delta = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "delta")!;
|
||||
expect(delta.members).toEqual([{ login: "bob", role: "owner", userId: "77" }]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sources and links", () => {
|
||||
it("accepts and keeps the custom source restriction", async () => {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", alice))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const alpha = listed.groups.find((g) => g.id === "alpha")!;
|
||||
const save = call("/admin/api/groups/alpha", alice, "PUT", {
|
||||
group: { ...alpha, providers: ["github", "custom"] },
|
||||
});
|
||||
await adminGroupUpdate(save, "alpha");
|
||||
expect(responseStatus(save)).toBe(200);
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(stored.providers).toEqual(["github", "custom"]);
|
||||
});
|
||||
|
||||
it("uses an account-link confirmation once", async () => {
|
||||
const { handleLinkConfirm } = await import("../server/lib/web/oauth");
|
||||
const nonce = "0123456789abcdef0123456789abcdef";
|
||||
const link = JSON.stringify({
|
||||
platform: "discord",
|
||||
userId: "d1",
|
||||
githubUserId: "42",
|
||||
login: "alice",
|
||||
});
|
||||
await env.KV.put(`link:${nonce}`, link);
|
||||
const confirm = (): H3Event =>
|
||||
makeEvent("/auth/github/link", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ nonce }),
|
||||
env,
|
||||
});
|
||||
await handleLinkConfirm(confirm());
|
||||
// Replayed later (a stale KV copy elsewhere still has the confirmation).
|
||||
await env.KV.put(`link:${nonce}`, link);
|
||||
const again = confirm();
|
||||
await handleLinkConfirm(again);
|
||||
expect(responseStatus(again)).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("repairing unreadable routes", () => {
|
||||
it("lets an admin explicitly replace a group whose stored route cannot be read", async () => {
|
||||
await saveGroupRoutes(env.KV, "alpha", [route("broken", { groupId: "alpha" })]);
|
||||
sqlite.exec("UPDATE d1_routes SET targets = '{oops' WHERE id = 'broken'");
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const shown = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", alice),
|
||||
"alpha",
|
||||
)) as { unreadable: boolean };
|
||||
expect(shown.unreadable).toBe(true);
|
||||
// A plain save is refused (the unseen row would be lost)...
|
||||
const plain = call("/admin/api/groups/alpha/routes", alice, "PUT", { routes: [route("a1")] });
|
||||
await adminGroupRoutesPut(plain, "alpha");
|
||||
expect(responseStatus(plain)).not.toBe(200);
|
||||
// ...an explicit repair goes through.
|
||||
const repair = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1")],
|
||||
discardUnreadable: true,
|
||||
});
|
||||
await adminGroupRoutesPut(repair, "alpha");
|
||||
expect(responseStatus(repair)).toBe(200);
|
||||
expect(sqlite.query("SELECT id FROM d1_routes WHERE group_id = 'alpha'").all()).toEqual([
|
||||
{ id: "a1" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("authorization at commit time", () => {
|
||||
it("refuses a route write by someone removed from the group meanwhile", async () => {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const write = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { targets: [{ platform: "discord", channelId: "fresh" }] })],
|
||||
});
|
||||
// Authorized now (the request's access check runs and is kept)...
|
||||
const { requireAnyAccess } = await import("../server/lib/web/auth");
|
||||
await requireAnyAccess(write);
|
||||
// ...but an owner removes alice before the write lands.
|
||||
await updateGroup(env.KV, "alpha", (g) => ({
|
||||
...g,
|
||||
members: [{ login: "carol", role: "owner" }],
|
||||
adminIds: ["carol"],
|
||||
}));
|
||||
// The write itself re-checks the role at commit.
|
||||
await expect(
|
||||
saveGroupRoutes(env.KV, "alpha", [route("a1", { groupId: "alpha" })], {
|
||||
guards: roleGuard(write, "alpha", "admin"),
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
expect((await loadRoutes(env.KV, { fresh: true })).some((r) => r.id === "a1")).toBe(false);
|
||||
});
|
||||
|
||||
it("voids an invite whose issuing owner was removed before it was used", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
await updateGroup(env.KV, "alpha", (g) => ({
|
||||
...g,
|
||||
members: [{ login: "carol", role: "owner" }],
|
||||
adminIds: ["carol"],
|
||||
}));
|
||||
expect(await acceptInvite(env, created.token, "9", "mallory")).toEqual({
|
||||
ok: false,
|
||||
reason: "invalid",
|
||||
});
|
||||
});
|
||||
|
||||
it("refuses a filter node that mixes condition kinds", async () => {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const mixed = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [route("a1", { ast: { all: 0, any: [{ type: "event", match: "push" }] } as never })],
|
||||
});
|
||||
await adminGroupRoutesPut(mixed, "alpha");
|
||||
expect(responseStatus(mixed)).toBe(400);
|
||||
});
|
||||
|
||||
it("refuses a glob longer than the matcher can run", async () => {
|
||||
const alice = await cookieFor("2", "alice");
|
||||
const long = call("/admin/api/groups/alpha/routes", alice, "PUT", {
|
||||
routes: [
|
||||
route("a1", { filters: [{ type: "branch", match: `${"a".repeat(201)}*`, exclude: true }] }),
|
||||
],
|
||||
});
|
||||
await adminGroupRoutesPut(long, "alpha");
|
||||
expect(responseStatus(long)).toBe(400);
|
||||
});
|
||||
|
||||
it("never accepts an invite revoked after it was read", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
const { revokeInvite } = await import("../server/lib/web/invites");
|
||||
// Revoked with its D1 record; the KV copy another isolate may still read
|
||||
// is put back to stand for that stale read.
|
||||
const raw = await env.KV.get(`invite:${created.token}`);
|
||||
await revokeInvite(env.KV, created.token, env.DB);
|
||||
await env.KV.put(`invite:${created.token}`, raw!);
|
||||
expect(await acceptInvite(env, created.token, "9", "mallory")).toEqual({
|
||||
ok: false,
|
||||
reason: "invalid",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("delivery target claims at commit time", () => {
|
||||
it("aborts a write whose new channel another group took meanwhile", async () => {
|
||||
// The pre-check passed; then beta committed a route and a log target.
|
||||
await saveGroupRoutes(env.KV, "beta", [
|
||||
route("b1", { groupId: "beta", targets: [{ platform: "telegram", chatId: "-100" }] }),
|
||||
]);
|
||||
await updateGroup(env.KV, "beta", (g) => ({
|
||||
...g,
|
||||
logTarget: { platform: "discord", channelId: "log-ch" },
|
||||
}));
|
||||
for (const channel of ["telegram:-100", "discord:log-ch"]) {
|
||||
await expect(
|
||||
saveGroupRoutes(env.KV, "alpha", [route("a1", { groupId: "alpha" })], {
|
||||
guards: (db) => [channelFreeGuard(db, channel, ["alpha"])],
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ConfigConflictError);
|
||||
}
|
||||
// Same platform id on another platform, or a group the writer may edit: free.
|
||||
await saveGroupRoutes(env.KV, "alpha", [route("a1", { groupId: "alpha" })], {
|
||||
guards: (db) => [
|
||||
channelFreeGuard(db, "discord:-100", ["alpha"]),
|
||||
channelFreeGuard(db, "telegram:-100", ["alpha", "beta"]),
|
||||
],
|
||||
});
|
||||
expect((await loadRoutes(env.KV, { fresh: true })).some((r) => r.id === "a1")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("member binding", () => {
|
||||
it("binds a member added by login to the account holding it now", async () => {
|
||||
await saveToken(env.KV, "2", "alice-token", 3600);
|
||||
const realFetch = globalThis.fetch;
|
||||
globalThis.fetch = (async (input: unknown) => {
|
||||
expect(String(input)).toBe("https://api.github.com/users/bob");
|
||||
return new Response(JSON.stringify({ id: 99, login: "bob" }), { status: 200 });
|
||||
}) as unknown as typeof fetch;
|
||||
try {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const alpha = listed.groups.find((g) => g.id === "alpha")!;
|
||||
const event = call("/admin/api/groups/alpha", cookie, "PUT", {
|
||||
group: { ...alpha, members: [...alpha.members!, { login: "bob", role: "viewer" }] },
|
||||
});
|
||||
await adminGroupUpdate(event, "alpha");
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
} finally {
|
||||
globalThis.fetch = realFetch;
|
||||
}
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(stored.members).toContainEqual({ login: "bob", role: "viewer", userId: "99" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("group writes", () => {
|
||||
it("an owner of one group can update it while viewing another", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const alpha = listed.groups.find((g) => g.id === "alpha")!;
|
||||
const event = call("/admin/api/groups/alpha", cookie, "PUT", {
|
||||
group: { ...alpha, name: "Alpha!" },
|
||||
});
|
||||
await adminGroupUpdate(event, "alpha");
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
expect((await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!.name).toBe(
|
||||
"Alpha!",
|
||||
);
|
||||
|
||||
// The legacy bulk API accepts the visible-but-not-owned group unchanged.
|
||||
const again = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const bulk = call("/admin/api/groups", cookie, "PUT", { groups: again.groups });
|
||||
await adminApiGroupsPut(bulk);
|
||||
expect(responseStatus(bulk)).toBe(200);
|
||||
});
|
||||
|
||||
it("refuses an update built on a stale copy", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
const alpha = listed.groups.find((g) => g.id === "alpha")!;
|
||||
const first = call("/admin/api/groups/alpha", cookie, "PUT", {
|
||||
group: { ...alpha, name: "first" },
|
||||
});
|
||||
await adminGroupUpdate(first, "alpha");
|
||||
const second = call("/admin/api/groups/alpha", cookie, "PUT", {
|
||||
group: { ...alpha, lang: "zh" },
|
||||
});
|
||||
await adminGroupUpdate(second, "alpha");
|
||||
expect(responseStatus(second)).toBe(409);
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(stored.name).toBe("first");
|
||||
expect(stored.lang).toBeUndefined();
|
||||
});
|
||||
|
||||
it("owners cannot change the source-isolation fields", async () => {
|
||||
await createGroup(env.KV, group("u-2", "alice", { isolated: true, installationId: 5 }));
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const event = call("/admin/api/groups/u-2", cookie, "PUT", {
|
||||
group: { ...group("u-2", "alice"), owners: [], isolated: false, installationId: 99 },
|
||||
});
|
||||
await adminGroupUpdate(event, "u-2");
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "u-2")!;
|
||||
expect(stored.isolated).toBe(true);
|
||||
expect(stored.installationId).toBe(5);
|
||||
});
|
||||
|
||||
it("a stale bulk save no longer reverts or drops another group", async () => {
|
||||
const admin = await cookieFor("1", "root");
|
||||
const snapshot = (await adminApiGroupsGet(call("/admin/api/groups", admin))) as {
|
||||
groups: Group[];
|
||||
};
|
||||
// Someone else edits beta and creates gamma afterwards.
|
||||
const bob = await cookieFor("3", "bob");
|
||||
const beta = snapshot.groups.find((g) => g.id === "beta")!;
|
||||
await adminGroupUpdate(
|
||||
call("/admin/api/groups/beta", bob, "PUT", { group: { ...beta, name: "Beta v2" } }),
|
||||
"beta",
|
||||
);
|
||||
// The super admin saves an alpha change from the old snapshot (old beta).
|
||||
const edited = snapshot.groups.map((g) => (g.id === "alpha" ? { ...g, lang: "zh" } : g));
|
||||
const bulk = call("/admin/api/groups", admin, "PUT", { groups: edited });
|
||||
await adminApiGroupsPut(bulk);
|
||||
expect(responseStatus(bulk)).toBe(200);
|
||||
const stored = await loadGroups(env.KV, { fresh: true });
|
||||
expect(stored.find((g) => g.id === "alpha")!.lang).toBe("zh");
|
||||
// beta came back unedited from the old snapshot: bob's change survives.
|
||||
expect(stored.find((g) => g.id === "beta")!.name).toBe("Beta v2");
|
||||
});
|
||||
|
||||
it("a bulk save based on an outdated list is refused", async () => {
|
||||
const admin = await cookieFor("1", "root");
|
||||
const snapshot = (await adminApiGroupsGet(call("/admin/api/groups", admin))) as {
|
||||
groups: Group[];
|
||||
revision: string;
|
||||
};
|
||||
await adminGroupCreate(
|
||||
call("/admin/api/groups", admin, "POST", { group: group("gamma", "carol") }),
|
||||
);
|
||||
const bulk = call("/admin/api/groups", admin, "PUT", {
|
||||
groups: snapshot.groups,
|
||||
revision: snapshot.revision,
|
||||
});
|
||||
await adminApiGroupsPut(bulk);
|
||||
expect(responseStatus(bulk)).toBe(409);
|
||||
expect((await loadGroups(env.KV, { fresh: true })).map((g) => g.id)).toContain("gamma");
|
||||
});
|
||||
|
||||
it("never deletes what was added to a group after its deletion was confirmed", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const shown = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", cookie),
|
||||
"alpha",
|
||||
)) as { revision: string; version: number };
|
||||
const groups = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Array<{ id: string; revision: string }>;
|
||||
};
|
||||
const rev = groups.groups.find((g) => g.id === "alpha")!.revision;
|
||||
// A filter fragment saved meanwhile (the routes are unchanged).
|
||||
const frag = call("/admin/api/groups/alpha/fragments", cookie, "PUT", {
|
||||
fragments: [{ id: "f1", name: "F", node: { type: "event", match: "push" } }],
|
||||
});
|
||||
await adminGroupFragmentsPut(frag, "alpha");
|
||||
expect(responseStatus(frag)).toBe(200);
|
||||
const del = call(
|
||||
`/admin/api/groups/alpha?revision=${rev}&routesRevision=${shown.revision}&version=${shown.version}`,
|
||||
cookie,
|
||||
"DELETE",
|
||||
);
|
||||
await adminGroupDelete(del, "alpha");
|
||||
expect(responseStatus(del)).toBe(409);
|
||||
});
|
||||
|
||||
it("deleting a group detaches its history and retires its id", async () => {
|
||||
sqlite.exec(
|
||||
"INSERT INTO send_logs (ts, route_id, group_id, event, target, ok) VALUES (1, 'r', 'alpha', 'push', 't', 1)",
|
||||
);
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
// Confirmed on the group and its routes as shown.
|
||||
const shown = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", cookie),
|
||||
"alpha",
|
||||
)) as { revision: string; version: number };
|
||||
const groups = (await adminApiGroupsGet(call("/admin/api/groups", cookie))) as {
|
||||
groups: Array<{ id: string; revision: string }>;
|
||||
};
|
||||
const rev = groups.groups.find((g) => g.id === "alpha")!.revision;
|
||||
const del = call(
|
||||
`/admin/api/groups/alpha?revision=${rev}&routesRevision=${shown.revision}&version=${shown.version}`,
|
||||
cookie,
|
||||
"DELETE",
|
||||
);
|
||||
await adminGroupDelete(del, "alpha");
|
||||
expect(responseStatus(del)).toBe(200);
|
||||
expect(sqlite.query("SELECT group_id FROM send_logs").get()).toEqual({ group_id: null });
|
||||
|
||||
const admin = await cookieFor("1", "root");
|
||||
const recreate = call("/admin/api/groups", admin, "POST", { group: group("alpha", "eve") });
|
||||
await adminGroupCreate(recreate);
|
||||
expect(responseStatus(recreate)).toBe(409);
|
||||
});
|
||||
|
||||
it("renaming moves routes and history in one go", async () => {
|
||||
sqlite.exec(
|
||||
"INSERT INTO send_logs (ts, route_id, group_id, event, target, ok) VALUES (1, 'r', 'alpha', 'push', 't', 1)",
|
||||
);
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
await adminGroupRoutesPut(
|
||||
call("/admin/api/groups/alpha/routes", cookie, "PUT", { routes: [route("r1")] }),
|
||||
"alpha",
|
||||
);
|
||||
const event = call("/admin/api/groups/alpha/rename", cookie, "PUT", { newId: "omega" });
|
||||
await adminGroupRename(event, "alpha");
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
expect((await loadRoutes(env.KV, { fresh: true })).map((r) => r.groupId)).toEqual(["omega"]);
|
||||
expect(sqlite.query("SELECT group_id FROM send_logs").get()).toEqual({ group_id: "omega" });
|
||||
expect(await resolveGroupAlias(env, "alpha")).toBe("omega");
|
||||
});
|
||||
});
|
||||
|
||||
describe("route writes", () => {
|
||||
it("refuses a save built on routes changed meanwhile", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const loaded = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", cookie),
|
||||
"alpha",
|
||||
)) as { revision: string };
|
||||
const first = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("a")],
|
||||
revision: loaded.revision,
|
||||
});
|
||||
const saved = (await adminGroupRoutesPut(first, "alpha")) as { revision: string };
|
||||
expect(responseStatus(first)).toBe(200);
|
||||
const stale = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("b")],
|
||||
revision: loaded.revision,
|
||||
});
|
||||
await adminGroupRoutesPut(stale, "alpha");
|
||||
expect(responseStatus(stale)).toBe(409);
|
||||
const fresh = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("a"), route("b")],
|
||||
revision: saved.revision,
|
||||
});
|
||||
await adminGroupRoutesPut(fresh, "alpha");
|
||||
expect(responseStatus(fresh)).toBe(200);
|
||||
});
|
||||
|
||||
it("rejects regexes RE2 cannot run and non-snowflake role ids", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const backref = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("x", { filters: [{ type: "keyword", match: "/(a)\\1/" }] })],
|
||||
});
|
||||
await adminGroupRoutesPut(backref, "alpha");
|
||||
expect(responseStatus(backref)).toBe(400);
|
||||
const mention = call("/admin/api/groups/alpha/routes", cookie, "PUT", {
|
||||
routes: [route("x", { discordRoleIds: ["1> @everyone <@&2"] })],
|
||||
});
|
||||
await adminGroupRoutesPut(mention, "alpha");
|
||||
expect(responseStatus(mention)).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("fragment writes", () => {
|
||||
it("replaces only the group's own fragments and detects stale saves", async () => {
|
||||
sqlite.exec(
|
||||
"INSERT INTO d1_fragments (id, group_id, name, node, created_at, updated_at) VALUES ('keep', 'beta', 'K', '{\"type\":\"event\",\"match\":\"push\"}', 1, 1)",
|
||||
);
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const loaded = (await adminGroupFragmentsGet(
|
||||
call("/admin/api/groups/alpha/fragments", cookie),
|
||||
"alpha",
|
||||
)) as { revision: string };
|
||||
const node = { type: "event", match: "push" };
|
||||
const save = call("/admin/api/groups/alpha/fragments", cookie, "PUT", {
|
||||
fragments: [{ id: "f1", name: "F1", node }],
|
||||
revision: loaded.revision,
|
||||
});
|
||||
await adminGroupFragmentsPut(save, "alpha");
|
||||
expect(responseStatus(save)).toBe(200);
|
||||
const stale = call("/admin/api/groups/alpha/fragments", cookie, "PUT", {
|
||||
fragments: [],
|
||||
revision: loaded.revision,
|
||||
});
|
||||
await adminGroupFragmentsPut(stale, "alpha");
|
||||
expect(responseStatus(stale)).toBe(409);
|
||||
expect(sqlite.query("SELECT group_id, id FROM d1_fragments ORDER BY group_id").all()).toEqual([
|
||||
{ group_id: "alpha", id: "f1" },
|
||||
{ group_id: "beta", id: "keep" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("token rotation", () => {
|
||||
it("stops an old token from authenticating after re-authorization", async () => {
|
||||
await saveToken(env.KV, "42", "old-token", 3600);
|
||||
expect(await findUserIdByToken(env.KV, "old-token")).toBe("42");
|
||||
await saveToken(env.KV, "42", "new-token", 3600);
|
||||
expect(await findUserIdByToken(env.KV, "old-token")).toBeNull();
|
||||
expect(await findUserIdByToken(env.KV, "new-token")).toBe("42");
|
||||
});
|
||||
|
||||
it("rejects a token whose reverse key outlived its replacement", async () => {
|
||||
await saveToken(env.KV, "42", "old-token", 3600);
|
||||
const reverse = [...(await env.KV.list({ prefix: "token-reverse:" })).keys][0]!.name;
|
||||
const value = await env.KV.get(reverse);
|
||||
await saveToken(env.KV, "42", "new-token", 3600);
|
||||
await env.KV.put(reverse, value!);
|
||||
expect(await findUserIdByToken(env.KV, "old-token")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("reserved and squatted installation ids", () => {
|
||||
it("does not let owners rename into the system id space", async () => {
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const event = call("/admin/api/groups/alpha/rename", cookie, "PUT", { newId: "inst-555" });
|
||||
await adminGroupRename(event, "alpha");
|
||||
expect(responseStatus(event)).toBe(400);
|
||||
});
|
||||
|
||||
it("never binds an installation to an unbound group that merely has its name", async () => {
|
||||
await createGroup(env.KV, group("inst-777", "mallory"));
|
||||
const bound = await ensureInstallationGroup(env.KV, 777, "victim-org");
|
||||
expect(bound).toBeNull();
|
||||
const stored = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "inst-777")!;
|
||||
expect(stored.installationId).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("round-2 regressions", () => {
|
||||
it("keeps a legacy personal group isolated when it is renamed", async () => {
|
||||
sqlite.exec(
|
||||
`INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES ('u-42', 'me', '{"id":"u-42","name":"me","adminIds":["alice"],"members":[{"login":"alice","role":"owner"}]}', 1, 0, 0)`,
|
||||
);
|
||||
const cookie = await cookieFor("2", "alice");
|
||||
const event = call("/admin/api/groups/u-42/rename", cookie, "PUT", { newId: "team" });
|
||||
await adminGroupRename(event, "u-42");
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
const team = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "team")!;
|
||||
expect(team.isolated).toBe(true);
|
||||
});
|
||||
|
||||
it("never lets an old invite join a group re-created under the same id", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
await deleteAs(owner, "alpha");
|
||||
// The invite's KV record survived (as if its cleanup had failed).
|
||||
await env.KV.put(
|
||||
`invite:${created.token}`,
|
||||
JSON.stringify({
|
||||
groupId: "alpha",
|
||||
role: "admin",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "alice",
|
||||
groupInstance: "gone",
|
||||
}),
|
||||
);
|
||||
sqlite.exec("DELETE FROM dedup_keys");
|
||||
await createGroup(env.KV, group("alpha", "eve"));
|
||||
const result = await acceptInvite(env, created.token, "9", "mallory");
|
||||
expect(result).toEqual({ ok: false, reason: "invalid" });
|
||||
});
|
||||
|
||||
it("never lets a second person in on an invite whose first use committed unseen", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
// The first acceptance committed, but its answer was lost: its claim was
|
||||
// given back while the "used" record stayed.
|
||||
sqlite.exec(
|
||||
`INSERT INTO dedup_keys (key, claimed_at, expires_at) VALUES ('accepted:invite-used:${created.token}', 0, ${Date.now() + 86400_000})`,
|
||||
);
|
||||
expect(await acceptInvite(env, created.token, "9", "mallory")).toEqual({
|
||||
ok: false,
|
||||
reason: "invalid",
|
||||
});
|
||||
const alpha = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "alpha")!;
|
||||
expect(alpha.members!.some((m) => m.login === "mallory")).toBe(false);
|
||||
});
|
||||
|
||||
it("never records an invite as used by a member write that changed nothing", async () => {
|
||||
// What the "used" record is conditioned on: the write right before it.
|
||||
await env.DB.batch([
|
||||
env.DB.prepare(
|
||||
"UPDATE d1_groups SET version = version + 1 WHERE id = ? AND version = ?",
|
||||
).bind("alpha", -1),
|
||||
env.DB.prepare(
|
||||
"INSERT INTO dedup_keys (key, claimed_at, expires_at) SELECT 'probe', 0, 1 WHERE changes() > 0",
|
||||
),
|
||||
]);
|
||||
expect(sqlite.query("SELECT COUNT(*) AS n FROM dedup_keys WHERE key = 'probe'").get()).toEqual({
|
||||
n: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("confirms the invite as used in the transaction that adds the member", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "viewer" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
const result = await acceptInvite(env, created.token, "9", "mallory");
|
||||
expect(result).toMatchObject({ ok: true, groupId: "alpha" });
|
||||
expect(
|
||||
sqlite
|
||||
.query("SELECT COUNT(*) AS n FROM dedup_keys WHERE key = ?")
|
||||
.get(`accepted:invite-used:${created.token}`),
|
||||
).toEqual({ n: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("deletion confirmed on a former group", () => {
|
||||
it("never deletes a later group that reused the id, however alike", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const routes = (await adminGroupRoutesGet(
|
||||
call("/admin/api/groups/alpha/routes", owner),
|
||||
"alpha",
|
||||
)) as { revision: string; version: number };
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", owner))) as {
|
||||
groups: Array<{ id: string; revision: string }>;
|
||||
};
|
||||
const rev = listed.groups.find((g) => g.id === "alpha")!.revision;
|
||||
await deleteAs(owner, "alpha");
|
||||
// Past the retirement: the id is taken again, by a group alike in every
|
||||
// field the console shows (and at the same version).
|
||||
sqlite.exec("DELETE FROM dedup_keys");
|
||||
await createGroup(env.KV, group("alpha", "alice"));
|
||||
const del = call(
|
||||
`/admin/api/groups/alpha?revision=${rev}&routesRevision=${routes.revision}&version=${routes.version}`,
|
||||
owner,
|
||||
"DELETE",
|
||||
);
|
||||
await adminGroupDelete(del, "alpha");
|
||||
expect(responseStatus(del)).toBe(409);
|
||||
expect((await loadGroups(env.KV, { fresh: true })).some((g) => g.id === "alpha")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("bulk deletion and unreadable fragments", () => {
|
||||
it("never deletes a group whose fragments cannot be read", async () => {
|
||||
sqlite.exec(
|
||||
`INSERT INTO d1_fragments (id, group_id, name, node, created_at, updated_at) VALUES ('f', 'alpha', 'f', '{', 0, 0)`,
|
||||
);
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const listed = (await adminApiGroupsGet(call("/admin/api/groups", owner))) as {
|
||||
groups: Array<Group & { revision: string }>;
|
||||
revision: string;
|
||||
};
|
||||
const event = call("/admin/api/groups", owner, "PUT", {
|
||||
groups: listed.groups.filter((g) => g.id !== "alpha"),
|
||||
revision: listed.revision,
|
||||
});
|
||||
await adminApiGroupsPut(event);
|
||||
expect(responseStatus(event)).toBe(409);
|
||||
expect((await loadGroups(env.KV, { fresh: true })).some((g) => g.id === "alpha")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("invite revocation at commit time", () => {
|
||||
it("refuses a revocation whose owner was removed before it committed", async () => {
|
||||
const owner = await cookieFor("2", "alice");
|
||||
const created = (await adminGroupInvitesPost(
|
||||
call("/admin/api/groups/alpha/invites", owner, "POST", { role: "admin" }),
|
||||
"alpha",
|
||||
)) as { token: string };
|
||||
// Alice is removed while her request is under way (after its checks).
|
||||
const kvGet = env.KV.get.bind(env.KV);
|
||||
let reads = 0;
|
||||
env.KV.get = (async (key: string, ...rest: unknown[]) => {
|
||||
if (key === `invite:${created.token}` && ++reads === 2) {
|
||||
sqlite.exec(
|
||||
`UPDATE d1_groups SET data = json_set(data, '$.members', json('[{"login":"bob","role":"owner"}]'), '$.adminIds', json('["bob"]')) WHERE id = 'alpha'`,
|
||||
);
|
||||
}
|
||||
return (kvGet as (...a: unknown[]) => Promise<unknown>)(key, ...rest);
|
||||
}) as KVNamespace["get"];
|
||||
const del = call(`/admin/api/invites/${created.token}`, owner, "DELETE");
|
||||
await adminInviteDelete(del, created.token);
|
||||
env.KV.get = kvGet;
|
||||
expect(responseStatus(del)).toBe(403);
|
||||
expect(await getInvite(env.KV, created.token)).not.toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,8 @@ import {
|
||||
getFailedSendLog,
|
||||
type SendRecord,
|
||||
} from "../server/lib/lib/send-log";
|
||||
import { adminApiMetrics, adminApiDelivery } from "../server/lib/web/admin";
|
||||
import { adminApiMetrics, adminApiDelivery, adminApiLogs } from "../server/lib/web/admin";
|
||||
import { createSqliteD1 } from "./d1-sqlite";
|
||||
import { createAdminSession, adminCookie } from "../server/lib/web/session";
|
||||
import { invalidateGroupsCache } from "../server/lib/web/groups";
|
||||
import { makeEvent, responseStatus } from "./helpers";
|
||||
@@ -95,6 +96,8 @@ const logRow = (overrides: Record<string, unknown> = {}): Record<string, unknown
|
||||
describe("delivery metrics", () => {
|
||||
it("aggregates send_logs into metrics", async () => {
|
||||
const db = createMetricsDB({
|
||||
// Totals, average duration and attempts come from one scan.
|
||||
attempts_total: [{ total: 10, ok: 8, avg: 120.5, attempts_total: 12, attempts_avg: 1.2 }],
|
||||
"platform AS key": [
|
||||
{ key: "discord", total: 7, ok: 6 },
|
||||
{ key: "telegram", total: 3, ok: 2 },
|
||||
@@ -104,10 +107,7 @@ describe("delivery metrics", () => {
|
||||
{ status: "200", count: 8 },
|
||||
{ status: "500", count: 2 },
|
||||
],
|
||||
"AVG(duration_ms)": [{ avg: 120.5 }],
|
||||
"SUM(attempts)": [{ total: 12, avg: 1.2 }],
|
||||
"ok = 0": [logRow({ ok: 0, status: 500, error_code: "DISCORD_5XX" })],
|
||||
"COUNT(*) AS total": [{ total: 10, ok: 8 }],
|
||||
});
|
||||
const m = await getDeliveryMetrics(db);
|
||||
// Every query goes out in a single batch (one D1 round trip).
|
||||
@@ -166,70 +166,106 @@ describe("delivery metrics", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("delivery metrics (SQLite)", () => {
|
||||
it("averages duration and attempts over the rows that have them, in one scan", async () => {
|
||||
const { db, sqlite } = createSqliteD1();
|
||||
const insert = sqlite.prepare(
|
||||
"INSERT INTO send_logs (ts, route_id, group_id, event, target, ok, duration_ms, attempts) VALUES (?, 'r', 'g', 'push', 't', ?, ?, ?)",
|
||||
);
|
||||
insert.run(1, 1, 100, 1);
|
||||
insert.run(2, 0, 300, 3);
|
||||
insert.run(3, 1, null, null);
|
||||
const m = await getDeliveryMetrics(db);
|
||||
expect(m.total).toBe(3);
|
||||
expect(m.ok).toBe(2);
|
||||
expect(m.avgDurationMs).toBe(200);
|
||||
expect(m.totalAttempts).toBe(4);
|
||||
expect(m.avgAttempts).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("admin metrics/delivery handlers", () => {
|
||||
beforeEach(() => invalidateGroupsCache());
|
||||
it("returns global metrics for super admins", async () => {
|
||||
|
||||
/** A real (SQLite) D1 with two groups and send logs in both. */
|
||||
function seeded(): { env: Env; kv: KVNamespace } {
|
||||
const { db, sqlite } = createSqliteD1();
|
||||
const kv = createMockKV();
|
||||
const db = createMetricsDB({ "COUNT(*) AS total": [{ total: 5, ok: 4 }] });
|
||||
const env = createEnv({ KV: kv, DB: db, ADMIN_USER_IDS: "1001" });
|
||||
const sid = await createAdminSession(kv, "1001", "alice");
|
||||
const event = makeEvent("/api/metrics", { headers: { cookie: adminCookie(sid) }, env });
|
||||
const insert = sqlite.prepare(
|
||||
"INSERT INTO send_logs (ts, route_id, group_id, event, target, ok, delivery_id, error_code) VALUES (?, 'r', ?, 'push', 't', ?, ?, ?)",
|
||||
);
|
||||
insert.run(1, "mine", 1, "d1", null);
|
||||
insert.run(2, "mine", 0, "d1", "NETWORK");
|
||||
insert.run(3, "theirs", 0, "d1", "NETWORK");
|
||||
insert.run(4, "theirs", 1, "d2", null);
|
||||
insert.run(5, "theirs", 1, "d3", null);
|
||||
const groups = [
|
||||
{ id: "mine", name: "Mine", adminIds: [], members: [{ login: "bob", role: "owner" }] },
|
||||
{ id: "theirs", name: "Theirs", adminIds: [], members: [{ login: "eve", role: "owner" }] },
|
||||
];
|
||||
for (const g of groups) {
|
||||
sqlite
|
||||
.prepare(
|
||||
"INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES (?, ?, ?, 1, 0, 0)",
|
||||
)
|
||||
.run(g.id, g.name, JSON.stringify(g));
|
||||
}
|
||||
return { env: createEnv({ KV: kv, DB: db, ADMIN_USER_IDS: "1001" }), kv };
|
||||
}
|
||||
|
||||
async function as(kv: KVNamespace, userId: string, login: string): Promise<string> {
|
||||
return adminCookie(await createAdminSession(kv, userId, login));
|
||||
}
|
||||
|
||||
it("returns global metrics for super admins", async () => {
|
||||
const { env, kv } = seeded();
|
||||
const cookie = await as(kv, "1001", "alice");
|
||||
const event = makeEvent("/api/metrics", { headers: { cookie }, env });
|
||||
const res = (await adminApiMetrics(event)) as { metrics: { total: number } };
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
expect(res.metrics.total).toBe(5);
|
||||
});
|
||||
|
||||
it("filters recentFailures for non-super users", async () => {
|
||||
const kv = createMockKV();
|
||||
await kv.put(
|
||||
"config:groups",
|
||||
JSON.stringify([
|
||||
{ id: "mine", name: "Mine", adminIds: [], members: [{ login: "bob", role: "owner" }] },
|
||||
]),
|
||||
);
|
||||
const db = createMetricsDB({
|
||||
"ok = 0": [logRow({ ok: 0, group_id: "mine" }), logRow({ ok: 0, group_id: "theirs" })],
|
||||
"COUNT(*) AS total": [{ total: 1, ok: 0 }],
|
||||
});
|
||||
const env = createEnv({ KV: kv, DB: db });
|
||||
const sid = await createAdminSession(kv, "2002", "bob");
|
||||
const event = makeEvent("/api/metrics", { headers: { cookie: adminCookie(sid) }, env });
|
||||
it("scopes every aggregate to the reader's groups", async () => {
|
||||
const { env, kv } = seeded();
|
||||
const cookie = await as(kv, "2002", "bob");
|
||||
const event = makeEvent("/api/metrics", { headers: { cookie }, env });
|
||||
const res = (await adminApiMetrics(event)) as {
|
||||
metrics: { recentFailures: Array<{ groupId?: string }> };
|
||||
metrics: { total: number; failed: number; recentFailures: Array<{ groupId?: string }> };
|
||||
};
|
||||
expect(res.metrics.recentFailures).toHaveLength(1);
|
||||
expect(res.metrics.recentFailures[0]!.groupId).toBe("mine");
|
||||
expect(res.metrics.total).toBe(2);
|
||||
expect(res.metrics.failed).toBe(1);
|
||||
expect(res.metrics.recentFailures.map((f) => f.groupId)).toEqual(["mine"]);
|
||||
});
|
||||
|
||||
it("returns 404 for an unknown delivery", async () => {
|
||||
const kv = createMockKV();
|
||||
const env = createEnv({
|
||||
KV: kv,
|
||||
DB: createMetricsDB({ delivery_id: [] }),
|
||||
ADMIN_USER_IDS: "1001",
|
||||
});
|
||||
const sid = await createAdminSession(kv, "1001", "alice");
|
||||
const event = makeEvent("/api/delivery/nope", { headers: { cookie: adminCookie(sid) }, env });
|
||||
const { env, kv } = seeded();
|
||||
const cookie = await as(kv, "1001", "alice");
|
||||
const event = makeEvent("/api/delivery/nope", { headers: { cookie }, env });
|
||||
const res = (await adminApiDelivery(event, "nope")) as { error?: string };
|
||||
expect(responseStatus(event)).toBe(404);
|
||||
expect(res.error).toBe("Delivery not found");
|
||||
});
|
||||
|
||||
it("returns correlated attempts for a delivery", async () => {
|
||||
const kv = createMockKV();
|
||||
const db = createMetricsDB({
|
||||
delivery_id: [logRow({ id: 1, ok: 1, delivery_id: "d1" })],
|
||||
});
|
||||
const env = createEnv({ KV: kv, DB: db, ADMIN_USER_IDS: "1001" });
|
||||
const sid = await createAdminSession(kv, "1001", "alice");
|
||||
const event = makeEvent("/api/delivery/d1", { headers: { cookie: adminCookie(sid) }, env });
|
||||
it("shows a reader only their groups' attempts of a shared delivery", async () => {
|
||||
const { env, kv } = seeded();
|
||||
const cookie = await as(kv, "2002", "bob");
|
||||
const event = makeEvent("/api/delivery/d1", { headers: { cookie }, env });
|
||||
const res = (await adminApiDelivery(event, "d1")) as {
|
||||
deliveryId?: string;
|
||||
attempts?: Array<{ deliveryId?: string }>;
|
||||
attempts?: Array<{ groupId?: string }>;
|
||||
};
|
||||
expect(responseStatus(event)).toBe(200);
|
||||
expect(res.deliveryId).toBe("d1");
|
||||
expect(res.attempts).toHaveLength(1);
|
||||
expect(res.attempts![0]!.deliveryId).toBe("d1");
|
||||
expect(res.attempts!.map((a) => a.groupId)).toEqual(["mine", "mine"]);
|
||||
const other = makeEvent("/api/delivery/d2", { headers: { cookie }, env });
|
||||
await adminApiDelivery(other, "d2");
|
||||
expect(responseStatus(other)).toBe(404);
|
||||
});
|
||||
|
||||
it("lists logs of the reader's groups even when others are newer", async () => {
|
||||
const { env, kv } = seeded();
|
||||
const cookie = await as(kv, "2002", "bob");
|
||||
const event = makeEvent("/api/logs?limit=2", { headers: { cookie }, env });
|
||||
const res = (await adminApiLogs(event)) as { logs: Array<{ groupId?: string }> };
|
||||
expect(res.logs.map((l) => l.groupId)).toEqual(["mine", "mine"]);
|
||||
});
|
||||
});
|
||||
+104
-9
@@ -6,6 +6,8 @@ import {
|
||||
enqueueWebhook,
|
||||
getDeliveryState,
|
||||
isRetryableError,
|
||||
isRetryableFailure,
|
||||
PayloadUnavailableError,
|
||||
resolvePayload,
|
||||
retryDelay,
|
||||
setDeliveryState,
|
||||
@@ -13,6 +15,8 @@ import {
|
||||
type DispatchSummary,
|
||||
} from "../server/lib/queue/delivery";
|
||||
import type { Env } from "../server/lib/types";
|
||||
import { markDeliveryDead, getDeliveryState } from "../server/lib/queue/delivery";
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
|
||||
function createMockKV(): { kv: KVNamespace; store: Map<string, string> } {
|
||||
const store = new Map<string, string>();
|
||||
@@ -105,7 +109,8 @@ describe("classifyDelivery", () => {
|
||||
};
|
||||
expect(classifyDelivery(summary)).toEqual({ failed: true, retryable: true });
|
||||
});
|
||||
it("is not retryable when any failure is permanent", () => {
|
||||
it("stays retryable when a permanent failure sits next to a transient one", () => {
|
||||
// The permanent target is recorded as done; only the transient one retries.
|
||||
const summary: DispatchSummary = {
|
||||
attempts: 2,
|
||||
failures: [
|
||||
@@ -113,8 +118,26 @@ describe("classifyDelivery", () => {
|
||||
{ target: "c2", errorCode: "DISCORD_ERROR" },
|
||||
],
|
||||
};
|
||||
expect(classifyDelivery(summary)).toEqual({ failed: true, retryable: true });
|
||||
});
|
||||
it("is not retryable when every failure is permanent", () => {
|
||||
const summary: DispatchSummary = {
|
||||
attempts: 2,
|
||||
failures: [
|
||||
{ target: "c1", errorCode: "DISCORD_ERROR" },
|
||||
{ target: "c2", errorCode: "FORMAT" },
|
||||
],
|
||||
};
|
||||
expect(classifyDelivery(summary)).toEqual({ failed: true, retryable: false });
|
||||
});
|
||||
it("treats 5xx and 429 statuses as transient whatever the code", () => {
|
||||
expect(isRetryableFailure({ target: "x", errorCode: "FEISHU_99991400", status: 429 })).toBe(
|
||||
true,
|
||||
);
|
||||
expect(isRetryableFailure({ target: "x", errorCode: "FEISHU_ERROR", status: 502 })).toBe(true);
|
||||
expect(isRetryableFailure({ target: "x", errorCode: "FEISHU_5XX" })).toBe(true);
|
||||
expect(isRetryableFailure({ target: "x", errorCode: "FEISHU_ERROR", status: 400 })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("delivery state", () => {
|
||||
@@ -135,7 +158,11 @@ describe("delivery state", () => {
|
||||
|
||||
it("scopes state keys by provider, group and delivery id", () => {
|
||||
expect(deliveryStateKey("github", "g1", "d1")).toBe("delivery-state:github:g1:d1");
|
||||
expect(deliveryStateKey("github", undefined, "d1")).toBe("delivery-state:github:global:d1");
|
||||
expect(deliveryStateKey("github", undefined, "d1")).toBe("delivery-state:github:@global:d1");
|
||||
// A group literally named "global" never shares state with the global endpoint.
|
||||
expect(deliveryStateKey("github", "global", "d1")).not.toBe(
|
||||
deliveryStateKey("github", undefined, "d1"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -159,12 +186,36 @@ describe("enqueueWebhook", () => {
|
||||
await enqueueWebhook(env, message({ payload: { data: large } }));
|
||||
expect(sent).toHaveLength(1);
|
||||
expect(sent[0].payload).toBeUndefined();
|
||||
expect(sent[0].payloadRef).toBe("queue:payload:github:global:d1");
|
||||
const stored = store.get("queue:payload:github:global:d1");
|
||||
expect(sent[0].payloadRef).toMatch(/^queue:payload:github:@global:d1:/);
|
||||
const stored = store.get(sent[0].payloadRef!);
|
||||
expect(stored).toBeDefined();
|
||||
expect(JSON.parse(stored!).data).toBe(large);
|
||||
});
|
||||
|
||||
it("measures the message in UTF-8 bytes, not characters", async () => {
|
||||
const { kv } = createMockKV();
|
||||
const { queue, sent } = createMockQueue();
|
||||
// 50k CJK characters are ~150 KB: over the queue limit despite the length.
|
||||
await enqueueWebhook(createEnv(kv, queue), message({ payload: { data: "中".repeat(50_000) } }));
|
||||
expect(sent[0].payload).toBeUndefined();
|
||||
expect(sent[0].payloadRef).toBeDefined();
|
||||
});
|
||||
|
||||
it("keeps the parked payload and rethrows when the send fails", async () => {
|
||||
const { kv, store } = createMockKV();
|
||||
const queue = {
|
||||
send: async () => {
|
||||
throw new Error("queue down");
|
||||
},
|
||||
} as unknown as Queue;
|
||||
await expect(
|
||||
enqueueWebhook(createEnv(kv, queue), message({ payload: { data: "x".repeat(150_000) } })),
|
||||
).rejects.toThrow("queue down");
|
||||
// The queue may have taken the message despite the error: its payload
|
||||
// stays (and expires on its own).
|
||||
expect([...store.keys()].filter((k) => k.startsWith("queue:payload:"))).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("throws when the QUEUE binding is missing", async () => {
|
||||
const { kv } = createMockKV();
|
||||
await expect(enqueueWebhook(createEnv(kv), message())).rejects.toThrow(
|
||||
@@ -192,14 +243,39 @@ describe("resolvePayload / discardPayload", () => {
|
||||
).toEqual({ a: 1 });
|
||||
});
|
||||
|
||||
it("returns an empty object when the payloadRef is missing", async () => {
|
||||
it("throws when the parked payload is missing", async () => {
|
||||
const { kv } = createMockKV();
|
||||
expect(
|
||||
await resolvePayload(
|
||||
await expect(
|
||||
resolvePayload(
|
||||
createEnv(kv),
|
||||
message({ payload: undefined, payloadRef: "queue:payload:missing" }),
|
||||
message({
|
||||
payload: undefined,
|
||||
payloadRef: "queue:payload:missing",
|
||||
receivedAt: Date.now() - 3600_000,
|
||||
}),
|
||||
),
|
||||
).toEqual({});
|
||||
).rejects.toBeInstanceOf(PayloadUnavailableError);
|
||||
});
|
||||
|
||||
it("retries (rather than gives up on) a KV payload parked moments ago", async () => {
|
||||
const { kv } = createMockKV();
|
||||
const err = await resolvePayload(
|
||||
createEnv(kv),
|
||||
message({ payload: undefined, payloadRef: "queue:payload:fresh" }),
|
||||
).catch((e: unknown) => e);
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err).not.toBeInstanceOf(PayloadUnavailableError);
|
||||
});
|
||||
|
||||
it("throws when the parked payload is corrupt", async () => {
|
||||
const { kv, store } = createMockKV();
|
||||
store.set("queue:payload:bad", "{not json");
|
||||
await expect(
|
||||
resolvePayload(
|
||||
createEnv(kv),
|
||||
message({ payload: undefined, payloadRef: "queue:payload:bad" }),
|
||||
),
|
||||
).rejects.toBeInstanceOf(PayloadUnavailableError);
|
||||
});
|
||||
|
||||
it("deletes the payloadRef on discard", async () => {
|
||||
@@ -209,3 +285,22 @@ describe("resolvePayload / discardPayload", () => {
|
||||
expect(store.has("queue:payload:github:global:d1")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("markDeliveryDead", () => {
|
||||
it("never overwrites a finished delivery or a live lease", async () => {
|
||||
const { db, sqlite } = createSqliteD1();
|
||||
const env: Env = { GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: db };
|
||||
const now = Date.now();
|
||||
sqlite.exec(
|
||||
`INSERT INTO delivery_state (key, status, updated_at) VALUES
|
||||
('done', 'delivered', ${now}), ('live', 'processing', ${now}),
|
||||
('stale', 'processing', ${now - 3600_000}), ('waiting', 'retrying', ${now})`,
|
||||
);
|
||||
for (const key of ["done", "live", "stale", "waiting", "new"]) await markDeliveryDead(env, key);
|
||||
expect(await getDeliveryState(env, "done")).toBe("delivered");
|
||||
expect(await getDeliveryState(env, "live")).toBe("processing");
|
||||
expect(await getDeliveryState(env, "stale")).toBe("dead");
|
||||
expect(await getDeliveryState(env, "waiting")).toBe("dead");
|
||||
expect(await getDeliveryState(env, "new")).toBe("dead");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,313 @@
|
||||
import { describe, it, expect, afterEach } from "bun:test";
|
||||
import { handleInteractionRequest } from "../server/lib/drivers/discord/interactions";
|
||||
import { saveToken } from "../server/lib/github/store";
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
import type { Env } from "../server/lib/types";
|
||||
|
||||
const restoredFetch = globalThis.fetch;
|
||||
afterEach(() => {
|
||||
globalThis.fetch = restoredFetch;
|
||||
});
|
||||
|
||||
function hex(bytes: ArrayBuffer): string {
|
||||
return Array.from(new Uint8Array(bytes))
|
||||
.map((b) => b.toString(16).padStart(2, "0"))
|
||||
.join("");
|
||||
}
|
||||
|
||||
async function setup(): Promise<{
|
||||
env: Env;
|
||||
send: (interaction: Record<string, unknown>) => Promise<Response>;
|
||||
}> {
|
||||
const keys = (await crypto.subtle.generateKey({ name: "Ed25519" }, true, [
|
||||
"sign",
|
||||
"verify",
|
||||
])) as CryptoKeyPair;
|
||||
const publicKey = hex(await crypto.subtle.exportKey("raw", keys.publicKey));
|
||||
const { db, sqlite } = createSqliteD1();
|
||||
sqlite.exec("INSERT INTO discord_links (discord_user_id, github_user_id) VALUES ('d1', 'g1')");
|
||||
const { kv } = createMemoryKV();
|
||||
await saveToken(kv, "g1", "gho_token", 3600);
|
||||
const env: Env = { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: db, DISCORD_PUBLIC_KEY: publicKey };
|
||||
async function send(interaction: Record<string, unknown>): Promise<Response> {
|
||||
const body = JSON.stringify(interaction);
|
||||
const timestamp = String(Math.floor(Date.now() / 1000));
|
||||
const signature = hex(
|
||||
await crypto.subtle.sign(
|
||||
"Ed25519",
|
||||
keys.privateKey,
|
||||
new TextEncoder().encode(timestamp + body),
|
||||
),
|
||||
);
|
||||
return handleInteractionRequest(
|
||||
new Request("https://x/discord/interactions", {
|
||||
method: "POST",
|
||||
headers: { "X-Signature-Ed25519": signature, "X-Signature-Timestamp": timestamp },
|
||||
body,
|
||||
}),
|
||||
env,
|
||||
);
|
||||
}
|
||||
return { env, send };
|
||||
}
|
||||
|
||||
describe("discord interactions", () => {
|
||||
it("edits the deferred reply through the application webhook and strips the clicked message", async () => {
|
||||
const calls: Array<{ method: string; url: string }> = [];
|
||||
globalThis.fetch = async (input, init): Promise<Response> => {
|
||||
calls.push({ method: init?.method ?? "GET", url: String(input) });
|
||||
return new Response("{}", { status: 200, headers: { "content-type": "application/json" } });
|
||||
};
|
||||
const { send } = await setup();
|
||||
const res = await send({
|
||||
id: "i1",
|
||||
application_id: "app1",
|
||||
token: "tok",
|
||||
type: 3,
|
||||
channel_id: "chan",
|
||||
member: { user: { id: "d1" } },
|
||||
message: { id: "msg9" },
|
||||
data: { custom_id: "ghpr|close|o|r|7" },
|
||||
});
|
||||
expect(res.status).toBe(202);
|
||||
expect(
|
||||
calls.some((c) => c.method === "PATCH" && c.url.endsWith("/channels/chan/messages/msg9")),
|
||||
).toBe(true);
|
||||
expect(
|
||||
calls.some(
|
||||
(c) => c.method === "PATCH" && c.url.endsWith("/webhooks/app1/tok/messages/@original"),
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("runs a replayed interaction only once", async () => {
|
||||
let callbacks = 0;
|
||||
globalThis.fetch = async (input): Promise<Response> => {
|
||||
if (String(input).includes("/callback")) callbacks++;
|
||||
return new Response("{}", { status: 200 });
|
||||
};
|
||||
const { send } = await setup();
|
||||
const interaction = {
|
||||
id: "i2",
|
||||
application_id: "app1",
|
||||
token: "tok",
|
||||
type: 2,
|
||||
member: { user: { id: "d1" } },
|
||||
data: { name: "gh", type: 1, options: [{ name: "logout" }] },
|
||||
};
|
||||
await send(interaction);
|
||||
await send(interaction);
|
||||
expect(callbacks).toBe(1);
|
||||
});
|
||||
|
||||
it("refuses to edit a comment longer than the modal can hold", async () => {
|
||||
const bodies: string[] = [];
|
||||
globalThis.fetch = async (input, init): Promise<Response> => {
|
||||
const url = String(input);
|
||||
if (url.includes("/issues/comments/")) {
|
||||
return new Response(JSON.stringify({ body: "x".repeat(5000), user: { login: "a" } }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
if (url.includes("/callback")) bodies.push(String(init?.body));
|
||||
return new Response("{}", { status: 200 });
|
||||
};
|
||||
const { send } = await setup();
|
||||
await send({
|
||||
id: "i3",
|
||||
application_id: "app1",
|
||||
token: "tok",
|
||||
type: 2,
|
||||
member: { user: { id: "d1" } },
|
||||
data: {
|
||||
name: "gh",
|
||||
type: 1,
|
||||
options: [
|
||||
{
|
||||
name: "comment",
|
||||
options: [
|
||||
{
|
||||
name: "edit",
|
||||
options: [
|
||||
{ name: "link", value: "https://github.com/o/r/issues/1#issuecomment-55" },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(bodies).toHaveLength(1);
|
||||
const reply = JSON.parse(bodies[0]!) as { type: number; data: { content?: string } };
|
||||
// A message (type 4) pointing to GitHub, not a modal (type 9) holding a cut-off copy.
|
||||
expect(reply.type).toBe(4);
|
||||
expect(reply.data.content).toContain("github.com/o/r/issues/1#issuecomment-55");
|
||||
});
|
||||
|
||||
it("acknowledges a comment modal before calling GitHub, then shows the outcome", async () => {
|
||||
const order: string[] = [];
|
||||
globalThis.fetch = async (input, init): Promise<Response> => {
|
||||
const url = String(input);
|
||||
if (url.includes("/callback")) {
|
||||
order.push(`callback:${(JSON.parse(String(init?.body)) as { type: number }).type}`);
|
||||
} else if (url.includes("api.github.com")) {
|
||||
order.push("github");
|
||||
return new Response(
|
||||
JSON.stringify({ html_url: "https://github.com/o/r/issues/7#c", user: { login: "a" } }),
|
||||
{ status: 201, headers: { "content-type": "application/json" } },
|
||||
);
|
||||
} else if (url.endsWith("/messages/@original")) {
|
||||
order.push("original");
|
||||
}
|
||||
return new Response("{}", { status: 200, headers: { "content-type": "application/json" } });
|
||||
};
|
||||
const { send } = await setup();
|
||||
await send({
|
||||
id: "i4",
|
||||
application_id: "app1",
|
||||
token: "tok",
|
||||
type: 5,
|
||||
member: { user: { id: "d1" } },
|
||||
data: {
|
||||
custom_id: "ghc|add|o|r|7",
|
||||
components: [{ components: [{ custom_id: "body", value: "hello" }] }],
|
||||
},
|
||||
});
|
||||
// Deferred (type 5) first: a slow GitHub call never fails the interaction.
|
||||
expect(order[0]).toBe("callback:5");
|
||||
expect(order.indexOf("github")).toBeGreaterThan(0);
|
||||
expect(order.at(-1)).toBe("original");
|
||||
});
|
||||
|
||||
it("leaves GitHub alone when Discord refused the acknowledgement", async () => {
|
||||
const calls: string[] = [];
|
||||
globalThis.fetch = async (input): Promise<Response> => {
|
||||
const url = String(input);
|
||||
calls.push(url);
|
||||
if (url.includes("/callback")) return new Response("unknown interaction", { status: 404 });
|
||||
return new Response(
|
||||
JSON.stringify({ html_url: "https://github.com/o/r/issues/7#c", user: { login: "a" } }),
|
||||
{ status: 201, headers: { "content-type": "application/json" } },
|
||||
);
|
||||
};
|
||||
const { send } = await setup();
|
||||
await send({
|
||||
id: "i5",
|
||||
application_id: "app1",
|
||||
token: "tok",
|
||||
type: 5,
|
||||
member: { user: { id: "d1" } },
|
||||
data: {
|
||||
custom_id: "ghc|add|o|r|7",
|
||||
components: [{ components: [{ custom_id: "body", value: "hello" }] }],
|
||||
},
|
||||
});
|
||||
expect(calls.some((u) => u.includes("api.github.com"))).toBe(false);
|
||||
});
|
||||
|
||||
it("refuses to save an edit over changes made on GitHub after the modal opened", async () => {
|
||||
let current = "original";
|
||||
const patches: string[] = [];
|
||||
const callbacks: string[] = [];
|
||||
const originals: string[] = [];
|
||||
globalThis.fetch = async (input, init): Promise<Response> => {
|
||||
const url = String(input);
|
||||
if (url.includes("/issues/comments/55")) {
|
||||
if ((init?.method ?? "GET") === "PATCH") patches.push(String(init?.body));
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
body: current,
|
||||
html_url: "https://github.com/o/r/issues/1#issuecomment-55",
|
||||
user: { login: "a" },
|
||||
}),
|
||||
{ status: 200, headers: { "content-type": "application/json" } },
|
||||
);
|
||||
}
|
||||
if (url.includes("/callback")) callbacks.push(String(init?.body));
|
||||
if (url.endsWith("/messages/@original")) originals.push(String(init?.body));
|
||||
return new Response("{}", { status: 200, headers: { "content-type": "application/json" } });
|
||||
};
|
||||
const { send } = await setup();
|
||||
await send({
|
||||
id: "i6",
|
||||
application_id: "app1",
|
||||
token: "tok",
|
||||
type: 2,
|
||||
member: { user: { id: "d1" } },
|
||||
data: {
|
||||
name: "gh",
|
||||
type: 1,
|
||||
options: [
|
||||
{
|
||||
name: "comment",
|
||||
options: [
|
||||
{
|
||||
name: "edit",
|
||||
options: [
|
||||
{ name: "link", value: "https://github.com/o/r/issues/1#issuecomment-55" },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
const modal = JSON.parse(callbacks[0]!) as { type: number; data: { custom_id: string } };
|
||||
expect(modal.type).toBe(9);
|
||||
expect(modal.data.custom_id.length).toBeLessThanOrEqual(100);
|
||||
// Short enough: the context (with the text's hash) travels in the id — no KV write.
|
||||
expect(modal.data.custom_id.startsWith("ghc|ed|")).toBe(true);
|
||||
current = "changed on GitHub";
|
||||
await send({
|
||||
id: "i7",
|
||||
application_id: "app1",
|
||||
token: "tok2",
|
||||
type: 5,
|
||||
member: { user: { id: "d1" } },
|
||||
data: {
|
||||
custom_id: modal.data.custom_id,
|
||||
components: [{ components: [{ custom_id: "body", value: "original, edited" }] }],
|
||||
},
|
||||
});
|
||||
expect(patches).toHaveLength(0);
|
||||
expect(originals.at(-1)).toContain("已被修改");
|
||||
});
|
||||
|
||||
it("keeps the custom id short for long repository names", async () => {
|
||||
const callbacks: string[] = [];
|
||||
globalThis.fetch = async (input, init): Promise<Response> => {
|
||||
if (String(input).includes("/callback")) callbacks.push(String(init?.body));
|
||||
return new Response("{}", { status: 200, headers: { "content-type": "application/json" } });
|
||||
};
|
||||
const { send } = await setup();
|
||||
const repo = "r".repeat(100);
|
||||
await send({
|
||||
id: "i8",
|
||||
application_id: "app1",
|
||||
token: "tok",
|
||||
type: 2,
|
||||
member: { user: { id: "d1" } },
|
||||
data: {
|
||||
name: "gh",
|
||||
type: 1,
|
||||
options: [
|
||||
{
|
||||
name: "comment",
|
||||
options: [
|
||||
{
|
||||
name: "add",
|
||||
options: [{ name: "link", value: `https://github.com/o/${repo}/issues/1` }],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
const modal = JSON.parse(callbacks[0]!) as { type: number; data: { custom_id: string } };
|
||||
expect(modal.type).toBe(9);
|
||||
expect(modal.data.custom_id.length).toBeLessThanOrEqual(100);
|
||||
// Too long to carry its context: kept in KV (the only case that writes).
|
||||
expect(modal.data.custom_id.startsWith("ghc|m|")).toBe(true);
|
||||
});
|
||||
});
|
||||
+48
-1
@@ -245,7 +245,8 @@ describe("dispatchEvent fallback routing", () => {
|
||||
route("omega-catch-all-2", "omega", { fallback: true, filters: [] }, "306"),
|
||||
];
|
||||
|
||||
await dispatchEvent({ ...baseConfig, routes }, { event: "push", payload: {} }, env);
|
||||
const groups = ["alpha", "zeta", "omega"].map((id) => ({ id, name: id, adminIds: [] }));
|
||||
await dispatchEvent({ ...baseConfig, routes }, { event: "push", payload: {} }, env, groups);
|
||||
const hits = (channel: string): number => sent.filter((u) => u.includes(`/${channel}/`)).length;
|
||||
// alpha stops after its first match without silencing zeta.
|
||||
expect(hits("301")).toBe(1);
|
||||
@@ -258,6 +259,52 @@ describe("dispatchEvent fallback routing", () => {
|
||||
expect(hits("306")).toBe(0);
|
||||
});
|
||||
|
||||
it("never fires routes whose group no longer exists", async () => {
|
||||
const sent: string[] = [];
|
||||
mockFetch((url) => {
|
||||
sent.push(url);
|
||||
return new Response("{}", { status: 200 });
|
||||
});
|
||||
const env = createEnv({ KV: createMockKV(), DB: createMockDB() });
|
||||
const routes: Route[] = [
|
||||
{
|
||||
id: "orphan",
|
||||
name: "orphan",
|
||||
enabled: true,
|
||||
filters: [{ type: "event", match: "push" }],
|
||||
targets: [{ channelId: "401" }],
|
||||
groupId: "deleted",
|
||||
},
|
||||
];
|
||||
await dispatchEvent({ ...baseConfig, routes }, { event: "push", payload: {} }, env, []);
|
||||
expect(sent).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("keeps isolated groups off the global endpoint but serves their own ingress", async () => {
|
||||
const sent: string[] = [];
|
||||
mockFetch((url) => {
|
||||
sent.push(url);
|
||||
return new Response("{}", { status: 200 });
|
||||
});
|
||||
const env = createEnv({ KV: createMockKV(), DB: createMockDB() });
|
||||
const routes: Route[] = [
|
||||
{
|
||||
id: "mine",
|
||||
name: "mine",
|
||||
enabled: true,
|
||||
filters: [{ type: "event", match: "push" }],
|
||||
targets: [{ channelId: "501" }],
|
||||
groupId: "u-1",
|
||||
},
|
||||
];
|
||||
const groups = [{ id: "u-1", name: "me", adminIds: [], isolated: true }];
|
||||
const event = { event: "push", payload: {} };
|
||||
await dispatchEvent({ ...baseConfig, routes }, event, env, groups);
|
||||
expect(sent).toHaveLength(0);
|
||||
await dispatchEvent({ ...baseConfig, routes }, event, env, groups, { tenantId: "u-1" });
|
||||
expect(sent.filter((u) => u.includes("/501/"))).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("prepends role mentions to the Discord message content", async () => {
|
||||
const bodies: string[] = [];
|
||||
mockFetch((url, init) => {
|
||||
|
||||
+190
-59
@@ -1,86 +1,217 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import type { Env } from "../server/lib/types";
|
||||
import {
|
||||
decryptFeishuPayload,
|
||||
verifyFeishuSignature,
|
||||
handleFeishuWebhookRequest,
|
||||
} from "../server/lib/drivers/feishu/updates";
|
||||
import { createMemoryKV } from "./d1-sqlite";
|
||||
|
||||
function sign(secret: string, timestamp: string, nonce: string, body: string): Promise<string> {
|
||||
return (async () => {
|
||||
const key = await crypto.subtle.importKey(
|
||||
"raw",
|
||||
new TextEncoder().encode(secret),
|
||||
{ name: "HMAC", hash: "SHA-256" },
|
||||
false,
|
||||
["sign"],
|
||||
);
|
||||
const sig = await crypto.subtle.sign(
|
||||
"HMAC",
|
||||
key,
|
||||
new TextEncoder().encode(`${timestamp}\n${nonce}\n${body}`),
|
||||
);
|
||||
let binary = "";
|
||||
const bytes = new Uint8Array(sig);
|
||||
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i] ?? 0);
|
||||
return btoa(binary);
|
||||
})();
|
||||
async function sha256Hex(data: string): Promise<string> {
|
||||
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(data));
|
||||
return Array.from(new Uint8Array(digest))
|
||||
.map((b) => b.toString(16).padStart(2, "0"))
|
||||
.join("");
|
||||
}
|
||||
|
||||
const stubKV = {
|
||||
get: async () => null,
|
||||
put: async () => undefined,
|
||||
delete: async () => undefined,
|
||||
} as unknown as KVNamespace<string>;
|
||||
const stubDB = {} as unknown as D1Database;
|
||||
const stubEnv: Env = {
|
||||
GITHUB_WEBHOOK_SECRET: "",
|
||||
FEISHU_APP_ID: "",
|
||||
FEISHU_APP_SECRET: "",
|
||||
KV: stubKV,
|
||||
DB: stubDB,
|
||||
};
|
||||
/** Feishu's event signature: hex SHA-256 of timestamp + nonce + encryptKey + body. */
|
||||
function sign(encryptKey: string, timestamp: string, nonce: string, body: string): Promise<string> {
|
||||
return sha256Hex(`${timestamp}${nonce}${encryptKey}${body}`);
|
||||
}
|
||||
|
||||
/** Encrypt like Feishu: AES-256-CBC, key SHA-256(encryptKey), IV prepended. */
|
||||
async function encrypt(encryptKey: string, plaintext: string): Promise<string> {
|
||||
const keyBytes = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(encryptKey));
|
||||
const key = await crypto.subtle.importKey("raw", keyBytes, { name: "AES-CBC" }, false, [
|
||||
"encrypt",
|
||||
]);
|
||||
const iv = crypto.getRandomValues(new Uint8Array(16));
|
||||
const ct = new Uint8Array(
|
||||
await crypto.subtle.encrypt({ name: "AES-CBC", iv }, key, new TextEncoder().encode(plaintext)),
|
||||
);
|
||||
const all = new Uint8Array(16 + ct.length);
|
||||
all.set(iv);
|
||||
all.set(ct, 16);
|
||||
let bin = "";
|
||||
for (const b of all) bin += String.fromCharCode(b);
|
||||
return btoa(bin);
|
||||
}
|
||||
|
||||
function env(overrides: Partial<Env> = {}): Env {
|
||||
return {
|
||||
GITHUB_WEBHOOK_SECRET: "",
|
||||
FEISHU_APP_ID: "",
|
||||
FEISHU_APP_SECRET: "",
|
||||
KV: createMemoryKV().kv,
|
||||
DB: {} as D1Database,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function post(body: string, headers: Record<string, string> = {}): Request {
|
||||
return new Request("https://x/feishu/webhook", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json", ...headers },
|
||||
body,
|
||||
});
|
||||
}
|
||||
|
||||
describe("feishu signature", () => {
|
||||
it("verifies a correct signature", async () => {
|
||||
const secret = "s3cr3t";
|
||||
const ts = "1700000000";
|
||||
const nonce = "abc";
|
||||
const body = JSON.stringify({ hello: "world" });
|
||||
const sig = await sign(secret, ts, nonce, body);
|
||||
expect(await verifyFeishuSignature(secret, ts, nonce, body, sig)).toBe(true);
|
||||
const sig = await sign("key", "1700000000", "abc", body);
|
||||
expect(await verifyFeishuSignature("key", "1700000000", "abc", body, sig)).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects a wrong signature", async () => {
|
||||
const secret = "s3cr3t";
|
||||
it("rejects a wrong or missing signature", async () => {
|
||||
const body = JSON.stringify({ hello: "world" });
|
||||
const sig = await sign("other", "1", "2", body);
|
||||
expect(await verifyFeishuSignature(secret, "1", "2", body, sig)).toBe(false);
|
||||
expect(await verifyFeishuSignature("key", "1", "2", body, sig)).toBe(false);
|
||||
expect(await verifyFeishuSignature("key", "1", "2", body, "")).toBe(false);
|
||||
});
|
||||
|
||||
it("decrypts an encrypted payload", async () => {
|
||||
const encrypted = await encrypt("key", JSON.stringify({ a: 1 }));
|
||||
expect(JSON.parse(await decryptFeishuPayload("key", encrypted))).toEqual({ a: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("feishu webhook", () => {
|
||||
it("answers the url_verification challenge", async () => {
|
||||
const body = JSON.stringify({ type: "url_verification", challenge: "xyz123" });
|
||||
const req = new Request("https://x/feishu/webhook", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body,
|
||||
});
|
||||
const res = await handleFeishuWebhookRequest(req, stubEnv);
|
||||
expect(res.status).toBe(200);
|
||||
const json = await res.json();
|
||||
expect(json.challenge).toBe("xyz123");
|
||||
it("refuses every request when no verification is configured", async () => {
|
||||
const body = JSON.stringify({ type: "url_verification", challenge: "xyz" });
|
||||
const res = await handleFeishuWebhookRequest(post(body), env());
|
||||
expect(res.status).toBe(403);
|
||||
});
|
||||
|
||||
it("rejects a bad signature with 401 when a secret is configured", async () => {
|
||||
const env = { ...stubEnv, FEISHU_APP_SECRET: "s3cr3t" };
|
||||
const body = JSON.stringify({ type: "other", challenge: "x" });
|
||||
const req = new Request("https://x/feishu/webhook", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json", "x-lark-signature": "deadbeef" },
|
||||
body,
|
||||
it("answers the url_verification challenge with a matching token", async () => {
|
||||
const body = JSON.stringify({ type: "url_verification", challenge: "xyz123", token: "vt" });
|
||||
const res = await handleFeishuWebhookRequest(
|
||||
post(body),
|
||||
env({ FEISHU_VERIFICATION_TOKEN: "vt" }),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(((await res.json()) as { challenge: string }).challenge).toBe("xyz123");
|
||||
});
|
||||
|
||||
it("rejects a wrong verification token", async () => {
|
||||
const body = JSON.stringify({
|
||||
schema: "2.0",
|
||||
header: { event_type: "im.message.receive_v1", token: "nope" },
|
||||
event: {},
|
||||
});
|
||||
const res = await handleFeishuWebhookRequest(req, env);
|
||||
const res = await handleFeishuWebhookRequest(
|
||||
post(body),
|
||||
env({ FEISHU_VERIFICATION_TOKEN: "vt" }),
|
||||
);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it("rejects a missing or bad signature when an encrypt key is set", async () => {
|
||||
const body = JSON.stringify({ schema: "2.0", header: { event_type: "x" }, event: {} });
|
||||
const e = env({ FEISHU_ENCRYPT_KEY: "key" });
|
||||
expect((await handleFeishuWebhookRequest(post(body), e)).status).toBe(401);
|
||||
expect(
|
||||
(await handleFeishuWebhookRequest(post(body, { "x-lark-signature": "deadbeef" }), e)).status,
|
||||
).toBe(401);
|
||||
});
|
||||
|
||||
it("accepts a correctly signed, encrypted event once", async () => {
|
||||
const inner = JSON.stringify({
|
||||
schema: "2.0",
|
||||
header: { event_id: "ev-1", event_type: "unknown.event" },
|
||||
event: {},
|
||||
});
|
||||
const body = JSON.stringify({ encrypt: await encrypt("key", inner) });
|
||||
const ts = String(Math.floor(Date.now() / 1000));
|
||||
const nonce = "n1";
|
||||
const headers = {
|
||||
"x-lark-request-timestamp": ts,
|
||||
"x-lark-request-nonce": nonce,
|
||||
"x-lark-signature": await sign("key", ts, nonce, body),
|
||||
};
|
||||
const e = env({ FEISHU_ENCRYPT_KEY: "key" });
|
||||
expect((await handleFeishuWebhookRequest(post(body, headers), e)).status).toBe(200);
|
||||
// The retried delivery of the same event is acknowledged without running again.
|
||||
expect((await handleFeishuWebhookRequest(post(body, headers), e)).status).toBe(200);
|
||||
});
|
||||
|
||||
it("refuses a correctly signed request whose timestamp is stale", async () => {
|
||||
const inner = JSON.stringify({
|
||||
schema: "2.0",
|
||||
header: { event_id: "ev-old", event_type: "unknown.event" },
|
||||
event: {},
|
||||
});
|
||||
const body = JSON.stringify({ encrypt: await encrypt("key", inner) });
|
||||
// Older than any event claim could still remember.
|
||||
const ts = String(Math.floor(Date.now() / 1000) - 13 * 3600);
|
||||
const headers = {
|
||||
"x-lark-request-timestamp": ts,
|
||||
"x-lark-request-nonce": "n2",
|
||||
"x-lark-signature": await sign("key", ts, "n2", body),
|
||||
};
|
||||
const e = env({ FEISHU_ENCRYPT_KEY: "key" });
|
||||
expect((await handleFeishuWebhookRequest(post(body, headers), e)).status).toBe(401);
|
||||
});
|
||||
|
||||
it("refuses an oversized body before parsing it", async () => {
|
||||
const body = JSON.stringify({ pad: "x".repeat(1024 * 1024) });
|
||||
const e = env({ FEISHU_VERIFICATION_TOKEN: "vt" });
|
||||
expect((await handleFeishuWebhookRequest(post(body), e)).status).toBe(413);
|
||||
});
|
||||
});
|
||||
|
||||
describe("feishu command parsing", () => {
|
||||
it("never runs a command merely quoted inside a message", async () => {
|
||||
const e = env({ FEISHU_VERIFICATION_TOKEN: "vt" });
|
||||
// Any account lookup would fail loudly: a parsed command would answer 503.
|
||||
e.DB = {
|
||||
prepare: (): never => {
|
||||
throw new Error("command ran");
|
||||
},
|
||||
} as unknown as D1Database;
|
||||
const body = JSON.stringify({
|
||||
schema: "2.0",
|
||||
header: { event_id: "ev-q", event_type: "im.message.receive_v1", token: "vt" },
|
||||
event: {
|
||||
sender: { sender_id: { open_id: "ou_1" } },
|
||||
message: {
|
||||
chat_id: "oc_1",
|
||||
content: JSON.stringify({
|
||||
text: "please do not run /gh merge https://github.com/o/r/pull/7",
|
||||
}),
|
||||
},
|
||||
},
|
||||
});
|
||||
expect((await handleFeishuWebhookRequest(post(body), e)).status).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("feishu command retries", () => {
|
||||
it("answers 503 and releases the event when it failed before reaching GitHub", async () => {
|
||||
const e = env({ FEISHU_VERIFICATION_TOKEN: "vt" });
|
||||
// A D1 binding whose link lookup fails: the command never reaches GitHub.
|
||||
e.DB = {
|
||||
prepare: (): unknown => ({
|
||||
bind: (): unknown => ({
|
||||
all: async (): Promise<never> => {
|
||||
throw new Error("D1 down");
|
||||
},
|
||||
}),
|
||||
}),
|
||||
} as unknown as D1Database;
|
||||
const body = JSON.stringify({
|
||||
schema: "2.0",
|
||||
header: { event_id: "ev-9", event_type: "im.message.receive_v1", token: "vt" },
|
||||
event: {
|
||||
sender: { sender_id: { open_id: "ou_1" } },
|
||||
message: {
|
||||
chat_id: "oc_1",
|
||||
content: JSON.stringify({ text: "/gh merge https://github.com/o/r/pull/1" }),
|
||||
},
|
||||
},
|
||||
});
|
||||
expect((await handleFeishuWebhookRequest(post(body), e)).status).toBe(503);
|
||||
// The retry is not swallowed as a duplicate.
|
||||
expect((await handleFeishuWebhookRequest(post(body), e)).status).toBe(503);
|
||||
});
|
||||
});
|
||||
@@ -168,3 +168,36 @@ describe("matchRoute AST integration", () => {
|
||||
expect(matchRoute({ ...baseRoute, enabled: false }, event())).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("filter-ast pattern engine", () => {
|
||||
test("runs backtracking-prone patterns in linear time", () => {
|
||||
const started = Date.now();
|
||||
const text = `${"a".repeat(50_000)}!`;
|
||||
for (const match of ["/(a+)+$/", "/a+a+a+a+$/", "*a*a*a*a*b"]) {
|
||||
expect(
|
||||
evaluateFilterNode(
|
||||
{ type: "field", path: "body", match },
|
||||
event({ payload: { body: text } }),
|
||||
),
|
||||
).toBe(false);
|
||||
}
|
||||
expect(Date.now() - started).toBeLessThan(2000);
|
||||
});
|
||||
|
||||
test("keeps glob ? and * semantics", () => {
|
||||
const ev = event();
|
||||
expect(evaluateFilterNode({ type: "repo", match: "acme/w?dget" }, ev)).toBe(true);
|
||||
expect(evaluateFilterNode({ type: "repo", match: "acme/*" }, ev)).toBe(true);
|
||||
expect(evaluateFilterNode({ type: "repo", match: "acme" }, ev)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("filter-ast payload paths", () => {
|
||||
test("never reads inherited properties", () => {
|
||||
const node = { type: "field" as const, path: "constructor", match: "x" };
|
||||
expect(evaluateFilterNode(node, event())).toBe(false);
|
||||
expect(evaluateFilterNode({ type: "field", path: "__proto__", op: "exists" }, event())).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
+20
-1
@@ -317,6 +317,7 @@ describe("message title spec", () => {
|
||||
head_sha: "abc123def456",
|
||||
status: "in_progress",
|
||||
conclusion: null,
|
||||
check_suite: { id: 7 },
|
||||
},
|
||||
repository: repo,
|
||||
sender,
|
||||
@@ -331,13 +332,31 @@ describe("message title spec", () => {
|
||||
head_sha: "abc123def456",
|
||||
status: "completed",
|
||||
conclusion: "failure",
|
||||
check_suite: { id: 7 },
|
||||
},
|
||||
repository: repo,
|
||||
sender,
|
||||
}),
|
||||
);
|
||||
expect(running.updateKey).toBe("check_run:acme/widget:Cloudflare Pages:abc123def456");
|
||||
expect(running.updateKey).toBe("check_run:acme/widget:7:Cloudflare Pages:abc123def456");
|
||||
expect(failed.updateKey).toBe(running.updateKey);
|
||||
// A same-named check of another suite (workflow / app) has its own message.
|
||||
const other = formatEvent(
|
||||
route,
|
||||
event("check_run", {
|
||||
check_run: {
|
||||
id: 44,
|
||||
name: "Cloudflare Pages",
|
||||
head_sha: "abc123def456",
|
||||
status: "completed",
|
||||
conclusion: "success",
|
||||
check_suite: { id: 8 },
|
||||
},
|
||||
repository: repo,
|
||||
sender,
|
||||
}),
|
||||
);
|
||||
expect(other.updateKey).not.toBe(running.updateKey);
|
||||
});
|
||||
|
||||
it("check_suite shows conclusion, service, branch and commit", () => {
|
||||
|
||||
@@ -116,7 +116,7 @@ describe("getWorkflowRunJobs", () => {
|
||||
);
|
||||
});
|
||||
|
||||
expect(await getWorkflowRunJobs(JOBS_URL, appId, pem, 555)).toBeDefined();
|
||||
expect(await getWorkflowRunJobs(JOBS_URL, appId, pem, 5551)).toBeDefined();
|
||||
expect(tokenUA).toBeTruthy();
|
||||
expect(sawUA).toBeTruthy();
|
||||
});
|
||||
|
||||
@@ -22,8 +22,9 @@ describe("deliveryKey", () => {
|
||||
expect(deliveryKey("github", "team-a", "deliv-1")).toBe("delivery:github:team-a:deliv-1");
|
||||
});
|
||||
|
||||
it("falls back to 'global' without a tenant", () => {
|
||||
expect(deliveryKey("github", undefined, "deliv-1")).toBe("delivery:github:global:deliv-1");
|
||||
it("uses a scope no group id can take without a tenant", () => {
|
||||
expect(deliveryKey("github", undefined, "deliv-1")).toBe("delivery:github:@global:deliv-1");
|
||||
expect(deliveryKey("github", "global", "deliv-1")).toBe("delivery:github:global:deliv-1");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { isAdminUser } from "../server/lib/web/session";
|
||||
import { createAdminSession, adminCookie } from "../server/lib/web/session";
|
||||
import {
|
||||
bindMemberIds,
|
||||
carryMemberIds,
|
||||
loadGroups,
|
||||
memberRole,
|
||||
saveGroups,
|
||||
} from "../server/lib/web/groups";
|
||||
import { adminApiGroupsGet, adminGroupDelete, adminGroupRoutesGet } from "../server/lib/web/admin";
|
||||
import { saveGroupRoutes, resetConfigStore } from "../server/lib/config";
|
||||
import { createInvite, listInvites } from "../server/lib/web/invites";
|
||||
import {
|
||||
claimOnce,
|
||||
confirmClaim,
|
||||
deliveryKey,
|
||||
idempotencyStore,
|
||||
} from "../server/lib/lib/idempotency";
|
||||
import { handleTelegramUpdate } from "../server/lib/drivers/telegram/commands";
|
||||
import { kvFallbackActive, noteKvFallback } from "../server/lib/storage/d1";
|
||||
import { d1ConfigStore } from "../server/lib/storage/config-store";
|
||||
import { makeEvent, responseStatus } from "./helpers";
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
import type { Env, Group, Route } from "../server/lib/types";
|
||||
|
||||
const route = (id: string, groupId: string): Route => ({
|
||||
id,
|
||||
name: id,
|
||||
enabled: true,
|
||||
filters: [{ type: "event", match: "push" }],
|
||||
targets: [{ platform: "discord", channelId: "c1" }],
|
||||
groupId,
|
||||
});
|
||||
|
||||
describe("member identity", () => {
|
||||
it("never matches one configured string both as a user id and as a login", () => {
|
||||
const env = { ADMIN_USER_IDS: "123, alice" } as Env;
|
||||
// User id 123 is the admin; a different account whose login is "123" is not.
|
||||
expect(isAdminUser(env, "123", "someone")).toBe(true);
|
||||
expect(isAdminUser(env, "999", "123")).toBe(false);
|
||||
expect(isAdminUser(env, "5", "ALICE")).toBe(true);
|
||||
});
|
||||
|
||||
it("matches a bound member by user id only, so a reclaimed login inherits nothing", () => {
|
||||
const group: Group = {
|
||||
id: "g",
|
||||
name: "G",
|
||||
adminIds: [],
|
||||
members: [{ login: "alice", role: "owner", userId: "42" }],
|
||||
};
|
||||
expect(memberRole(group, "42", "alice-renamed")).toBe("owner");
|
||||
// Someone who took over the old login after a rename.
|
||||
expect(memberRole(group, "77", "alice")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("binds a member's user id on sign-in and keeps it across member edits", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
resetConfigStore(kv);
|
||||
await saveGroups(kv, [
|
||||
{
|
||||
id: "g",
|
||||
name: "G",
|
||||
adminIds: [],
|
||||
members: [
|
||||
{ login: "alice", role: "owner" },
|
||||
{ login: "bob", role: "viewer" },
|
||||
],
|
||||
},
|
||||
]);
|
||||
await bindMemberIds(kv, await loadGroups(kv, { fresh: true }), "42", "Alice");
|
||||
const stored = (await loadGroups(kv, { fresh: true }))[0]!;
|
||||
expect(stored.members).toEqual([
|
||||
{ login: "alice", role: "owner", userId: "42" },
|
||||
{ login: "bob", role: "viewer" },
|
||||
]);
|
||||
// A client edit (which never carries ids) keeps the binding.
|
||||
const edited = carryMemberIds(
|
||||
{
|
||||
...stored,
|
||||
members: [
|
||||
{ login: "alice", role: "owner" },
|
||||
{ login: "bob", role: "admin" },
|
||||
],
|
||||
},
|
||||
stored,
|
||||
);
|
||||
expect(edited.members![0]).toEqual({ login: "alice", role: "owner", userId: "42" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("group deletion", () => {
|
||||
it("refuses to delete a group whose routes changed after the deletion was confirmed", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
resetConfigStore(kv);
|
||||
await saveGroups(kv, [
|
||||
{ id: "g", name: "G", adminIds: [], members: [{ login: "alice", role: "owner" }] },
|
||||
]);
|
||||
const env = { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: {} as D1Database } as Env;
|
||||
const sessionId = await createAdminSession(kv, "42", "alice");
|
||||
const headers = { cookie: adminCookie(sessionId) };
|
||||
const view = makeEvent("/admin/api/groups/g/routes", { headers, env });
|
||||
const { revision } = (await adminGroupRoutesGet(view, "g")) as { revision: string };
|
||||
const listed = (await adminApiGroupsGet(makeEvent("/admin/api/groups", { headers, env }))) as {
|
||||
groups: Array<{ revision: string }>;
|
||||
};
|
||||
const groupRev = listed.groups[0]!.revision;
|
||||
// Someone adds a route after the owner saw an empty group.
|
||||
await saveGroupRoutes(kv, "g", [route("r1", "g")]);
|
||||
const del = makeEvent(`/admin/api/groups/g?revision=${groupRev}&routesRevision=${revision}`, {
|
||||
method: "DELETE",
|
||||
headers,
|
||||
env,
|
||||
});
|
||||
await adminGroupDelete(del, "g");
|
||||
expect(responseStatus(del)).toBe(409);
|
||||
expect((await loadGroups(kv, { fresh: true })).map((g) => g.id)).toEqual(["g"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("one-time claims", () => {
|
||||
it("tells a confirmed run from one left unconfirmed", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
const store = idempotencyStore({} as D1Database, kv);
|
||||
expect(await claimOnce(store, "k", 60)).toBe("claimed");
|
||||
expect(await claimOnce(store, "k", 60)).toBe("pending");
|
||||
await confirmClaim(store, "k", 600);
|
||||
expect(await claimOnce(store, "k", 60)).toBe("done");
|
||||
// A delivery id crafted to look like a confirmation never passes for one.
|
||||
await confirmClaim(store, deliveryKey("custom", "g", "x#accepted"), 600);
|
||||
await confirmClaim(store, deliveryKey("custom", "g", "accepted:x"), 600);
|
||||
expect(await claimOnce(store, deliveryKey("custom", "g", "x"), 60)).toBe("claimed");
|
||||
});
|
||||
|
||||
it("makes Telegram redeliver an update whose earlier run is unconfirmed", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
const env = { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: {} as D1Database } as Env;
|
||||
// An earlier run took the update and could neither finish nor give it back.
|
||||
await idempotencyStore(env.DB, kv).claim("bot:telegram::7", 300);
|
||||
const update = { update_id: 7, message: { text: "/gh", chat: { id: 1 } } };
|
||||
expect(handleTelegramUpdate(env, update)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("invite listing", () => {
|
||||
it("lists both of two invites whose index writes overwrote each other", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
const invite = {
|
||||
groupId: "team",
|
||||
role: "viewer" as const,
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
};
|
||||
const a = await createInvite(kv, invite);
|
||||
const b = await createInvite(kv, invite);
|
||||
// The second creation read the index before the first wrote it.
|
||||
await kv.put("invite:group:team", JSON.stringify([b]));
|
||||
expect((await listInvites(kv, "team")).map((i) => i.token).sort()).toEqual([a, b].sort());
|
||||
});
|
||||
});
|
||||
|
||||
describe("storage fallbacks", () => {
|
||||
it("retries the KV fallback marker until it is actually stored", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
const put = kv.put.bind(kv);
|
||||
let failing = true;
|
||||
kv.put = async (key: string, value: string, opts?: KVNamespacePutOptions): Promise<void> => {
|
||||
if (failing) throw new Error("KV down");
|
||||
return put(key, value, opts);
|
||||
};
|
||||
await expect(noteKvFallback(kv)).rejects.toThrow();
|
||||
failing = false;
|
||||
await noteKvFallback(kv);
|
||||
expect(Number(store.get("storage:kv-fallback"))).toBeGreaterThan(Date.now());
|
||||
expect(await kvFallbackActive(kv)).toBe(true);
|
||||
});
|
||||
|
||||
it("remembers KV-only state, so a D1 binding added later still honors it", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
const kvOnly = idempotencyStore({} as D1Database, kv);
|
||||
expect(await kvOnly.claim("delivery:github:@global:d9", 600)).toBe(true);
|
||||
expect(store.has("storage:kv-fallback")).toBe(true);
|
||||
// The deployment gets D1 (tables migrated) in a fresh isolate.
|
||||
const { db } = createSqliteD1();
|
||||
const { kv: sameKv } = { kv: { ...kv } as KVNamespace };
|
||||
expect(await idempotencyStore(db, sameKv).claim("delivery:github:@global:d9", 600)).toBe(false);
|
||||
});
|
||||
|
||||
it("never re-seeds a configuration another isolate seeded and an admin emptied", async () => {
|
||||
const { db } = createSqliteD1();
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:groups", JSON.stringify([{ id: "legacy", name: "L", adminIds: [] }]));
|
||||
store.set("config:routes", JSON.stringify([route("r1", "legacy")]));
|
||||
const first = d1ConfigStore(db, kv);
|
||||
expect((await first.loadGroups({ fresh: true })).map((g) => g.id)).toEqual(["legacy"]);
|
||||
await first.deleteGroup("legacy");
|
||||
// A second isolate that had already passed the KV marker check.
|
||||
store.delete("config:d1-seeded");
|
||||
const late = d1ConfigStore(db, kv);
|
||||
expect(await late.loadGroups({ fresh: true })).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps listing invites when the index cannot be read, instead of dropping them", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
await createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "viewer",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
});
|
||||
const get = kv.get.bind(kv);
|
||||
kv.get = (async (key: string, type?: never) => {
|
||||
if (key === "invite:group:team") throw new Error("KV down");
|
||||
return get(key, type);
|
||||
}) as typeof kv.get;
|
||||
await expect(
|
||||
createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "viewer",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
kv.get = get;
|
||||
// Neither invite is lost from the list: the half-created one is listed
|
||||
// through its own marker, so it can still be found and revoked.
|
||||
expect(await listInvites(kv, "team")).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,560 @@
|
||||
import { describe, it, expect, afterEach } from "bun:test";
|
||||
import { createHmac } from "crypto";
|
||||
import { processWebhook } from "../server/lib/webhook";
|
||||
import { dispatchEvent } from "../server/lib/core/dispatch";
|
||||
import { resetConfigStore } from "../server/lib/config";
|
||||
import { githubApiUrl } from "../server/lib/github/app-token";
|
||||
import { htmlToText } from "../server/lib/formatters/helpers";
|
||||
import { formatEvent } from "../server/lib/formatters";
|
||||
import { renderNeutralMessage as renderTelegram } from "../server/lib/drivers/telegram/render";
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
import type { Config, Env, Group, Route } from "../server/lib/types";
|
||||
import type { DeliveryMessage, DispatchSummary } from "../server/lib/queue/delivery";
|
||||
|
||||
const restoredFetch = globalThis.fetch;
|
||||
afterEach(() => {
|
||||
globalThis.fetch = restoredFetch;
|
||||
});
|
||||
|
||||
function sign(body: string, secret: string): string {
|
||||
return `sha256=${createHmac("sha256", secret).update(body).digest("hex")}`;
|
||||
}
|
||||
|
||||
function config(routes: Route[]): Config {
|
||||
return {
|
||||
baseUrl: "https://x",
|
||||
github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" },
|
||||
discord: { token: "t" },
|
||||
routes,
|
||||
};
|
||||
}
|
||||
|
||||
function route(id: string, groupId: string, extra: Partial<Route> = {}): Route {
|
||||
return {
|
||||
id,
|
||||
name: id,
|
||||
enabled: true,
|
||||
filters: [{ type: "event", match: ["push", "workflow_run", "issues"] }],
|
||||
targets: [{ platform: "discord", channelId: "c1" }],
|
||||
groupId,
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
const group = (id: string, extra: Partial<Group> = {}): Group => ({
|
||||
id,
|
||||
name: id,
|
||||
adminIds: [],
|
||||
...extra,
|
||||
});
|
||||
|
||||
describe("webhook ingress", () => {
|
||||
function tenantEnv(queue?: Queue, routes: Route[] = [route("r", "team")]): Env {
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:groups", JSON.stringify([group("team")]));
|
||||
store.set("config:routes", JSON.stringify(routes));
|
||||
store.set("tenant:team", "tenant-secret");
|
||||
resetConfigStore(kv);
|
||||
return {
|
||||
GITHUB_WEBHOOK_SECRET: "global",
|
||||
DISCORD_TOKEN: "t",
|
||||
KV: kv,
|
||||
DB: {} as D1Database,
|
||||
QUEUE: queue,
|
||||
};
|
||||
}
|
||||
|
||||
it("never lets an accepted installation event provision again when redelivered", async () => {
|
||||
const sent: DeliveryMessage[] = [];
|
||||
const queue = { send: async (m: DeliveryMessage) => void sent.push(m) } as unknown as Queue;
|
||||
const env = tenantEnv(queue);
|
||||
const body = JSON.stringify({
|
||||
action: "created",
|
||||
installation: { id: 123, account: { login: "acme" } },
|
||||
});
|
||||
const headers = {
|
||||
"x-github-event": "installation",
|
||||
"x-github-delivery": "inst-dup",
|
||||
"x-hub-signature-256": sign(body, "global"),
|
||||
};
|
||||
expect((await processWebhook(env, body, headers, () => undefined)).status).toBe(200);
|
||||
const { loadGroups, updateGroup } = await import("../server/lib/web/groups");
|
||||
const created = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "inst-123");
|
||||
expect(created?.installationId).toBe(123);
|
||||
// A super admin unbinds it; the same delivery arrives again.
|
||||
await updateGroup(env.KV, "inst-123", (g) => ({ ...g, installationId: undefined }));
|
||||
const again = await processWebhook(env, body, headers, () => undefined);
|
||||
expect(again.body).toMatchObject({ duplicate: true });
|
||||
const after = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "inst-123");
|
||||
expect(after?.installationId).toBeUndefined();
|
||||
});
|
||||
|
||||
it("asks for a retry when installation provisioning failed, queueing nothing", async () => {
|
||||
const sent: DeliveryMessage[] = [];
|
||||
const queue = { send: async (m: DeliveryMessage) => void sent.push(m) } as unknown as Queue;
|
||||
const env = tenantEnv(queue);
|
||||
const kvPut = env.KV.put.bind(env.KV);
|
||||
env.KV.put = (async (key: string, ...rest: unknown[]): Promise<void> =>
|
||||
key === "config:groups"
|
||||
? Promise.reject(new Error("KV down"))
|
||||
: (kvPut as (...a: unknown[]) => Promise<void>)(key, ...rest)) as KVNamespace["put"];
|
||||
const body = JSON.stringify({
|
||||
action: "created",
|
||||
installation: { id: 99, account: { login: "acme" } },
|
||||
});
|
||||
const res = await processWebhook(
|
||||
env,
|
||||
body,
|
||||
{
|
||||
"x-github-event": "installation",
|
||||
"x-github-delivery": "inst-1",
|
||||
"x-hub-signature-256": sign(body, "global"),
|
||||
},
|
||||
() => undefined,
|
||||
);
|
||||
expect(res.status).toBe(503);
|
||||
expect(sent).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("never passes a claim it could not give back for an accepted duplicate", async () => {
|
||||
let fail = true;
|
||||
const sent: DeliveryMessage[] = [];
|
||||
const queue = {
|
||||
send: async (m: DeliveryMessage) => {
|
||||
if (fail) throw new Error("queue down");
|
||||
sent.push(m);
|
||||
},
|
||||
} as unknown as Queue;
|
||||
const env = tenantEnv(queue);
|
||||
// Giving the claim back fails too.
|
||||
const kvDelete = env.KV.delete.bind(env.KV);
|
||||
env.KV.delete = async (key: string): Promise<void> =>
|
||||
key.startsWith("delivery:") ? Promise.reject(new Error("KV down")) : kvDelete(key);
|
||||
const body = JSON.stringify({ ref: "refs/heads/main" });
|
||||
const headers = {
|
||||
"x-github-event": "push",
|
||||
"x-github-delivery": "d-3",
|
||||
"x-hub-signature-256": sign(body, "tenant-secret"),
|
||||
};
|
||||
expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(503);
|
||||
fail = false;
|
||||
// The leftover claim is not confirmed: the retry is told to come back,
|
||||
// never acknowledged as a duplicate of an event that was never queued.
|
||||
const retry = await processWebhook(env, body, headers, () => undefined, "team");
|
||||
expect(retry.status).toBe(503);
|
||||
expect(sent).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("keeps one delivery identity for a nonce-signed request retried after an unclear enqueue", async () => {
|
||||
const sent: DeliveryMessage[] = [];
|
||||
let first = true;
|
||||
const queue = {
|
||||
send: async (m: DeliveryMessage) => {
|
||||
sent.push(m);
|
||||
// The queue took it, but the answer was lost.
|
||||
if (first) {
|
||||
first = false;
|
||||
throw new Error("connection reset");
|
||||
}
|
||||
},
|
||||
} as unknown as Queue;
|
||||
const env = tenantEnv(queue);
|
||||
const body = JSON.stringify({ alert: "disk full" });
|
||||
const ts = String(Math.floor(Date.now() / 1000));
|
||||
const headers = {
|
||||
"x-webhooker-signature": `sha256=${createHmac("sha256", "tenant-secret")
|
||||
.update(`${ts}.n-1.${body}`)
|
||||
.digest("hex")}`,
|
||||
"x-webhooker-timestamp": ts,
|
||||
"x-webhooker-nonce": "n-1",
|
||||
};
|
||||
expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(503);
|
||||
expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(200);
|
||||
expect(sent).toHaveLength(2);
|
||||
// Same delivery: the consumer recognizes the second copy.
|
||||
expect(sent[1]!.deliveryId).toBe(sent[0]!.deliveryId);
|
||||
});
|
||||
|
||||
it("asks for a retry on a transient failure even without a delivery id", async () => {
|
||||
const env = tenantEnv(undefined, [
|
||||
route("r", "team", {
|
||||
event: undefined,
|
||||
filters: [{ type: "event", match: "custom" }],
|
||||
targets: [
|
||||
{ platform: "discord", channelId: "ok" },
|
||||
{ platform: "discord", channelId: "flaky" },
|
||||
],
|
||||
}),
|
||||
]);
|
||||
globalThis.fetch = async (input): Promise<Response> =>
|
||||
String(input).includes("/channels/flaky/")
|
||||
? new Response(JSON.stringify({ retry_after: 999 }), { status: 429 })
|
||||
: new Response(JSON.stringify({ id: "m" }), { status: 200 });
|
||||
const body = JSON.stringify({ alert: "disk full" });
|
||||
const headers = { "x-webhooker-signature": sign(body, "tenant-secret") };
|
||||
// No identity to skip what was sent: a duplicate beats a lost target.
|
||||
const res = await processWebhook(env, body, headers, () => undefined, "team");
|
||||
expect(res.status).toBe(503);
|
||||
});
|
||||
|
||||
it("answers an inline dispatch after it ran and asks for a retry on a transient failure", async () => {
|
||||
const env = tenantEnv(undefined, [
|
||||
route("r", "team", {
|
||||
targets: [
|
||||
{ platform: "discord", channelId: "ok" },
|
||||
{ platform: "discord", channelId: "flaky" },
|
||||
],
|
||||
}),
|
||||
]);
|
||||
const calls: Record<string, number> = {};
|
||||
let limited = true;
|
||||
globalThis.fetch = async (input): Promise<Response> => {
|
||||
const channel = String(input).split("/channels/")[1]!.split("/")[0]!;
|
||||
calls[channel] = (calls[channel] ?? 0) + 1;
|
||||
if (channel === "flaky" && limited) {
|
||||
return new Response(JSON.stringify({ retry_after: 999 }), { status: 429 });
|
||||
}
|
||||
return new Response(JSON.stringify({ id: "m" }), { status: 200 });
|
||||
};
|
||||
const body = JSON.stringify({ ref: "refs/heads/main" });
|
||||
const headers = {
|
||||
"x-github-event": "push",
|
||||
"x-github-delivery": "d-4",
|
||||
"x-hub-signature-256": sign(body, "tenant-secret"),
|
||||
};
|
||||
const first = await processWebhook(env, body, headers, () => undefined, "team");
|
||||
expect(first.status).toBe(503);
|
||||
limited = false;
|
||||
const retry = await processWebhook(env, body, headers, () => undefined, "team");
|
||||
expect(retry.status).toBe(200);
|
||||
// The target sent the first time is not sent again.
|
||||
expect(calls).toEqual({ ok: 1, flaky: 2 });
|
||||
const again = await processWebhook(env, body, headers, () => undefined, "team");
|
||||
expect(again.body).toMatchObject({ duplicate: true });
|
||||
});
|
||||
|
||||
it("answers 503 and releases the dedup claim when the queue rejects the event", async () => {
|
||||
let fail = true;
|
||||
const sent: DeliveryMessage[] = [];
|
||||
const queue = {
|
||||
send: async (m: DeliveryMessage) => {
|
||||
if (fail) throw new Error("queue down");
|
||||
sent.push(m);
|
||||
},
|
||||
} as unknown as Queue;
|
||||
const env = tenantEnv(queue);
|
||||
const body = JSON.stringify({ ref: "refs/heads/main" });
|
||||
const headers = {
|
||||
"x-github-event": "push",
|
||||
"x-github-delivery": "d-1",
|
||||
"x-hub-signature-256": sign(body, "tenant-secret"),
|
||||
};
|
||||
const first = await processWebhook(env, body, headers, () => undefined, "team");
|
||||
expect(first.status).toBe(503);
|
||||
fail = false;
|
||||
// The sender's redelivery is accepted, not dropped as a duplicate.
|
||||
const retry = await processWebhook(env, body, headers, () => undefined, "team");
|
||||
expect(retry.status).toBe(200);
|
||||
expect((retry.body as { duplicate?: boolean }).duplicate).toBeUndefined();
|
||||
expect(sent).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("drops the installation claim of tenant-signed payloads", async () => {
|
||||
const sent: DeliveryMessage[] = [];
|
||||
const queue = { send: async (m: DeliveryMessage) => void sent.push(m) } as unknown as Queue;
|
||||
const env = tenantEnv(queue);
|
||||
const body = JSON.stringify({
|
||||
action: "created",
|
||||
installation: { id: 999, account: { login: "victim" } },
|
||||
});
|
||||
const headers = {
|
||||
"x-github-event": "installation",
|
||||
"x-github-delivery": "d-2",
|
||||
"x-hub-signature-256": sign(body, "tenant-secret"),
|
||||
};
|
||||
const res = await processWebhook(env, body, headers, () => undefined, "team");
|
||||
expect(res.status).toBe(200);
|
||||
expect(sent[0]!.installationId).toBeUndefined();
|
||||
// No group was provisioned for the claimed installation.
|
||||
const groups = JSON.parse((await env.KV.get("config:groups"))!) as Group[];
|
||||
expect(groups.map((g) => g.id)).toEqual(["team"]);
|
||||
});
|
||||
|
||||
it("never lets a custom delivery id take the key of a nonce-identified request", async () => {
|
||||
const env = tenantEnv(undefined, [
|
||||
route("r", "team", {
|
||||
event: undefined,
|
||||
filters: [{ type: "event", match: "custom" }],
|
||||
targets: [{ platform: "discord", channelId: "ok" }],
|
||||
}),
|
||||
]);
|
||||
let sends = 0;
|
||||
globalThis.fetch = async (): Promise<Response> => {
|
||||
sends++;
|
||||
return new Response(JSON.stringify({ id: "m" }), { status: 200 });
|
||||
};
|
||||
const { replayNonceKey } = await import("../server/lib/providers/custom");
|
||||
const nonce = "n-77";
|
||||
// Event A names, as its delivery id, exactly the identity event B's nonce gets.
|
||||
const a = JSON.stringify({ deliveryId: `n:${await replayNonceKey("tenant-secret", nonce)}` });
|
||||
expect(
|
||||
(
|
||||
await processWebhook(
|
||||
env,
|
||||
a,
|
||||
{ "x-webhooker-signature": sign(a, "tenant-secret") },
|
||||
() => undefined,
|
||||
"team",
|
||||
)
|
||||
).status,
|
||||
).toBe(200);
|
||||
const b = JSON.stringify({ alert: "other event" });
|
||||
const ts = String(Math.floor(Date.now() / 1000));
|
||||
const res = await processWebhook(
|
||||
env,
|
||||
b,
|
||||
{
|
||||
"x-webhooker-signature": `sha256=${createHmac("sha256", "tenant-secret")
|
||||
.update(`${ts}.${nonce}.${b}`)
|
||||
.digest("hex")}`,
|
||||
"x-webhooker-timestamp": ts,
|
||||
"x-webhooker-nonce": nonce,
|
||||
},
|
||||
() => undefined,
|
||||
"team",
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect((res.body as { duplicate?: boolean }).duplicate).toBeUndefined();
|
||||
expect(sends).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("dispatch", () => {
|
||||
function env(): Env {
|
||||
return { GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: createSqliteD1().db };
|
||||
}
|
||||
|
||||
it("records a formatter crash as a permanent failure instead of dropping it", async () => {
|
||||
globalThis.fetch = async (): Promise<Response> => new Response("{}", { status: 200 });
|
||||
const e = env();
|
||||
const summary = await dispatchEvent(
|
||||
config([route("r", "g")]),
|
||||
// `issue: null` makes the issues formatter throw.
|
||||
{ event: "issues", payload: { issue: null } as never },
|
||||
e,
|
||||
[group("g")],
|
||||
);
|
||||
expect(summary.failures).toHaveLength(1);
|
||||
expect(summary.failures[0]!.errorCode).toBe("FORMAT");
|
||||
});
|
||||
|
||||
it("skips targets a previous attempt already finished", async () => {
|
||||
const urls: string[] = [];
|
||||
globalThis.fetch = async (input): Promise<Response> => {
|
||||
urls.push(String(input));
|
||||
return new Response(JSON.stringify({ id: "m" }), { status: 200 });
|
||||
};
|
||||
const r = route("r", "g", {
|
||||
targets: [
|
||||
{ platform: "discord", channelId: "done" },
|
||||
{ platform: "discord", channelId: "todo" },
|
||||
],
|
||||
});
|
||||
const summary = await dispatchEvent(
|
||||
config([r]),
|
||||
{ event: "push", payload: {} },
|
||||
env(),
|
||||
[group("g")],
|
||||
{ skipTarget: async (key) => key.endsWith(":done") },
|
||||
);
|
||||
expect(urls.some((u) => u.includes("/done/"))).toBe(false);
|
||||
expect(urls.some((u) => u.includes("/todo/"))).toBe(true);
|
||||
expect(summary.succeeded).toEqual(["g:r:discord:todo"]);
|
||||
});
|
||||
|
||||
it("never mints App tokens for tenant events and only calls the GitHub API", async () => {
|
||||
const urls: string[] = [];
|
||||
globalThis.fetch = async (input): Promise<Response> => {
|
||||
urls.push(String(input));
|
||||
return new Response(JSON.stringify({ id: "m" }), { status: 200 });
|
||||
};
|
||||
const e = { ...env(), GITHUB_APP_ID: "1", GITHUB_PRIVATE_KEY: "pem" };
|
||||
await dispatchEvent(
|
||||
config([route("r", "g")]),
|
||||
{
|
||||
event: "workflow_run",
|
||||
payload: { workflow_run: { id: 1, jobs_url: "https://evil.example/steal" } },
|
||||
installationId: 42,
|
||||
},
|
||||
e,
|
||||
[group("g")],
|
||||
{ tenantId: "g" },
|
||||
);
|
||||
expect(urls.some((u) => u.includes("evil.example"))).toBe(false);
|
||||
expect(urls.some((u) => u.includes("access_tokens"))).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps editing the same message after a transient edit failure", async () => {
|
||||
const calls: Array<{ method: string; url: string }> = [];
|
||||
let editStatus = 200;
|
||||
globalThis.fetch = async (input, init): Promise<Response> => {
|
||||
const method = init?.method ?? "GET";
|
||||
calls.push({ method, url: String(input) });
|
||||
if (method === "PATCH") {
|
||||
return editStatus === 200
|
||||
? new Response(JSON.stringify({ id: "m1" }), { status: 200 })
|
||||
: new Response("bad request", { status: editStatus });
|
||||
}
|
||||
return new Response(JSON.stringify({ id: "m1" }), { status: 200 });
|
||||
};
|
||||
const e = env();
|
||||
const run = (status: string, updated: string): Promise<DispatchSummary> =>
|
||||
dispatchEvent(
|
||||
config([route("r", "g")]),
|
||||
{
|
||||
event: "workflow_run",
|
||||
provider: "github",
|
||||
payload: {
|
||||
action: "in_progress",
|
||||
repository: { full_name: "o/r", html_url: "https://github.com/o/r" },
|
||||
workflow_run: { id: 7, status, name: "CI", updated_at: updated, jobs: [{}] },
|
||||
},
|
||||
},
|
||||
e,
|
||||
[group("g")],
|
||||
);
|
||||
await run("queued", "2026-01-01T00:00:00Z");
|
||||
expect(calls.filter((c) => c.method === "POST")).toHaveLength(1);
|
||||
editStatus = 400;
|
||||
const failed = await run("in_progress", "2026-01-01T00:01:00Z");
|
||||
expect(failed.failures).toHaveLength(1);
|
||||
// No second message was posted for the same chain.
|
||||
expect(calls.filter((c) => c.method === "POST")).toHaveLength(1);
|
||||
editStatus = 200;
|
||||
// An older event arriving late does not overwrite the newer state.
|
||||
const before = calls.length;
|
||||
await run("queued", "2025-12-31T00:00:00Z");
|
||||
expect(calls.length).toBe(before);
|
||||
});
|
||||
|
||||
it("separates update chains of two forges with the same repository name", async () => {
|
||||
const posts: string[] = [];
|
||||
globalThis.fetch = async (input, init): Promise<Response> => {
|
||||
if ((init?.method ?? "GET") === "POST") posts.push(String(input));
|
||||
return new Response(JSON.stringify({ id: `m${posts.length}` }), { status: 200 });
|
||||
};
|
||||
const e = env();
|
||||
for (const host of ["git1.example.com", "git2.example.com"]) {
|
||||
await dispatchEvent(
|
||||
config([route("r", "g")]),
|
||||
{
|
||||
event: "workflow_run",
|
||||
provider: "gitea",
|
||||
payload: {
|
||||
repository: { full_name: "o/r", html_url: `https://${host}/o/r` },
|
||||
workflow_run: { id: 7, status: "queued", name: "CI", jobs: [{}] },
|
||||
},
|
||||
},
|
||||
e,
|
||||
[group("g")],
|
||||
);
|
||||
}
|
||||
expect(posts).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatting safety", () => {
|
||||
it("keeps out-of-range character references verbatim", () => {
|
||||
expect(htmlToText("a � b A")).toBe("a � b A");
|
||||
});
|
||||
|
||||
it("offers merge/close buttons only for github.com", () => {
|
||||
const payload = {
|
||||
action: "opened",
|
||||
repository: { full_name: "o/r", html_url: "https://gitea.example/o/r" },
|
||||
pull_request: { number: 1, state: "open", title: "t" },
|
||||
};
|
||||
const r = route("r", "g");
|
||||
expect(formatEvent(r, { event: "pull_request", provider: "gitea", payload }).actions).toBe(
|
||||
undefined,
|
||||
);
|
||||
const gh = {
|
||||
...payload,
|
||||
repository: { full_name: "o/r", html_url: "https://github.com/o/r" },
|
||||
};
|
||||
expect(
|
||||
formatEvent(r, { event: "pull_request", provider: "github", payload: gh }).actions,
|
||||
).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("shows the deployment state, not the status object", () => {
|
||||
const message = formatEvent(route("r", "g"), {
|
||||
event: "deployment_status",
|
||||
payload: {
|
||||
repository: { full_name: "o/r" },
|
||||
deployment_status: { state: "success", environment: "prod" },
|
||||
deployment: { id: 1 },
|
||||
},
|
||||
});
|
||||
const status = message.fields!.find((f) => /status/i.test(f.name))!;
|
||||
expect(status.value).toContain("success");
|
||||
expect(status.value).not.toContain("[object Object]");
|
||||
});
|
||||
|
||||
it("drops Telegram's author emoji when the group turned emoji off", () => {
|
||||
const html = renderTelegram({ title: "t", author: { name: "bob" }, emoji: false });
|
||||
expect(html).not.toContain("👤");
|
||||
});
|
||||
|
||||
it("accepts only GitHub REST repository URLs for enrichment", () => {
|
||||
expect(githubApiUrl("https://api.github.com/repos/o/r/actions/runs/1/jobs")).not.toBeNull();
|
||||
expect(githubApiUrl("https://evil.example/repos/o/r")).toBeNull();
|
||||
expect(githubApiUrl("http://api.github.com/repos/o/r")).toBeNull();
|
||||
expect(githubApiUrl("https://api.github.com/user")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("round-trip safety", () => {
|
||||
it("records an unreadable delivery ledger as a retryable failure", async () => {
|
||||
globalThis.fetch = async (): Promise<Response> =>
|
||||
new Response(JSON.stringify({ id: "m" }), { status: 200 });
|
||||
const summary = await dispatchEvent(
|
||||
config([route("r", "g")]),
|
||||
{ event: "push", payload: {} },
|
||||
{ GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: createSqliteD1().db },
|
||||
[group("g")],
|
||||
{
|
||||
skipTarget: async () => {
|
||||
throw new Error("D1 down");
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(summary.failures).toHaveLength(1);
|
||||
expect(summary.failures[0]!.errorCode).toBeUndefined();
|
||||
});
|
||||
|
||||
it("gives the custom-webhook nonce back when the queue rejects the event", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:groups", JSON.stringify([group("team")]));
|
||||
store.set("tenant:team", "tenant-secret");
|
||||
resetConfigStore(kv);
|
||||
let fail = true;
|
||||
const queue = {
|
||||
send: async (): Promise<void> => {
|
||||
if (fail) throw new Error("queue down");
|
||||
},
|
||||
} as unknown as Queue;
|
||||
const env: Env = { GITHUB_WEBHOOK_SECRET: "g", KV: kv, DB: {} as D1Database, QUEUE: queue };
|
||||
const body = JSON.stringify({ title: "hi" });
|
||||
const ts = String(Math.floor(Date.now() / 1000));
|
||||
const nonce = "n-1";
|
||||
const headers = {
|
||||
"x-webhooker-signature": `sha256=${createHmac("sha256", "tenant-secret").update(`${ts}.${nonce}.${body}`).digest("hex")}`,
|
||||
"x-webhooker-timestamp": ts,
|
||||
"x-webhooker-nonce": nonce,
|
||||
};
|
||||
expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(503);
|
||||
fail = false;
|
||||
expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(200);
|
||||
});
|
||||
});
|
||||
+146
-30
@@ -1,4 +1,4 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
|
||||
import { describe, it, expect, beforeAll, beforeEach, afterEach } from "bun:test";
|
||||
import {
|
||||
handleOAuthStart,
|
||||
handleInstallPage,
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
import { createAdminSession, adminCookie } from "../server/lib/web/session";
|
||||
import { loadGroups, invalidateGroupsCache } from "../server/lib/web/groups";
|
||||
import { getInstallationAccount } from "../server/lib/github/oauth";
|
||||
import { saveToken } from "../server/lib/github/store";
|
||||
import { makeEvent, responseStatus, responseHeader } from "./helpers";
|
||||
import type { Env } from "../server/lib/types";
|
||||
|
||||
@@ -46,15 +47,42 @@ function createMockDB(): D1Database {
|
||||
} as unknown as D1Database;
|
||||
}
|
||||
|
||||
/** App credentials, so the install flow can look the installation's account up. */
|
||||
let appCredentials: Partial<Env> = {};
|
||||
|
||||
function createEnv(overrides: Partial<Env> = {}): Env {
|
||||
return {
|
||||
GITHUB_WEBHOOK_SECRET: "secret",
|
||||
KV: createMockKV(),
|
||||
DB: createMockDB(),
|
||||
...appCredentials,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
/** The signed-in test user's role in the installation's organization. */
|
||||
let orgRole = "admin";
|
||||
|
||||
/** GitHub API stub: the installation lookup and the user's org membership. */
|
||||
function githubApiMock(input: RequestInfo | URL): Promise<Response> {
|
||||
const url = String(input);
|
||||
const body = url.includes("/user/memberships/orgs/myorg")
|
||||
? { state: "active", role: orgRole }
|
||||
: { id: 555, account: { login: "myorg", id: 9, type: "Organization" } };
|
||||
return Promise.resolve(
|
||||
new Response(JSON.stringify(body), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/** A session for alice (GitHub id 1001) with a stored OAuth token. */
|
||||
async function signIn(kv: KVNamespace): Promise<string> {
|
||||
await saveToken(kv, "1001", "gho_alice", 3600);
|
||||
return adminCookie(await createAdminSession(kv, "1001", "alice"));
|
||||
}
|
||||
|
||||
function bufToPem(buf: ArrayBuffer): string {
|
||||
const bytes = new Uint8Array(buf);
|
||||
let s = "";
|
||||
@@ -122,17 +150,15 @@ describe("getInstallationAccount", () => {
|
||||
describe("GET /auth/github/install", () => {
|
||||
const restoredFetch = globalThis.fetch;
|
||||
|
||||
beforeAll(async () => {
|
||||
const { appId, pem } = await makeAppKeyPair();
|
||||
appCredentials = { GITHUB_APP_ID: appId, GITHUB_PRIVATE_KEY: pem };
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
invalidateGroupsCache();
|
||||
globalThis.fetch = (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
void init;
|
||||
return Promise.resolve(
|
||||
new Response(JSON.stringify({ id: 555, account: { login: "myorg" } }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
);
|
||||
};
|
||||
orgRole = "admin";
|
||||
globalThis.fetch = githubApiMock;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -172,10 +198,10 @@ describe("GET /auth/github/install", () => {
|
||||
]),
|
||||
);
|
||||
const env = createEnv({ KV: kv });
|
||||
const sessionId = await createAdminSession(kv, "1001", "alice");
|
||||
const cookie = await signIn(kv);
|
||||
|
||||
const event = makeEvent("/auth/github/install?installation_id=555&setup_action=install", {
|
||||
headers: { cookie: adminCookie(sessionId), accept: "text/html" },
|
||||
headers: { cookie, accept: "text/html" },
|
||||
env,
|
||||
});
|
||||
const html = (await handleInstallPage(event)) as string;
|
||||
@@ -185,6 +211,19 @@ describe("GET /auth/github/install", () => {
|
||||
expect(html).not.toContain('value="theirs"');
|
||||
});
|
||||
|
||||
it("refuses an installation the user cannot manage", async () => {
|
||||
const kv = createMockKV();
|
||||
orgRole = "member";
|
||||
const env = createEnv({ KV: kv });
|
||||
const cookie = await signIn(kv);
|
||||
const event = makeEvent("/auth/github/install?installation_id=555", {
|
||||
headers: { cookie, accept: "text/html" },
|
||||
env,
|
||||
});
|
||||
await handleInstallPage(event);
|
||||
expect(responseHeader(event, "location")).toBe("/admin?error=install-forbidden");
|
||||
});
|
||||
|
||||
it("rejects a missing installation id", async () => {
|
||||
const env = createEnv();
|
||||
const event = makeEvent("/auth/github/install", { headers: { accept: "text/html" }, env });
|
||||
@@ -197,15 +236,8 @@ describe("POST /auth/github/install/bind", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
invalidateGroupsCache();
|
||||
globalThis.fetch = (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
void init;
|
||||
return Promise.resolve(
|
||||
new Response(JSON.stringify({ id: 555, account: { login: "myorg" } }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
);
|
||||
};
|
||||
orgRole = "admin";
|
||||
globalThis.fetch = githubApiMock;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -242,9 +274,9 @@ describe("POST /auth/github/install/bind", () => {
|
||||
]),
|
||||
);
|
||||
const env = createEnv({ KV: kv });
|
||||
const sessionId = await createAdminSession(kv, "1001", "alice");
|
||||
const cookie = await signIn(kv);
|
||||
|
||||
const event = bindEvent(env, adminCookie(sessionId), { installation_id: "555", group: "mine" });
|
||||
const event = bindEvent(env, cookie, { installation_id: "555", group: "mine" });
|
||||
await handleInstallBind(event);
|
||||
expect(responseStatus(event)).toBe(302);
|
||||
expect(responseHeader(event, "location")).toBe("/admin?install=ok");
|
||||
@@ -269,9 +301,9 @@ describe("POST /auth/github/install/bind", () => {
|
||||
]),
|
||||
);
|
||||
const env = createEnv({ KV: kv });
|
||||
const sessionId = await createAdminSession(kv, "1001", "alice");
|
||||
const cookie = await signIn(kv);
|
||||
|
||||
const event = bindEvent(env, adminCookie(sessionId), {
|
||||
const event = bindEvent(env, cookie, {
|
||||
installation_id: "555",
|
||||
group: "theirs",
|
||||
});
|
||||
@@ -292,9 +324,9 @@ describe("POST /auth/github/install/bind", () => {
|
||||
GITHUB_APP_ID: appId,
|
||||
GITHUB_PRIVATE_KEY: pem,
|
||||
});
|
||||
const sessionId = await createAdminSession(kv, "1001", "alice");
|
||||
const cookie = await signIn(kv);
|
||||
|
||||
const event = bindEvent(env, adminCookie(sessionId), { installation_id: "555", group: "" });
|
||||
const event = bindEvent(env, cookie, { installation_id: "555", group: "" });
|
||||
await handleInstallBind(event);
|
||||
expect(responseStatus(event)).toBe(302);
|
||||
expect(responseHeader(event, "location")).toBe("/admin?install=ok");
|
||||
@@ -304,15 +336,61 @@ describe("POST /auth/github/install/bind", () => {
|
||||
expect(group).toBeDefined();
|
||||
expect(group?.installationId).toBe(555);
|
||||
expect(group?.name).toBe("myorg");
|
||||
expect(group?.members).toContainEqual({ login: "alice", role: "owner" });
|
||||
expect(group?.members).toContainEqual({
|
||||
login: "alice",
|
||||
role: "owner",
|
||||
userId: expect.any(String),
|
||||
});
|
||||
});
|
||||
|
||||
it("refuses to bind an installation the user cannot manage", async () => {
|
||||
const kv = createMockKV();
|
||||
orgRole = "member";
|
||||
await kv.put(
|
||||
"config:groups",
|
||||
JSON.stringify([
|
||||
{ id: "mine", name: "Mine", adminIds: [], members: [{ login: "alice", role: "owner" }] },
|
||||
]),
|
||||
);
|
||||
const env = createEnv({ KV: kv, ALLOW_SELF_SIGNUP: "1" });
|
||||
const cookie = await signIn(kv);
|
||||
for (const group of ["mine", ""]) {
|
||||
const event = bindEvent(env, cookie, { installation_id: "555", group });
|
||||
await handleInstallBind(event);
|
||||
expect(responseHeader(event, "location")).toBe("/admin?error=install-forbidden");
|
||||
}
|
||||
const groups = await loadGroups(kv);
|
||||
expect(groups.map((g) => [g.id, g.installationId])).toEqual([["mine", undefined]]);
|
||||
});
|
||||
|
||||
it("never makes the installer an owner of a group someone already manages", async () => {
|
||||
const kv = createMockKV();
|
||||
await kv.put(
|
||||
"config:groups",
|
||||
JSON.stringify([
|
||||
{
|
||||
id: "inst-555",
|
||||
name: "Taken",
|
||||
adminIds: [],
|
||||
installationId: 555,
|
||||
members: [{ login: "bob", role: "owner" }],
|
||||
},
|
||||
]),
|
||||
);
|
||||
const env = createEnv({ KV: kv, ALLOW_SELF_SIGNUP: "1" });
|
||||
const cookie = await signIn(kv);
|
||||
const event = bindEvent(env, cookie, { installation_id: "555", group: "" });
|
||||
await handleInstallBind(event);
|
||||
const group = (await loadGroups(kv)).find((g) => g.id === "inst-555");
|
||||
expect(group?.members).toEqual([{ login: "bob", role: "owner" }]);
|
||||
});
|
||||
|
||||
it("auto-creates inst-{id} without joining members when self-signup is off", async () => {
|
||||
const kv = createMockKV();
|
||||
const env = createEnv({ KV: kv });
|
||||
const sessionId = await createAdminSession(kv, "1001", "alice");
|
||||
const cookie = await signIn(kv);
|
||||
|
||||
const event = bindEvent(env, adminCookie(sessionId), { installation_id: "555", group: "" });
|
||||
const event = bindEvent(env, cookie, { installation_id: "555", group: "" });
|
||||
await handleInstallBind(event);
|
||||
expect(responseStatus(event)).toBe(302);
|
||||
const groups = await loadGroups(kv);
|
||||
@@ -360,4 +438,42 @@ describe("oauth misc", () => {
|
||||
expect(responseStatus(event)).toBe(401);
|
||||
expect(result.error).toBe("Unauthorized");
|
||||
});
|
||||
|
||||
it("lets users delete only their own token", async () => {
|
||||
const kv = createMockKV();
|
||||
const env = createEnv({ KV: kv });
|
||||
const cookie = await signIn(kv);
|
||||
await saveToken(kv, "2002", "gho_bob", 3600);
|
||||
const other = makeEvent("/auth/token/2002", { method: "DELETE", headers: { cookie }, env });
|
||||
await handleTokenDelete(other, "2002");
|
||||
expect(responseStatus(other)).toBe(403);
|
||||
expect(await kv.get("token:2002")).not.toBeNull();
|
||||
const own = makeEvent("/auth/token/1001", { method: "DELETE", headers: { cookie }, env });
|
||||
await handleTokenDelete(own, "1001");
|
||||
expect(responseStatus(own)).toBe(200);
|
||||
expect(await kv.get("token:1001")).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects a browser callback whose state was started in another browser", async () => {
|
||||
const kv = createMockKV();
|
||||
const env = createEnv({ KV: kv });
|
||||
await kv.put(
|
||||
"state:abc",
|
||||
JSON.stringify({ redirectTo: "/admin", expiresAt: Date.now() + 60_000 }),
|
||||
);
|
||||
const event = makeEvent("/auth/github/callback?code=c&state=abc", {
|
||||
headers: { accept: "text/html" },
|
||||
env,
|
||||
});
|
||||
const result = (await handleOAuthCallback(event)) as { error?: string };
|
||||
expect(responseStatus(event)).toBe(400);
|
||||
expect(result.error).toBe("Invalid or expired state");
|
||||
});
|
||||
|
||||
it("sets the state cookie when starting the flow", async () => {
|
||||
const env = createEnv({ GITHUB_CLIENT_ID: "client-1" });
|
||||
const event = makeEvent("/auth/github?redirect=/admin", { env });
|
||||
await handleOAuthStart(event);
|
||||
expect(String(responseHeader(event, "set-cookie"))).toContain("wh_oauth_state=");
|
||||
});
|
||||
});
|
||||
+161
-8
@@ -4,8 +4,11 @@ import {
|
||||
getInvite,
|
||||
listInvites,
|
||||
revokeInvite,
|
||||
consumeInvite,
|
||||
acceptInvite,
|
||||
INVITE_LIFETIME_MS,
|
||||
} from "../server/lib/web/invites";
|
||||
import { createSqliteD1, createMemoryKV } from "./d1-sqlite";
|
||||
import { saveGroups, loadGroups, invalidateGroupsCache } from "../server/lib/web/groups";
|
||||
import type { Group } from "../server/lib/types";
|
||||
|
||||
@@ -46,9 +49,11 @@ const group: Group = {
|
||||
|
||||
describe("invites", () => {
|
||||
let kv: KVNamespace;
|
||||
let env: { KV: KVNamespace; DB: D1Database };
|
||||
|
||||
beforeEach(() => {
|
||||
kv = createMockKV();
|
||||
env = { KV: kv, DB: {} as D1Database };
|
||||
invalidateGroupsCache();
|
||||
});
|
||||
|
||||
@@ -81,6 +86,51 @@ describe("invites", () => {
|
||||
expect(invites[0]!.groupId).toBe("team");
|
||||
});
|
||||
|
||||
it("hides invites an earlier group under the same id left behind, without revoking them", async () => {
|
||||
const old = await createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "admin",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "former",
|
||||
groupInstance: "former-instance",
|
||||
});
|
||||
const legacy = await createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "viewer",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "former",
|
||||
});
|
||||
const mine = await createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "viewer",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
groupInstance: "current",
|
||||
});
|
||||
// A group created after instances existed: neither older invite is its own.
|
||||
const listed = await listInvites(kv, "team", { instance: "current" });
|
||||
expect(listed.map((i) => i.token)).toEqual([mine]);
|
||||
// Their issuer may live on under another id: not this group's to revoke.
|
||||
expect(await getInvite(kv, old)).not.toBeNull();
|
||||
expect(await getInvite(kv, legacy)).not.toBeNull();
|
||||
expect((await listInvites(kv, "team", { instance: "current" })).map((i) => i.token)).toEqual([
|
||||
mine,
|
||||
]);
|
||||
});
|
||||
|
||||
it("still lists a renamed group's invites whose move to the new id failed", async () => {
|
||||
const token = await createInvite(kv, {
|
||||
groupId: "old",
|
||||
role: "viewer",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
groupInstance: "inst-1",
|
||||
});
|
||||
// Renamed to "new"; moving the invite failed, it still names "old".
|
||||
const listed = await listInvites(kv, "new", { instance: "inst-1" });
|
||||
expect(listed.map((i) => i.token)).toEqual([token]);
|
||||
});
|
||||
|
||||
it("revokes an invite", async () => {
|
||||
const token = await createInvite(kv, {
|
||||
groupId: "team",
|
||||
@@ -100,15 +150,44 @@ describe("invites", () => {
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
});
|
||||
const result = await acceptInvite(kv, token, "777", "newbie");
|
||||
expect(result).toEqual({ ok: true, groupId: "team", role: "admin" });
|
||||
const result = await acceptInvite(env, token, "777", "newbie");
|
||||
expect(result).toEqual({
|
||||
ok: true,
|
||||
groupId: "team",
|
||||
groupInstance: expect.any(String),
|
||||
role: "admin",
|
||||
});
|
||||
expect(await getInvite(kv, token)).toBeNull();
|
||||
const groups = await loadGroups(kv);
|
||||
expect(groups[0]!.members).toContainEqual({ login: "newbie", role: "admin" });
|
||||
expect(groups[0]!.members).toContainEqual({ login: "newbie", role: "admin", userId: "777" });
|
||||
// adminIds stays in sync with owners only.
|
||||
expect(groups[0]!.adminIds).toEqual(["boss"]);
|
||||
});
|
||||
|
||||
it("refuses (and keeps the invite) when the login is on a member bound to another account", async () => {
|
||||
await saveGroups(kv, [
|
||||
{
|
||||
...group,
|
||||
members: [
|
||||
{ login: "boss", role: "owner" },
|
||||
// A member who renamed since; "newbie" now belongs to someone else.
|
||||
{ login: "newbie", role: "viewer", userId: "42" },
|
||||
],
|
||||
},
|
||||
]);
|
||||
const token = await createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "admin",
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
});
|
||||
expect(await acceptInvite(env, token, "777", "newbie")).toEqual({
|
||||
ok: false,
|
||||
reason: "conflict",
|
||||
});
|
||||
expect(await getInvite(kv, token)).not.toBeNull();
|
||||
});
|
||||
|
||||
it("upgrades an existing viewer to admin", async () => {
|
||||
await saveGroups(kv, [
|
||||
{
|
||||
@@ -125,9 +204,49 @@ describe("invites", () => {
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
});
|
||||
await acceptInvite(kv, token, "777", "newbie");
|
||||
await acceptInvite(env, token, "777", "newbie");
|
||||
const groups = await loadGroups(kv);
|
||||
expect(groups[0]!.members).toContainEqual({ login: "newbie", role: "admin" });
|
||||
expect(groups[0]!.members).toContainEqual({ login: "newbie", role: "admin", userId: "777" });
|
||||
});
|
||||
|
||||
it("lets only one of two concurrent accepts use the invite", async () => {
|
||||
const { db } = createSqliteD1();
|
||||
const shared = { KV: kv, DB: db };
|
||||
await saveGroups(kv, [group]);
|
||||
const token = await createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "admin",
|
||||
expiresAt: Date.now() + INVITE_LIFETIME_MS,
|
||||
createdBy: "boss",
|
||||
});
|
||||
const results = await Promise.all([
|
||||
acceptInvite(shared, token, "1", "first"),
|
||||
acceptInvite(shared, token, "2", "second"),
|
||||
]);
|
||||
expect(results.filter((r) => r.ok)).toHaveLength(1);
|
||||
const members = (await loadGroups(kv))[0]!.members!.map((m) => m.login);
|
||||
expect(members.filter((m) => m === "first" || m === "second")).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("keeps an invite's KV key for its whole seven-day lifetime", async () => {
|
||||
const ttls = new Map<string, number>();
|
||||
const put = kv.put.bind(kv);
|
||||
(kv as { put: unknown }).put = async (
|
||||
key: string,
|
||||
value: string,
|
||||
opts?: { expirationTtl?: number },
|
||||
): Promise<void> => {
|
||||
ttls.set(key, opts?.expirationTtl ?? 0);
|
||||
await put(key, value, opts);
|
||||
};
|
||||
const token = await createInvite(kv, {
|
||||
groupId: "team",
|
||||
role: "viewer",
|
||||
expiresAt: Date.now() + INVITE_LIFETIME_MS,
|
||||
createdBy: "boss",
|
||||
});
|
||||
expect(ttls.get(`invite:${token}`)).toBeGreaterThan(6 * 86400);
|
||||
expect(ttls.get("invite:group:team")).toBeGreaterThan(6 * 86400);
|
||||
});
|
||||
|
||||
it("rejects expired or unknown invites", async () => {
|
||||
@@ -138,8 +257,8 @@ describe("invites", () => {
|
||||
expiresAt: Date.now() - 1000,
|
||||
createdBy: "boss",
|
||||
});
|
||||
expect(await acceptInvite(kv, token, "1", "x")).toEqual({ ok: false, reason: "invalid" });
|
||||
expect(await acceptInvite(kv, "deadbeef", "1", "x")).toEqual({ ok: false, reason: "invalid" });
|
||||
expect(await acceptInvite(env, token, "1", "x")).toEqual({ ok: false, reason: "invalid" });
|
||||
expect(await acceptInvite(env, "deadbeef", "1", "x")).toEqual({ ok: false, reason: "invalid" });
|
||||
});
|
||||
|
||||
it("rejects invites for missing groups", async () => {
|
||||
@@ -150,6 +269,40 @@ describe("invites", () => {
|
||||
expiresAt: Date.now() + 86400_000,
|
||||
createdBy: "boss",
|
||||
});
|
||||
expect(await acceptInvite(kv, token, "1", "x")).toEqual({ ok: false, reason: "group-missing" });
|
||||
expect(await acceptInvite(env, token, "1", "x")).toEqual({
|
||||
ok: false,
|
||||
reason: "group-missing",
|
||||
});
|
||||
// The failed attempt gave the invite back.
|
||||
await saveGroups(kv, [{ ...group, id: "ghost" }]);
|
||||
expect(await acceptInvite(env, token, "1", "x")).toMatchObject({ ok: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe("invite listing quota", () => {
|
||||
it("writes nothing to KV when listing after an invite was used", async () => {
|
||||
const { kv } = createMemoryKV();
|
||||
const group = { instance: "i1" };
|
||||
const token = await createInvite(kv, {
|
||||
groupId: "g",
|
||||
role: "viewer",
|
||||
expiresAt: Date.now() + INVITE_LIFETIME_MS,
|
||||
createdBy: "1",
|
||||
groupInstance: "i1",
|
||||
});
|
||||
await consumeInvite(kv, token);
|
||||
let writes = 0;
|
||||
const put = kv.put.bind(kv);
|
||||
const del = kv.delete.bind(kv);
|
||||
kv.put = ((...args: Parameters<KVNamespace["put"]>) => {
|
||||
writes++;
|
||||
return put(...args);
|
||||
}) as KVNamespace["put"];
|
||||
kv.delete = ((key: string) => {
|
||||
writes++;
|
||||
return del(key);
|
||||
}) as KVNamespace["delete"];
|
||||
for (let i = 0; i < 3; i++) expect(await listInvites(kv, "g", group)).toEqual([]);
|
||||
expect(writes).toBe(0);
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
@@ -21,8 +21,11 @@ describe("kvMessageTracker", () => {
|
||||
it("stores and retrieves a message id by event and target", async () => {
|
||||
const kv = createMockKV();
|
||||
const tracker = kvMessageTracker(kv);
|
||||
await tracker.set("route-1:workflow-9", "channel-1", "msg-42");
|
||||
expect(await tracker.get("route-1:workflow-9", "channel-1")).toBe("msg-42");
|
||||
await tracker.set("route-1:workflow-9", "channel-1", { messageId: "msg-42", version: 5 });
|
||||
expect(await tracker.get("route-1:workflow-9", "channel-1")).toEqual({
|
||||
messageId: "msg-42",
|
||||
version: 5,
|
||||
});
|
||||
});
|
||||
|
||||
it("returns null for an unknown event/target", async () => {
|
||||
@@ -31,7 +34,7 @@ describe("kvMessageTracker", () => {
|
||||
expect(await tracker.get("missing", "channel-1")).toBeNull();
|
||||
});
|
||||
|
||||
it("uses the msg:{eventId}:{targetId} key with a 24h TTL", async () => {
|
||||
it("uses the msg:{eventId}:{targetId} key with a 30-day TTL", async () => {
|
||||
const kv = createMockKV();
|
||||
const keys = new Map<string, string>();
|
||||
const ttls = new Map<string, number>();
|
||||
@@ -44,15 +47,15 @@ describe("kvMessageTracker", () => {
|
||||
ttls.set(key, opts?.expirationTtl ?? 0);
|
||||
};
|
||||
const tracker = kvMessageTracker(kv);
|
||||
await tracker.set("event-1", "target-1", "msg-7");
|
||||
await tracker.set("event-1", "target-1", { messageId: "msg-7", version: 0 });
|
||||
expect(keys.has("msg:event-1:target-1")).toBe(true);
|
||||
expect(ttls.get("msg:event-1:target-1")).toBe(86400);
|
||||
expect(ttls.get("msg:event-1:target-1")).toBe(30 * 86400);
|
||||
});
|
||||
|
||||
it("deletes tracked entries", async () => {
|
||||
const kv = createMockKV();
|
||||
const tracker = kvMessageTracker(kv);
|
||||
await tracker.set("event-1", "target-1", "msg-7");
|
||||
await tracker.set("event-1", "target-1", { messageId: "msg-7", version: 0 });
|
||||
await tracker.delete("event-1", "target-1");
|
||||
expect(await tracker.get("event-1", "target-1")).toBeNull();
|
||||
});
|
||||
|
||||
@@ -34,6 +34,29 @@ describe("platform contract", () => {
|
||||
}
|
||||
expect(embed!.footer).toBeDefined();
|
||||
expect(embed!.footer!.text.length).toBeLessThanOrEqual(MAX_FOOTER);
|
||||
// Discord also caps the embed's combined text at 6000 characters.
|
||||
const total =
|
||||
embed!.title!.length +
|
||||
embed!.description!.length +
|
||||
embed!.footer!.text.length +
|
||||
embed!.fields!.reduce((n, f) => n + f.name.length + f.value.length, 0);
|
||||
expect(total).toBeLessThanOrEqual(6000);
|
||||
});
|
||||
|
||||
test("discord render never lets content mention anyone but the route's roles", () => {
|
||||
const out = renderDiscord({ title: "t", mentionRoleIds: ["123456789012345678"] });
|
||||
expect(out.allowed_mentions).toEqual({ parse: [], roles: ["123456789012345678"] });
|
||||
});
|
||||
|
||||
test("discord render drops buttons whose id exceeds Discord's limit", () => {
|
||||
const out = renderDiscord({
|
||||
title: "t",
|
||||
actions: [
|
||||
{ id: `ghpr|merge|o|${"r".repeat(120)}|1`, label: "Merge", style: "primary" },
|
||||
{ id: "ghpr|close|o|r|1", label: "Close", style: "danger" },
|
||||
],
|
||||
});
|
||||
expect(out.components![0]!.components.map((c) => c.custom_id)).toEqual(["ghpr|close|o|r|1"]);
|
||||
});
|
||||
|
||||
test("telegram render stays within the 4096-char cap", () => {
|
||||
|
||||
@@ -4,12 +4,16 @@ import { invalidateConfigCache } from "../server/lib/config";
|
||||
import { invalidateGroupsCache } from "../server/lib/web/groups";
|
||||
import type { Env } from "../server/lib/types";
|
||||
import type { DeliveryMessage, DispatchSummary } from "../server/lib/queue/delivery";
|
||||
import { createMemoryKV, createSqliteD1 } from "./d1-sqlite";
|
||||
import type { DispatchOptions } from "../server/lib/core/dispatch";
|
||||
|
||||
let summary: DispatchSummary;
|
||||
let dispatchCalls: number;
|
||||
let lastArgs: unknown[];
|
||||
|
||||
async function fakeDispatch(..._args: unknown[]): Promise<DispatchSummary> {
|
||||
async function fakeDispatch(...args: unknown[]): Promise<DispatchSummary> {
|
||||
dispatchCalls += 1;
|
||||
lastArgs = args;
|
||||
return summary;
|
||||
}
|
||||
|
||||
@@ -80,7 +84,7 @@ function body(overrides: Partial<DeliveryMessage> = {}): DeliveryMessage {
|
||||
};
|
||||
}
|
||||
|
||||
const STATE_KEY = "delivery-state:github:global:d1";
|
||||
const STATE_KEY = "delivery-state:github:@global:d1";
|
||||
|
||||
describe("handleQueueBatch", () => {
|
||||
beforeEach(() => {
|
||||
@@ -136,4 +140,443 @@ describe("handleQueueBatch", () => {
|
||||
expect(msg.acked).toBe(true);
|
||||
expect(dispatchCalls).toBe(0);
|
||||
});
|
||||
|
||||
it("never records a lost parked payload as delivered, nor closes the delivery", async () => {
|
||||
const { kv, store } = createMockKV();
|
||||
const msg = makeMessage(
|
||||
body({
|
||||
payload: undefined,
|
||||
payloadRef: "queue:payload:gone",
|
||||
receivedAt: Date.now() - 3600_000,
|
||||
}),
|
||||
);
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), createEnv(kv), fakeDispatch);
|
||||
expect(dispatchCalls).toBe(0);
|
||||
expect(msg.acked).toBe(true);
|
||||
// Left open: a redelivery carrying its own payload can still deliver it.
|
||||
expect(JSON.parse(store.get(STATE_KEY)!)).toMatchObject({ status: "retrying" });
|
||||
const redelivery = makeMessage(body());
|
||||
await handleQueueBatch(
|
||||
makeBatch("webhooker-delivery", [redelivery]),
|
||||
createEnv(kv),
|
||||
fakeDispatch,
|
||||
);
|
||||
expect(dispatchCalls).toBe(1);
|
||||
});
|
||||
|
||||
it("retries only the transient targets of a partially failed delivery", async () => {
|
||||
const { kv, store } = createMockKV();
|
||||
const env = createEnv(kv);
|
||||
summary = {
|
||||
attempts: 3,
|
||||
succeeded: ["g:r:discord:ok"],
|
||||
failures: [
|
||||
{ key: "g:r:discord:flaky", target: "flaky", errorCode: "DISCORD_5XX" },
|
||||
{ key: "g:r:discord:bad", target: "bad", errorCode: "DISCORD_ERROR" },
|
||||
],
|
||||
};
|
||||
const first = makeMessage(body(), 1);
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [first]), env, fakeDispatch);
|
||||
expect(first.retried).toEqual({ delaySeconds: 5 });
|
||||
// Every attempt consults the ledger (another message may have sent some).
|
||||
expect(typeof (lastArgs[4] as { skipTarget?: unknown }).skipTarget).toBe("function");
|
||||
|
||||
const second = makeMessage(body(), 2);
|
||||
summary = { attempts: 1, failures: [], succeeded: ["g:r:discord:flaky"] };
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [second]), env, fakeDispatch);
|
||||
const skip = (lastArgs[4] as { skipTarget: (k: string) => Promise<boolean> }).skipTarget;
|
||||
expect(await skip("g:r:discord:ok")).toBe(true);
|
||||
expect(await skip("g:r:discord:bad")).toBe(true);
|
||||
// Recorded too, now that the retry delivered it.
|
||||
expect(await skip("g:r:discord:flaky")).toBe(true);
|
||||
expect(second.acked).toBe(true);
|
||||
// The 403 target never got the message: the delivery ends as failed.
|
||||
expect(JSON.parse(store.get(STATE_KEY)!)).toMatchObject({ status: "failed" });
|
||||
});
|
||||
|
||||
it("follows a tenant group renamed while the delivery was queued", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:groups", JSON.stringify([{ id: "new-id", name: "G", adminIds: [] }]));
|
||||
store.set(
|
||||
"config:routes",
|
||||
JSON.stringify([
|
||||
{ id: "r1", name: "R", enabled: true, filters: [], targets: [], groupId: "new-id" },
|
||||
]),
|
||||
);
|
||||
store.set("group:renamed:old-id", "new-id");
|
||||
const msg = makeMessage(body({ groupId: "old-id" }));
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), createEnv(kv), fakeDispatch);
|
||||
const config = lastArgs[0] as { routes: Array<{ id: string }> };
|
||||
expect(config.routes.map((r) => r.id)).toEqual(["r1"]);
|
||||
expect((lastArgs[4] as { tenantId?: string }).tenantId).toBe("new-id");
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleQueueBatch edge cases", () => {
|
||||
beforeEach(() => {
|
||||
summary = { attempts: 0, failures: [] };
|
||||
dispatchCalls = 0;
|
||||
});
|
||||
|
||||
it("waits out a busy lease with a single long retry", async () => {
|
||||
const { kv, store } = createMockKV();
|
||||
store.set(STATE_KEY, JSON.stringify({ status: "processing", at: Date.now() }));
|
||||
// KV leases are best effort: emulate the D1 answer with a fake.
|
||||
const env = {
|
||||
...createEnv(kv),
|
||||
DB: {
|
||||
prepare: (sql: string): unknown => {
|
||||
const stmt = {
|
||||
run: async (): Promise<unknown> => ({ meta: { changes: 0 } }),
|
||||
// Delivery state answers "processing"; the configuration is empty.
|
||||
first: async (): Promise<unknown> =>
|
||||
sql.includes("delivery_state") ? { status: "processing" } : null,
|
||||
all: async (): Promise<unknown> => ({ results: [] }),
|
||||
};
|
||||
return { ...stmt, bind: (): unknown => stmt };
|
||||
},
|
||||
batch: async (stmts: unknown[]) => stmts.map(() => ({ results: [], meta: { changes: 0 } })),
|
||||
} as unknown as D1Database,
|
||||
};
|
||||
const msg = makeMessage(body());
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, fakeDispatch);
|
||||
expect(dispatchCalls).toBe(0);
|
||||
expect(msg.retried).toEqual({ delaySeconds: 310 });
|
||||
});
|
||||
|
||||
it("does not reopen a delivered delivery when payload cleanup fails", async () => {
|
||||
const { kv, store } = createMockKV();
|
||||
const failingDelete = {
|
||||
...kv,
|
||||
delete: async (): Promise<never> => Promise.reject(new Error("KV down")),
|
||||
};
|
||||
store.set("queue:payload:x", JSON.stringify({ a: 1 }));
|
||||
summary = { attempts: 1, failures: [] };
|
||||
const msg = makeMessage(
|
||||
body({ payload: undefined, payloadRef: "queue:payload:x", payloadRefType: "kv" }),
|
||||
);
|
||||
await handleQueueBatch(
|
||||
makeBatch("webhooker-delivery", [msg]),
|
||||
createEnv(failingDelete as unknown as KVNamespace),
|
||||
fakeDispatch,
|
||||
);
|
||||
expect(msg.acked).toBe(true);
|
||||
expect(msg.retried).toBeNull();
|
||||
expect(JSON.parse(store.get(STATE_KEY)!)).toMatchObject({ status: "delivered" });
|
||||
});
|
||||
|
||||
it("marks a delivery for a deleted tenant group as failed", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
store.set("config:groups", JSON.stringify([]));
|
||||
const msg = makeMessage(body({ groupId: "gone" }));
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), createEnv(kv), fakeDispatch);
|
||||
expect(dispatchCalls).toBe(0);
|
||||
expect(msg.acked).toBe(true);
|
||||
expect(JSON.parse(store.get("delivery-state:github:gone:d1")!)).toMatchObject({
|
||||
status: "failed",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("tenant resolution by group instance", () => {
|
||||
beforeEach(() => {
|
||||
summary = { attempts: 1, failures: [] };
|
||||
dispatchCalls = 0;
|
||||
});
|
||||
|
||||
it("follows the instance across renames and ignores a re-created id", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
// Identities as the server assigned them (after the one-time migration).
|
||||
store.set("config:identity-migrated", "1");
|
||||
store.set(
|
||||
"config:groups",
|
||||
JSON.stringify([
|
||||
{ id: "team", name: "T", adminIds: [], instance: "new-one" },
|
||||
{ id: "moved", name: "M", adminIds: [], instance: "orig" },
|
||||
]),
|
||||
);
|
||||
store.set(
|
||||
"config:routes",
|
||||
JSON.stringify([
|
||||
{ id: "r1", name: "R", enabled: true, filters: [], targets: [], groupId: "moved" },
|
||||
{ id: "r2", name: "R", enabled: true, filters: [], targets: [], groupId: "team" },
|
||||
]),
|
||||
);
|
||||
const msg = makeMessage(body({ groupId: "team", groupInstance: "orig" }));
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), createEnv(kv), fakeDispatch);
|
||||
const config = lastArgs[0] as { routes: Array<{ id: string }> };
|
||||
// Delivered to the renamed original group, not to the new "team".
|
||||
expect(config.routes.map((r) => r.id)).toEqual(["r1"]);
|
||||
});
|
||||
|
||||
it("shares one lease renewal between routes running in parallel", async () => {
|
||||
const { db } = createSqliteD1();
|
||||
const { kv } = createMemoryKV();
|
||||
const env: Env = { GITHUB_WEBHOOK_SECRET: "s", KV: kv, DB: db };
|
||||
let answers: boolean[] = [];
|
||||
const dispatch = async (...args: unknown[]): Promise<DispatchSummary> => {
|
||||
const opts = args[4] as DispatchOptions;
|
||||
const realNow = Date.now;
|
||||
// Both routes reach their next target once a renewal is due.
|
||||
Date.now = (): number => realNow() + 90_000;
|
||||
try {
|
||||
answers = await Promise.all([opts.beforeTarget!(), opts.beforeTarget!()]);
|
||||
} finally {
|
||||
Date.now = realNow;
|
||||
}
|
||||
return { attempts: 0, failures: [] };
|
||||
};
|
||||
const msg = makeMessage(body());
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, dispatch);
|
||||
// Neither route takes the shared renewal for a lost lease.
|
||||
expect(answers).toEqual([true, true]);
|
||||
expect(msg.acked).toBe(true);
|
||||
const row = await db
|
||||
.prepare("SELECT status FROM delivery_state WHERE key = ?")
|
||||
.bind("delivery-state:github:@global:d1")
|
||||
.first<{ status: string }>();
|
||||
expect(row?.status).toBe("delivered");
|
||||
});
|
||||
|
||||
it("never acks when its own renewal went through unseen", async () => {
|
||||
const { db: real } = createSqliteD1();
|
||||
// The renewal commits, but its answer is lost.
|
||||
const db = {
|
||||
...real,
|
||||
batch: real.batch.bind(real),
|
||||
prepare: (sql: string): D1PreparedStatement => {
|
||||
const stmt = real.prepare(sql);
|
||||
if (!sql.includes("UPDATE delivery_state SET updated_at = ?")) return stmt;
|
||||
return {
|
||||
...stmt,
|
||||
bind: (...args: unknown[]) => {
|
||||
const bound = stmt.bind(...args);
|
||||
return {
|
||||
...bound,
|
||||
run: async () => {
|
||||
await bound.run();
|
||||
throw new Error("network");
|
||||
},
|
||||
} as D1PreparedStatement;
|
||||
},
|
||||
} as D1PreparedStatement;
|
||||
},
|
||||
} as D1Database;
|
||||
const env: Env = { GITHUB_WEBHOOK_SECRET: "s", KV: createMemoryKV().kv, DB: db };
|
||||
const dispatch = async (...args: unknown[]): Promise<DispatchSummary> => {
|
||||
const opts = args[4] as DispatchOptions;
|
||||
const realNow = Date.now;
|
||||
Date.now = (): number => realNow() + 90_000;
|
||||
try {
|
||||
expect(await opts.beforeTarget!()).toBe(false);
|
||||
} finally {
|
||||
Date.now = realNow;
|
||||
}
|
||||
return {
|
||||
attempts: 1,
|
||||
failures: [{ key: "k", target: "t", error: "Delivery lease lost; left to its new holder" }],
|
||||
};
|
||||
};
|
||||
const msg = makeMessage(body());
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, dispatch);
|
||||
// No new holder exists: the message comes back once the lease surely expired.
|
||||
expect(msg.acked).toBe(false);
|
||||
expect(msg.retried).toEqual({ delaySeconds: 310 });
|
||||
});
|
||||
|
||||
it("never hands a pre-instance message to a group renamed onto its id", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
// Identities as the server assigned them (after the one-time migration).
|
||||
store.set("config:identity-migrated", "1");
|
||||
// "team" is held by a pre-instance group that had another id back then.
|
||||
store.set(
|
||||
"config:groups",
|
||||
JSON.stringify([
|
||||
{
|
||||
id: "team",
|
||||
name: "T",
|
||||
adminIds: [],
|
||||
instance: "b",
|
||||
instanceBackfilled: true,
|
||||
legacyId: "other",
|
||||
},
|
||||
]),
|
||||
);
|
||||
store.set("config:routes", JSON.stringify([]));
|
||||
const msg = makeMessage(body({ groupId: "team" }));
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), createEnv(kv), fakeDispatch);
|
||||
expect(dispatchCalls).toBe(0);
|
||||
expect(msg.acked).toBe(true);
|
||||
});
|
||||
|
||||
it("never hands a pre-instance message to a group created since", async () => {
|
||||
const { kv, store } = createMemoryKV();
|
||||
// Identities as the server assigned them (after the one-time migration).
|
||||
store.set("config:identity-migrated", "1");
|
||||
// "team" now belongs to a group created after instances existed.
|
||||
store.set(
|
||||
"config:groups",
|
||||
JSON.stringify([{ id: "team", name: "T", adminIds: [], instance: "fresh" }]),
|
||||
);
|
||||
store.set(
|
||||
"config:routes",
|
||||
JSON.stringify([
|
||||
{ id: "r2", name: "R", enabled: true, filters: [], targets: [], groupId: "team" },
|
||||
]),
|
||||
);
|
||||
const msg = makeMessage(body({ groupId: "team" }));
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), createEnv(kv), fakeDispatch);
|
||||
expect(dispatchCalls).toBe(0);
|
||||
expect(msg.acked).toBe(true);
|
||||
expect(JSON.parse(store.get("delivery-state:github:team:d1")!)).toMatchObject({
|
||||
status: "failed",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("consumer time budget", () => {
|
||||
beforeEach(() => {
|
||||
invalidateConfigCache();
|
||||
invalidateGroupsCache();
|
||||
dispatchCalls = 0;
|
||||
});
|
||||
|
||||
it("gives a message it has no time left for back to the queue, uncopied", async () => {
|
||||
const { kv } = createMockKV();
|
||||
const sent: unknown[] = [];
|
||||
const env: Env = {
|
||||
...createEnv(kv),
|
||||
QUEUE: { send: async (b: unknown) => void sent.push(b) } as unknown as Queue,
|
||||
};
|
||||
const msg = makeMessage(body());
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, fakeDispatch, 0);
|
||||
expect(dispatchCalls).toBe(0);
|
||||
// Back to the queue as it is (one queue read), not copied (a write and a
|
||||
// delete more).
|
||||
expect(sent).toHaveLength(0);
|
||||
expect(msg.retried).not.toBeNull();
|
||||
});
|
||||
|
||||
it("never spends a waiting message's last retry: it is copied instead", async () => {
|
||||
const { kv } = createMockKV();
|
||||
const sent: unknown[] = [];
|
||||
const env: Env = {
|
||||
...createEnv(kv),
|
||||
QUEUE: { send: async (b: unknown) => void sent.push(b) } as unknown as Queue,
|
||||
};
|
||||
// Left for later on every earlier attempt (max_retries is 5).
|
||||
const msg = makeMessage(body(), 6);
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, fakeDispatch, 0);
|
||||
expect(dispatchCalls).toBe(0);
|
||||
expect(sent).toHaveLength(1);
|
||||
expect(msg.acked).toBe(true);
|
||||
expect(msg.retried).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps the retry count of a started delivery that has to wait", async () => {
|
||||
const { kv, store } = createMockKV();
|
||||
// Taken and failed before: its attempts were real failures.
|
||||
store.set(STATE_KEY, JSON.stringify({ status: "retrying", at: Date.now() }));
|
||||
const sent: unknown[] = [];
|
||||
const env: Env = {
|
||||
...createEnv(kv),
|
||||
QUEUE: { send: async (b: unknown) => void sent.push(b) } as unknown as Queue,
|
||||
};
|
||||
const msg = makeMessage(body(), 6);
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, fakeDispatch, 0);
|
||||
// Not copied (a copy would start its failure count over): retried.
|
||||
expect(sent).toHaveLength(0);
|
||||
expect(msg.retried).not.toBeNull();
|
||||
});
|
||||
|
||||
it("continues a delivery that ran out of time in a new message", async () => {
|
||||
const { kv } = createMockKV();
|
||||
const sent: unknown[] = [];
|
||||
const env: Env = {
|
||||
...createEnv(kv),
|
||||
QUEUE: { send: async (b: unknown) => void sent.push(b) } as unknown as Queue,
|
||||
};
|
||||
const slowDispatch = async (...args: unknown[]): Promise<DispatchSummary> => {
|
||||
const options = args[4] as DispatchOptions;
|
||||
await new Promise((r) => setTimeout(r, 60));
|
||||
expect(await options.beforeTarget!()).toBe(false);
|
||||
return {
|
||||
attempts: 2,
|
||||
succeeded: ["k0"],
|
||||
failures: [{ key: "k", target: "t", error: "paused" }],
|
||||
};
|
||||
};
|
||||
const msg = makeMessage(body());
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, slowDispatch, 30);
|
||||
expect(sent).toHaveLength(1);
|
||||
expect(msg.acked).toBe(true);
|
||||
expect(msg.retried).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("consumer time budget without progress", () => {
|
||||
it("retries (and counts) a delivery that ran out of time without finishing a target", async () => {
|
||||
invalidateConfigCache();
|
||||
invalidateGroupsCache();
|
||||
const { kv } = createMockKV();
|
||||
const sent: unknown[] = [];
|
||||
const env: Env = {
|
||||
...createEnv(kv),
|
||||
QUEUE: { send: async (b: unknown) => void sent.push(b) } as unknown as Queue,
|
||||
};
|
||||
const stuck = async (...args: unknown[]): Promise<DispatchSummary> => {
|
||||
const options = args[4] as DispatchOptions;
|
||||
await new Promise((r) => setTimeout(r, 60));
|
||||
await options.beforeTarget!();
|
||||
return { attempts: 1, failures: [{ key: "k", target: "t", error: "timeout" }] };
|
||||
};
|
||||
const msg = makeMessage(body());
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, stuck, 30);
|
||||
expect(sent).toHaveLength(0);
|
||||
expect(msg.retried).not.toBeNull();
|
||||
});
|
||||
|
||||
it("reads a pre-upgrade custom delivery id as sent, prefix-like text included", async () => {
|
||||
invalidateConfigCache();
|
||||
invalidateGroupsCache();
|
||||
const { db } = createSqliteD1();
|
||||
const { kv } = createMemoryKV();
|
||||
const env: Env = { GITHUB_WEBHOOK_SECRET: "s", KV: kv, DB: db };
|
||||
// The delivery "42" finished before the upgrade; "d:42" is another one.
|
||||
const { setDeliveryState } = await import("../server/lib/queue/delivery");
|
||||
await setDeliveryState(env, "delivery-state:custom:global:42", "delivered");
|
||||
const msg = makeMessage(body({ provider: "custom", deliveryId: "d:42" }));
|
||||
dispatchCalls = 0;
|
||||
summary = { attempts: 0, failures: [] };
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, fakeDispatch);
|
||||
expect(dispatchCalls).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("consumer send budget", () => {
|
||||
it("stops a batch after its send limit and continues the rest in a new message", async () => {
|
||||
invalidateConfigCache();
|
||||
invalidateGroupsCache();
|
||||
const { kv } = createMockKV();
|
||||
const sent: unknown[] = [];
|
||||
const env: Env = {
|
||||
...createEnv(kv),
|
||||
QUEUE: { send: async (b: unknown) => void sent.push(b) } as unknown as Queue,
|
||||
};
|
||||
let allowed = 0;
|
||||
const wide = async (...args: unknown[]): Promise<DispatchSummary> => {
|
||||
const options = args[4] as DispatchOptions;
|
||||
for (let i = 0; i < 40; i++) if (await options.beforeTarget!()) allowed++;
|
||||
return {
|
||||
attempts: 40,
|
||||
succeeded: Array.from({ length: allowed }, (_, i) => `k${i}`),
|
||||
failures: [{ key: "rest", target: "t", error: "paused" }],
|
||||
};
|
||||
};
|
||||
const msg = makeMessage(body());
|
||||
await handleQueueBatch(makeBatch("webhooker-delivery", [msg]), env, wide);
|
||||
expect(allowed).toBe(20);
|
||||
expect(sent).toHaveLength(1);
|
||||
expect(msg.acked).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -83,7 +83,7 @@ function reply(chatId: string, topicId?: number): Record<string, unknown> {
|
||||
}
|
||||
|
||||
describe("telegram-commands /gh login", () => {
|
||||
it("stores state with telegramUserId and replies with OAuth URL", async () => {
|
||||
it("only hands out the OAuth link in the private chat", async () => {
|
||||
let sentBody: Record<string, unknown> | undefined;
|
||||
mockFetch((_url, init) => {
|
||||
sentBody = JSON.parse(String(init!.body)) as Record<string, unknown>;
|
||||
@@ -94,10 +94,32 @@ describe("telegram-commands /gh login", () => {
|
||||
await handleTelegramUpdate(env, {
|
||||
message: { ...reply("-100123"), text: "/gh login" },
|
||||
});
|
||||
|
||||
expect(sentBody?.chat_id).toBe("-100123");
|
||||
expect(String(sentBody?.text)).not.toContain("github.com/login/oauth/authorize");
|
||||
|
||||
await handleTelegramUpdate(env, {
|
||||
message: {
|
||||
...reply("42"),
|
||||
chat: { id: 42, type: "private" },
|
||||
text: "/gh login",
|
||||
},
|
||||
});
|
||||
expect(sentBody?.chat_id).toBe("42");
|
||||
expect(String(sentBody?.text)).toContain("github.com/login/oauth/authorize");
|
||||
});
|
||||
|
||||
it("runs a redelivered update only once", async () => {
|
||||
let calls = 0;
|
||||
mockFetch(() => {
|
||||
calls++;
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), { status: 200 });
|
||||
});
|
||||
const env = createEnv();
|
||||
const update = { update_id: 99, message: { ...reply("-100123"), text: "/gh logout" } };
|
||||
await handleTelegramUpdate(env, update);
|
||||
await handleTelegramUpdate(env, update);
|
||||
expect(calls).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram-commands /gh logout", () => {
|
||||
@@ -207,3 +229,413 @@ describe("telegram-updates webhook", () => {
|
||||
expect(await res.text()).toBe("ok");
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram-commands addressed by bot name", () => {
|
||||
it("runs `/gh@ThisBot` and ignores a command addressed to another bot", async () => {
|
||||
const sent: string[] = [];
|
||||
mockFetch((url, init) => {
|
||||
if (url.endsWith("/getMe")) {
|
||||
return new Response(JSON.stringify({ ok: true, result: { username: "ReviewBot" } }), {
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
sent.push(String((JSON.parse(String(init!.body)) as Record<string, unknown>).text));
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), { status: 200 });
|
||||
});
|
||||
const env = createEnv();
|
||||
await handleTelegramUpdate(env, {
|
||||
message: { ...reply("-100123"), text: "/gh@reviewbot logout" },
|
||||
});
|
||||
expect(sent).toHaveLength(1);
|
||||
await handleTelegramUpdate(env, {
|
||||
message: { ...reply("-100123"), text: "/gh@OtherBot logout" },
|
||||
});
|
||||
expect(sent).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram-commands replies", () => {
|
||||
it("gives an update back when its reply could not be sent", async () => {
|
||||
let sends = 0;
|
||||
mockFetch(() => {
|
||||
sends++;
|
||||
return new Response(JSON.stringify({ ok: false, description: "Bad Gateway" }), {
|
||||
status: 502,
|
||||
});
|
||||
});
|
||||
const env = createEnv();
|
||||
const update = {
|
||||
update_id: 501,
|
||||
message: { ...reply("42"), chat: { id: 42, type: "private" }, text: "/gh login" },
|
||||
};
|
||||
await expect(handleTelegramUpdate(env, update)).rejects.toThrow();
|
||||
mockFetch(() => {
|
||||
sends++;
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), { status: 200 });
|
||||
});
|
||||
// Telegram's redelivery runs it again: the link is sent this time.
|
||||
await handleTelegramUpdate(env, update);
|
||||
expect(sends).toBeGreaterThan(1);
|
||||
});
|
||||
|
||||
it("keeps a comment's line breaks and indentation", async () => {
|
||||
const env = createEnv();
|
||||
const { saveTelegramLink } = await import("../server/lib/github/store");
|
||||
await saveTelegramLink(env.DB, "111", "111980217");
|
||||
await env.KV.put(
|
||||
"token:111980217",
|
||||
JSON.stringify({
|
||||
userId: "111980217",
|
||||
accessToken: "ghu_test",
|
||||
expiresAt: Date.now() + 3600_000,
|
||||
}),
|
||||
);
|
||||
const calls: Array<{ url: string; body: string }> = [];
|
||||
mockFetch((url, init) => {
|
||||
calls.push({ url, body: String(init!.body) });
|
||||
if (String(url).endsWith("/sendMessage")) {
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), {
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
return new Response(
|
||||
JSON.stringify({ html_url: "https://github.com/acme/widget/issues/7#issuecomment-9" }),
|
||||
{ status: 200 },
|
||||
);
|
||||
});
|
||||
await handleTelegramUpdate(env, {
|
||||
message: {
|
||||
...reply("-100123"),
|
||||
text: "/gh comment first line\n indented\n\n- item",
|
||||
reply_to_message: {
|
||||
...reply("-100123"),
|
||||
text: "acme/widget#7: Add feature",
|
||||
entities: [{ type: "text_link", url: "https://github.com/acme/widget/issues/7" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
const ghCall = calls.find((c) => c.url.includes("/repos/"));
|
||||
expect((JSON.parse(ghCall!.body) as { body: string }).body).toBe(
|
||||
"first line\n indented\n\n- item",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram-commands refusals and whitespace", () => {
|
||||
it("gives back a command GitHub never ran when its error reply fails", async () => {
|
||||
const env = createEnv();
|
||||
const { saveTelegramLink } = await import("../server/lib/github/store");
|
||||
// Linked, but no token: refused before any GitHub request.
|
||||
await saveTelegramLink(env.DB, "111", "111980217");
|
||||
let replies = 0;
|
||||
let github = 0;
|
||||
mockFetch((url) => {
|
||||
if (url.includes("api.github.com")) github++;
|
||||
replies++;
|
||||
return new Response(JSON.stringify({ ok: false, description: "Bad Gateway" }), {
|
||||
status: 502,
|
||||
});
|
||||
});
|
||||
const update = {
|
||||
update_id: 777,
|
||||
message: {
|
||||
...reply("-100123"),
|
||||
text: "/gh comment hello",
|
||||
reply_to_message: {
|
||||
...reply("-100123"),
|
||||
text: "acme/widget#7: Add feature",
|
||||
entities: [{ type: "text_link", url: "https://github.com/acme/widget/issues/7" }],
|
||||
},
|
||||
},
|
||||
};
|
||||
await expect(handleTelegramUpdate(env, update)).rejects.toThrow();
|
||||
// Not confirmed: the redelivery runs it (and replies) again.
|
||||
await expect(handleTelegramUpdate(env, update)).rejects.toThrow();
|
||||
expect(github).toBe(0);
|
||||
expect(replies).toBeGreaterThan(1);
|
||||
});
|
||||
|
||||
it("keeps an indented first line of a comment", async () => {
|
||||
const env = createEnv();
|
||||
const { saveTelegramLink } = await import("../server/lib/github/store");
|
||||
await saveTelegramLink(env.DB, "111", "111980217");
|
||||
await env.KV.put(
|
||||
"token:111980217",
|
||||
JSON.stringify({
|
||||
userId: "111980217",
|
||||
accessToken: "ghu_test",
|
||||
expiresAt: Date.now() + 3600_000,
|
||||
}),
|
||||
);
|
||||
const calls: Array<{ url: string; body: string }> = [];
|
||||
mockFetch((url, init) => {
|
||||
calls.push({ url, body: String(init!.body) });
|
||||
if (String(url).endsWith("/sendMessage")) {
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), {
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
return new Response(
|
||||
JSON.stringify({ html_url: "https://github.com/acme/widget/issues/7#issuecomment-9" }),
|
||||
{ status: 200 },
|
||||
);
|
||||
});
|
||||
await handleTelegramUpdate(env, {
|
||||
message: {
|
||||
...reply("-100123"),
|
||||
text: "/gh comment\n code()",
|
||||
reply_to_message: {
|
||||
...reply("-100123"),
|
||||
text: "acme/widget#7: Add feature",
|
||||
entities: [{ type: "text_link", url: "https://github.com/acme/widget/issues/7" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
const ghCall = calls.find((c) => c.url.includes("/repos/"));
|
||||
expect((JSON.parse(ghCall!.body) as { body: string }).body).toBe(" code()");
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram-commands done before the write", () => {
|
||||
it("records the command as done before GitHub is written to", async () => {
|
||||
const env = createEnv();
|
||||
const { saveTelegramLink } = await import("../server/lib/github/store");
|
||||
await saveTelegramLink(env.DB, "111", "111980217");
|
||||
await env.KV.put(
|
||||
"token:111980217",
|
||||
JSON.stringify({
|
||||
userId: "111980217",
|
||||
accessToken: "ghu_test",
|
||||
expiresAt: Date.now() + 3600_000,
|
||||
}),
|
||||
);
|
||||
let doneAtWrite: unknown = "unset";
|
||||
// Storage touches of the "done" record (each is a quota operation).
|
||||
let doneTouches = 0;
|
||||
const kvGet = env.KV.get.bind(env.KV);
|
||||
env.KV.get = (async (key: string, ...rest: unknown[]) => {
|
||||
if (key === "accepted:bot:telegram:tg-token:888") doneTouches++;
|
||||
return (kvGet as (...a: unknown[]) => Promise<unknown>)(key, ...rest);
|
||||
}) as KVNamespace["get"];
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input instanceof Request ? input.url : input);
|
||||
const method = input instanceof Request ? input.method : (init?.method ?? "GET");
|
||||
if (url.includes("/comments") && method === "POST") {
|
||||
doneAtWrite = await kvGet("accepted:bot:telegram:tg-token:888");
|
||||
}
|
||||
if (url.endsWith("/sendMessage")) {
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), {
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
login: "a",
|
||||
html_url: "https://github.com/acme/widget/issues/7#issuecomment-9",
|
||||
}),
|
||||
{ status: 200 },
|
||||
);
|
||||
}) as typeof fetch;
|
||||
await handleTelegramUpdate(env, {
|
||||
update_id: 888,
|
||||
message: {
|
||||
...reply("-100123"),
|
||||
text: "/gh comment hello",
|
||||
reply_to_message: {
|
||||
...reply("-100123"),
|
||||
text: "acme/widget#7: Add feature",
|
||||
entities: [{ type: "text_link", url: "https://github.com/acme/widget/issues/7" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
// A lost confirmation afterwards can no longer let a redelivery write again.
|
||||
expect(doneAtWrite).not.toBeNull();
|
||||
expect(doneAtWrite).not.toBe("unset");
|
||||
// Written once, before the write — not confirmed a second time after it
|
||||
// (which would touch the record once more).
|
||||
expect(doneTouches).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram-commands outlived claims", () => {
|
||||
it("writes nothing once another run finished the same update", async () => {
|
||||
const env = createEnv();
|
||||
const { saveTelegramLink } = await import("../server/lib/github/store");
|
||||
await saveTelegramLink(env.DB, "111", "111980217");
|
||||
await env.KV.put(
|
||||
"token:111980217",
|
||||
JSON.stringify({
|
||||
userId: "111980217",
|
||||
accessToken: "ghu_test",
|
||||
expiresAt: Date.now() + 3600_000,
|
||||
}),
|
||||
);
|
||||
let posted = 0;
|
||||
// Meanwhile (this run outlived its claim, reading its token) another run
|
||||
// finished it.
|
||||
const kvGet = env.KV.get.bind(env.KV);
|
||||
env.KV.get = (async (key: string, ...rest: unknown[]) => {
|
||||
if (key === "token:111980217")
|
||||
await env.KV.put("accepted:bot:telegram:tg-token:999", "other-run");
|
||||
return (kvGet as (...a: unknown[]) => Promise<unknown>)(key, ...rest);
|
||||
}) as KVNamespace["get"];
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input instanceof Request ? input.url : input);
|
||||
const method = input instanceof Request ? input.method : (init?.method ?? "GET");
|
||||
if (url.includes("/comments") && method === "POST") posted++;
|
||||
if (url.endsWith("/sendMessage")) {
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), {
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
login: "a",
|
||||
html_url: "https://github.com/acme/widget/issues/7#issuecomment-9",
|
||||
}),
|
||||
{ status: 200 },
|
||||
);
|
||||
}) as typeof fetch;
|
||||
await handleTelegramUpdate(env, {
|
||||
update_id: 999,
|
||||
message: {
|
||||
...reply("-100123"),
|
||||
text: "/gh comment hello",
|
||||
reply_to_message: {
|
||||
...reply("-100123"),
|
||||
text: "acme/widget#7: Add feature",
|
||||
entities: [{ type: "text_link", url: "https://github.com/acme/widget/issues/7" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(posted).toBe(0);
|
||||
// The other run's "done" stays.
|
||||
expect(await env.KV.get("accepted:bot:telegram:tg-token:999")).toBe("other-run");
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram-commands foreign done markers", () => {
|
||||
it("never gives back a done another run wrote, even when its own read failed", async () => {
|
||||
const env = createEnv();
|
||||
const { saveTelegramLink } = await import("../server/lib/github/store");
|
||||
await saveTelegramLink(env.DB, "111", "111980217");
|
||||
await env.KV.put(
|
||||
"token:111980217",
|
||||
JSON.stringify({
|
||||
userId: "111980217",
|
||||
accessToken: "ghu_test",
|
||||
expiresAt: Date.now() + 3600_000,
|
||||
}),
|
||||
);
|
||||
// Another run finished meanwhile; this run's token read then fails
|
||||
// (before anything is written).
|
||||
const kvGet = env.KV.get.bind(env.KV);
|
||||
env.KV.get = (async (key: string, ...rest: unknown[]) => {
|
||||
if (key === "token:111980217") {
|
||||
await env.KV.put("accepted:bot:telegram:tg-token:1001", "other-run");
|
||||
throw new Error("KV down");
|
||||
}
|
||||
return (kvGet as (...a: unknown[]) => Promise<unknown>)(key, ...rest);
|
||||
}) as KVNamespace["get"];
|
||||
globalThis.fetch = (async () => {
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), {
|
||||
status: 200,
|
||||
});
|
||||
}) as typeof fetch;
|
||||
await expect(
|
||||
handleTelegramUpdate(env, {
|
||||
update_id: 1001,
|
||||
message: {
|
||||
...reply("-100123"),
|
||||
text: "/gh comment hello",
|
||||
reply_to_message: {
|
||||
...reply("-100123"),
|
||||
text: "acme/widget#7: Add feature",
|
||||
entities: [{ type: "text_link", url: "https://github.com/acme/widget/issues/7" }],
|
||||
},
|
||||
},
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
expect(await env.KV.get("accepted:bot:telegram:tg-token:1001")).toBe("other-run");
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram-commands logout and bot identity", () => {
|
||||
it("never unlinks twice when the logout reply failed", async () => {
|
||||
const env = createEnv();
|
||||
const { saveTelegramLink, getTelegramLink } = await import("../server/lib/github/store");
|
||||
await saveTelegramLink(env.DB, "111", "old");
|
||||
mockFetch(
|
||||
() => new Response(JSON.stringify({ ok: false, description: "down" }), { status: 502 }),
|
||||
);
|
||||
const update = { update_id: 4242, message: { ...reply("-100123"), text: "/gh logout" } };
|
||||
await expect(handleTelegramUpdate(env, update)).rejects.toThrow();
|
||||
// Linked again since; the redelivered logout must leave the new link alone.
|
||||
await saveTelegramLink(env.DB, "111", "new");
|
||||
mockFetch(
|
||||
() => new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), { status: 200 }),
|
||||
);
|
||||
await handleTelegramUpdate(env, update);
|
||||
expect(await getTelegramLink(env.DB, "111")).toBe("new");
|
||||
});
|
||||
|
||||
it("tells two bots' updates with the same number apart", async () => {
|
||||
let sends = 0;
|
||||
mockFetch(() => {
|
||||
sends++;
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), { status: 200 });
|
||||
});
|
||||
const update = { update_id: 77, message: { ...reply("-100123"), text: "/gh logout" } };
|
||||
const kv = createMockKV();
|
||||
const db = createMockDB();
|
||||
await handleTelegramUpdate({ ...createEnv(), KV: kv, DB: db, TELEGRAM_TOKEN: "1:a" }, update);
|
||||
await handleTelegramUpdate({ ...createEnv(), KV: kv, DB: db, TELEGRAM_TOKEN: "2:b" }, update);
|
||||
expect(sends).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("telegram-commands reply failure after the write", () => {
|
||||
it("keeps the command done when the reply fails after the comment was written", async () => {
|
||||
const env = createEnv();
|
||||
const { saveTelegramLink } = await import("../server/lib/github/store");
|
||||
await saveTelegramLink(env.DB, "111", "111980217");
|
||||
await env.KV.put(
|
||||
"token:111980217",
|
||||
JSON.stringify({
|
||||
userId: "111980217",
|
||||
accessToken: "ghu_test",
|
||||
expiresAt: Date.now() + 3600_000,
|
||||
}),
|
||||
);
|
||||
let comments = 0;
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input instanceof Request ? input.url : input);
|
||||
const method = input instanceof Request ? input.method : (init?.method ?? "GET");
|
||||
if (url.endsWith("/sendMessage")) return new Response("Bad Gateway", { status: 502 });
|
||||
if (url.includes("/comments") && method === "POST") comments++;
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
user: { login: "a" },
|
||||
html_url: "https://github.com/acme/widget/issues/7#issuecomment-9",
|
||||
}),
|
||||
{ status: 201 },
|
||||
);
|
||||
}) as typeof fetch;
|
||||
const update = {
|
||||
update_id: 2024,
|
||||
message: {
|
||||
...reply("-100123"),
|
||||
text: "/gh comment hello",
|
||||
reply_to_message: {
|
||||
...reply("-100123"),
|
||||
text: "acme/widget#7: Add feature",
|
||||
entities: [{ type: "text_link", url: "https://github.com/acme/widget/issues/7" }],
|
||||
},
|
||||
},
|
||||
};
|
||||
// The reply fails: Telegram redelivers the update.
|
||||
await handleTelegramUpdate(env, update).catch(() => undefined);
|
||||
await handleTelegramUpdate(env, update).catch(() => undefined);
|
||||
expect(comments).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { handleTelegramWebhookRequest } from "../server/lib/drivers/telegram/updates";
|
||||
import { telegramWebhookSecret } from "../server/lib/drivers/telegram/secret";
|
||||
import { createMemoryKV } from "./d1-sqlite";
|
||||
import type { Env } from "../server/lib/types";
|
||||
|
||||
function env(overrides: Partial<Env> = {}): Env {
|
||||
return { GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: {} as D1Database, ...overrides };
|
||||
}
|
||||
|
||||
function post(secret?: string): Request {
|
||||
return new Request("https://x/telegram/webhook", {
|
||||
method: "POST",
|
||||
headers: secret ? { "X-Telegram-Bot-Api-Secret-Token": secret } : {},
|
||||
body: JSON.stringify({ update_id: 1 }),
|
||||
});
|
||||
}
|
||||
|
||||
describe("telegram webhook authentication", () => {
|
||||
it("stays closed without a bot token", async () => {
|
||||
expect((await handleTelegramWebhookRequest(post("anything"), env())).status).toBe(403);
|
||||
});
|
||||
|
||||
it("derives a secret from the bot token when none is configured", async () => {
|
||||
const e = env({ TELEGRAM_TOKEN: "123:abc" });
|
||||
const secret = await telegramWebhookSecret(e);
|
||||
expect(secret).toMatch(/^[0-9a-f]{64}$/);
|
||||
expect((await handleTelegramWebhookRequest(post(), e)).status).toBe(401);
|
||||
expect((await handleTelegramWebhookRequest(post("wrong"), e)).status).toBe(401);
|
||||
expect((await handleTelegramWebhookRequest(post(secret), e)).status).toBe(200);
|
||||
});
|
||||
|
||||
it("prefers the configured secret", async () => {
|
||||
const e = env({ TELEGRAM_TOKEN: "123:abc", TELEGRAM_WEBHOOK_SECRET: "explicit" });
|
||||
expect(await telegramWebhookSecret(e)).toBe("explicit");
|
||||
expect((await handleTelegramWebhookRequest(post("explicit"), e)).status).toBe(200);
|
||||
});
|
||||
});
|
||||
+12
-3
@@ -224,9 +224,11 @@ describe("TelegramDriver", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("sends a small avatar photo (s=64) when the author has an icon", async () => {
|
||||
it("shows a small avatar (s=64) as the link preview of a text message", async () => {
|
||||
let capturedInit: RequestInit | undefined;
|
||||
let capturedUrl = "";
|
||||
mockFetch((url, init) => {
|
||||
capturedUrl = url;
|
||||
capturedInit = init;
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 9 } }), { status: 200 });
|
||||
});
|
||||
@@ -242,8 +244,15 @@ describe("TelegramDriver", () => {
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
const body = JSON.parse(String(capturedInit!.body)) as Record<string, unknown>;
|
||||
expect(body.photo).toBe("https://avatars.githubusercontent.com/u/1?v=4&s=64");
|
||||
// A text message (never a captioned photo), so later edits keep the full
|
||||
// 4096-character budget.
|
||||
expect(capturedUrl).toContain("/sendMessage");
|
||||
const body = JSON.parse(String(capturedInit!.body)) as {
|
||||
link_preview_options?: { url?: string };
|
||||
};
|
||||
expect(body.link_preview_options?.url).toBe(
|
||||
"https://avatars.githubusercontent.com/u/1?v=4&s=64",
|
||||
);
|
||||
});
|
||||
|
||||
it("sends a plain message when the author has no icon", async () => {
|
||||
|
||||
Reference in new issue
Block a user