fix(federation): explicit per-status handling in queueOutboxEvent

Replaces the silent UNIQUE-swallow placeholder branch. Each peer
status has an explicit branch:
  active/pending/unreachable: race-catch — re-fetch peer row and
    enqueue via matchedPeers. Previously skipped silently, losing
    real-time delivery under asymmetric failure.
  awaiting_approval/needs_attention/rejected/revoked: drop with
    logged reason. Mutation log still captures; sync-pull on
    activation replays.
  default: exhaustiveness check (no 'as never' cast) — TypeScript
    enforces that every status value is handled explicitly.
This commit is contained in:
Jannis Braun
2026-04-22 00:34:43 +02:00
parent ae035eba9b
commit 57d7ca66d3
2 changed files with 157 additions and 21 deletions
@@ -0,0 +1,92 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import Database from 'better-sqlite3';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as schema from '../db/schema.js';
import { eq } from 'drizzle-orm';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
type TestDb = ReturnType<typeof drizzle<typeof schema>>;
// Mutable reference updated in beforeEach — the factory closes over this.
let testDb: TestDb;
vi.mock('../db/index.js', () => ({
getDb: () => testDb,
schema,
}));
// Mock federation-auth helpers to avoid env-var dependency
vi.mock('../utils/federationAuth.js', () => ({
getOurOrigin: () => 'https://local.example',
buildFederationHeaders: () => ({}),
generateHmacSecret: () => 'test-secret',
}));
function applyMigrations(db: Database.Database): void {
const migrationsDir = path.resolve(__dirname, '../../drizzle');
const files = fs.readdirSync(migrationsDir).filter(f => f.endsWith('.sql')).sort();
for (const f of files) {
const sql = fs.readFileSync(path.join(migrationsDir, f), 'utf8');
const statements = sql.split(/-->\s*statement-breakpoint/);
for (const stmt of statements) {
const clean = stmt.trim();
if (clean) db.exec(clean);
}
}
}
function seedSettings(): void {
testDb.insert(schema.instanceSettings).values({
id: 1,
federationRelayEnabled: 1,
federationRelayTtlDays: 30,
updatedAt: Date.now(),
}).run();
}
function seedPeer(id: string, origin: string, status: string): void {
testDb.insert(schema.federationPeers).values({
id, origin, hmacSecret: 'secret',
status, lastSyncedAt: 0, createdAt: Date.now(),
}).run();
}
function countOutbox(peerId: string): number {
return testDb.select().from(schema.federationOutbox)
.where(eq(schema.federationOutbox.peerId, peerId))
.all().length;
}
// Import once at module level — vi.mock is hoisted and the factory returns the
// live testDb reference, so re-using the cached import is correct.
const { queueOutboxEvent } = await import('./federationOutbox.js');
describe('queueOutboxEvent — non-deliverable statuses', () => {
beforeEach(() => {
const sqlite = new Database(':memory:');
testDb = drizzle(sqlite, { schema });
applyMigrations(sqlite);
seedSettings();
vi.restoreAllMocks();
});
it.each([
['awaiting_approval'],
['needs_attention'],
['rejected'],
['revoked'],
])('drops the event and logs a reason for %s peers (no outbox row, no throw)', (status) => {
seedPeer('peer-drop', 'https://drop.example', status);
const debugSpy = vi.spyOn(console, 'debug').mockImplementation(() => {});
queueOutboxEvent('entity-1', 'ctx-1', 'create', '{}', ['https://drop.example'], 'dm');
expect(countOutbox('peer-drop')).toBe(0);
expect(debugSpy).toHaveBeenCalled();
expect(debugSpy.mock.calls[0]![0] as string).toContain(status);
});
});