import { describe, expect, test } from 'bun:test'; import { pickNewestArtifact } from './backup-pull'; describe('pickNewestArtifact', () => { const keys = [ '2026-06-02/celilo-mgmt-2026-06-02T10-00-00-000Z.backup', '2026-06-04/celilo-mgmt-2026-06-04T21-05-21-637Z.backup', '2026-06-03/celilo-mgmt-2026-06-03T08-30-00-000Z.backup', '2026-06-04/system-2026-06-04T22-00-00-000Z.backup', '2026-06-01/celilo-2026-06-01T00-00-00-000Z.backup', ]; test('picks the chronologically newest module artifact', () => { expect(pickNewestArtifact(keys, 'celilo-mgmt')).toBe( '2026-06-04/celilo-mgmt-2026-06-04T21-05-21-637Z.backup', ); }); test('does not treat a shorter module id as a prefix of a longer one', () => { // 'celilo' must NOT match 'celilo-mgmt-...' artifacts — only the literal // celilo- artifact. expect(pickNewestArtifact(keys, 'celilo')).toBe( '2026-06-01/celilo-2026-06-01T00-00-00-000Z.backup', ); }); test('matches system-state artifacts via the system prefix', () => { expect(pickNewestArtifact(keys, 'system')).toBe( '2026-06-04/system-2026-06-04T22-00-00-000Z.backup', ); }); test('returns null when nothing matches', () => { expect(pickNewestArtifact(keys, 'homebridge')).toBeNull(); expect(pickNewestArtifact([], 'celilo-mgmt')).toBeNull(); }); test('ignores non-.backup objects and verify probes', () => { const noisy = [ 'celilo-mgmt/.celilo-verify-test', '2026-06-04/celilo-mgmt-2026-06-04T21-05-21-637Z.backup', '2026-06-04/celilo-mgmt-notes.txt', ]; expect(pickNewestArtifact(noisy, 'celilo-mgmt')).toBe( '2026-06-04/celilo-mgmt-2026-06-04T21-05-21-637Z.backup', ); }); });