import { DiagnosticsOptions, getSyncRulesStatus } from '@/api/diagnostics.js'; import { RouteAPI, SlotWalBudgetInfo } from '@/api/RouteAPI.js'; import { ParsedSyncConfigSet, storage } from '@/index.js'; import { parseSyncConfig } from '@powersync/service-sync-rules'; import { describe, expect, test } from 'vitest'; const GB = 1024 * 1024 * 1024; const MINIMAL_SYNC_RULES = ` bucket_definitions: global: data: - SELECT id FROM test_table `; function makeSyncRulesContent(overrides?: { slot_name?: string; version_label?: string; status?: storage.PersistedSyncConfigStatus; }): storage.PersistedSyncConfigContent { // We don't implement the entire interface correctly here - just enough to test the diagnostics logic. const status = overrides?.status ?? { id: '1', replicationStreamId: 1, state: storage.SyncRuleState.ACTIVE, last_checkpoint_lsn: 'some_lsn', last_fatal_error: null, last_fatal_error_ts: null, last_keepalive_ts: new Date(), last_checkpoint_ts: new Date() }; return { replicationStreamId: 1, syncConfigId: null, replicationStreamName: overrides?.slot_name ?? 'test_slot', sync_rules_content: MINIMAL_SYNC_RULES, compiled_plan: null, storageVersion: 1, syncConfigState: status.state, version_label: overrides?.version_label, parsed(options?: any) { const syncRules = parseSyncConfig(MINIMAL_SYNC_RULES, { ...options, defaultSchema: 'public' }); return { syncConfigs: [syncRules] } as ParsedSyncConfigSet; }, logger: null as any, asUpdateOptions: null as any, getStorageConfig: null as any, async getSyncConfigStatus() { return status; } } as storage.PersistedSyncConfigContent; } function makeSystemStorage() { return { async getStatus() { return { snapshotDone: true, resumeLsn: 'some_lsn' }; } } as storage.SyncRulesBucketStorage; } function makeRouteAPI(walBudget?: SlotWalBudgetInfo | undefined): RouteAPI { return { getParseSyncRulesOptions() { return { defaultSchema: 'public' }; }, async getSourceConfig() { return { tag: 'test', id: 'test', type: 'postgresql' }; }, async getConnectionStatus() { return { connected: true }; }, async getDebugTablesInfo() { return []; }, async getReplicationLagBytes() { return 0; }, ...(walBudget !== undefined ? { async getSlotWalBudget() { return walBudget; } } : {}) } as unknown as RouteAPI; } const OPTIONS: DiagnosticsOptions = { live_status: true, check_connection: true, include_content: false, active: true }; describe('getSyncRulesStatus WAL budget warnings', () => { test('warns when WAL budget is at 40%', async () => { const api = makeRouteAPI({ wal_status: 'extended', safe_wal_size: 4 * GB, max_slot_wal_keep_size: 10 * GB }); const result = await getSyncRulesStatus(api, makeSyncRulesContent(), OPTIONS, makeSystemStorage()); const walWarnings = result!.errors.filter((e) => e.message.includes('WAL budget')); expect(walWarnings).toHaveLength(1); expect(walWarnings[0].level).toBe('warning'); expect(walWarnings[0].message).toContain('40%'); }); test('no warning when WAL budget is at 80%', async () => { const api = makeRouteAPI({ wal_status: 'reserved', safe_wal_size: 8 * GB, max_slot_wal_keep_size: 10 * GB }); const result = await getSyncRulesStatus(api, makeSyncRulesContent(), OPTIONS, makeSystemStorage()); const walWarnings = result!.errors.filter((e) => e.message.includes('WAL budget')); expect(walWarnings).toHaveLength(0); }); test('clamps negative safe_wal_size to 0%', async () => { const api = makeRouteAPI({ wal_status: 'unreserved', safe_wal_size: -2.4 * GB, max_slot_wal_keep_size: 1 * 1024 * 1024 // 1MB }); const result = await getSyncRulesStatus(api, makeSyncRulesContent(), OPTIONS, makeSystemStorage()); const walWarnings = result!.errors.filter((e) => e.message.includes('WAL budget')); expect(walWarnings).toHaveLength(1); expect(walWarnings[0].message).toContain('0%'); expect(walWarnings[0].message).not.toMatch(/-\d+%/); }); test('no WAL budget error when slot status is lost', async () => { const api = makeRouteAPI({ wal_status: 'lost' }); const result = await getSyncRulesStatus(api, makeSyncRulesContent(), OPTIONS, makeSystemStorage()); const walErrors = result!.errors.filter( (e) => e.message.includes('WAL budget') || e.message.includes('PSYNC_S1146') ); expect(walErrors).toHaveLength(0); }); test('no WAL error when getSlotWalBudget is not defined', async () => { const api = makeRouteAPI(); const result = await getSyncRulesStatus(api, makeSyncRulesContent(), OPTIONS, makeSystemStorage()); const walErrors = result!.errors.filter( (e) => e.message.includes('WAL budget') || e.message.includes('PSYNC_S1146') ); expect(walErrors).toHaveLength(0); }); test('uses sync config status for status-derived diagnostics fields', async () => { const configStatus: storage.PersistedSyncConfigStatus = { id: 'config-a', replicationStreamId: 1, state: storage.SyncRuleState.ACTIVE, last_checkpoint_lsn: 'config_lsn', last_fatal_error: 'config failed', last_fatal_error_ts: new Date('2026-01-01T00:00:00.000Z'), last_keepalive_ts: new Date('2026-01-01T00:01:00.000Z'), last_checkpoint_ts: new Date('2026-01-01T00:02:00.000Z') }; const result = await getSyncRulesStatus( makeRouteAPI(), makeSyncRulesContent({ status: configStatus }), OPTIONS, makeSystemStorage() ); expect(result!.connections[0].last_checkpoint_ts).toBe('2026-01-01T00:02:00.000Z'); expect(result!.connections[0].last_keepalive_ts).toBe('2026-01-01T00:01:00.000Z'); expect(result!.errors.some((error) => error.message == 'config failed')).toBe(true); }); test('includes the sync config version label', async () => { const result = await getSyncRulesStatus( makeRouteAPI(), makeSyncRulesContent({ version_label: 'v6' }), OPTIONS, makeSystemStorage() ); expect(result!.version_label).toBe('v6'); }); test('includes the sync config version label when parsing fails', async () => { const content = makeSyncRulesContent({ version_label: 'v6' }); content.parsed = () => { throw new Error('Invalid sync config'); }; const result = await getSyncRulesStatus(makeRouteAPI(), content, OPTIONS, makeSystemStorage()); expect(result!.version_label).toBe('v6'); expect(result!.errors).toEqual([expect.objectContaining({ level: 'fatal', message: 'Invalid sync config' })]); }); test('reports each sync config error with its source location when parsing fails', async () => { const yaml = ` config: edition: 3 streams: global: query: SELECT id FROM test_table unknown_key: true `; const content = makeSyncRulesContent(); content.parsed = (options?: any) => { parseSyncConfig(yaml, { ...options, defaultSchema: 'public', throwOnError: true }); throw new Error('Expected parsing to fail'); }; const result = await getSyncRulesStatus(makeRouteAPI(), content, OPTIONS, makeSystemStorage()); expect(result!.errors).toHaveLength(1); const [error] = result!.errors; expect(error.level).toBe('fatal'); expect(error.message).not.toContain('Expected parsing to fail'); expect(yaml.slice(error.location!.start_offset, error.location!.end_offset)).toBe('unknown_key'); }); }); describe('source capability validation', () => { test('exposes stored replication errors when source checks cannot run', async () => { const content = makeSyncRulesContent({ status: { id: '1', replicationStreamId: 1, state: storage.SyncRuleState.PROCESSING, last_checkpoint_lsn: null, last_fatal_error: 'Source capability unavailable', last_fatal_error_ts: new Date(), last_keepalive_ts: null, last_checkpoint_ts: null } }); const result = await getSyncRulesStatus( makeRouteAPI(), content, { ...OPTIONS, check_connection: false, active: false, include_content: true }, makeSystemStorage() ); expect(result?.content).toBe(MINIMAL_SYNC_RULES); expect(result?.errors).toContainEqual( expect.objectContaining({ level: 'fatal', message: 'Source capability unavailable' }) ); }); test('reports capability failures alongside table diagnostics', async () => { const api = makeRouteAPI(); api.validateSourceCapabilities = async () => [ { level: 'fatal', message: 'Source capability unavailable' }, { level: 'warning', message: 'Source advisory', location: { start_offset: 1, end_offset: 4 } } ]; api.getDebugTablesInfo = async () => [ { schema: 'public', pattern: 'items', wildcard: false, table: { schema: 'public', name: 'items', data_queries: true, parameter_queries: false, replication_id: [], errors: [{ level: 'warning', message: 'Index missing' }] } } ]; const result = await getSyncRulesStatus(api, makeSyncRulesContent(), OPTIONS, makeSystemStorage()); expect(result!.errors).toEqual( expect.arrayContaining([ expect.objectContaining({ level: 'fatal', message: 'Source capability unavailable' }), expect.objectContaining({ level: 'warning', message: 'Index missing' }), expect.objectContaining({ level: 'warning', message: 'Source advisory', location: { start_offset: 1, end_offset: 4 } }) ]) ); expect(result!.connections[0].tables).toHaveLength(1); }); test('preserves both validation failures and continues independent diagnostics', async () => { const api = makeRouteAPI({ wal_status: 'extended', safe_wal_size: 4 * GB, max_slot_wal_keep_size: 10 * GB }); api.validateSourceCapabilities = async () => { throw new Error('Source capability unavailable'); }; api.getDebugTablesInfo = async () => { throw new Error('Table metadata unavailable'); }; const result = await getSyncRulesStatus(api, makeSyncRulesContent(), OPTIONS, makeSystemStorage()); expect(result!.errors).toEqual( expect.arrayContaining([ expect.objectContaining({ level: 'fatal', message: 'Source capability unavailable' }), expect.objectContaining({ level: 'fatal', message: 'Table metadata unavailable' }), expect.objectContaining({ level: 'warning', message: expect.stringContaining('WAL budget') }) ]) ); }); test('does not check capabilities when connection checks are disabled', async () => { const api = makeRouteAPI(); let called = false; api.validateSourceCapabilities = async () => { called = true; return []; }; await getSyncRulesStatus(api, makeSyncRulesContent(), { ...OPTIONS, check_connection: false }, makeSystemStorage()); expect(called).toBe(false); }); });