summaryrefslogtreecommitdiffstats
path: root/tests/cache-manifest.test.ts
blob: d54cd8b7d704405a6831b06c4e5c44f40b4e5ea9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { test, expect } from 'bun:test';
import { findCacheByKey, findCacheByPrefix } from '../scripts/cache/manifest';
import type { CacheManifest } from '../scripts/cache/utils';

test('findCacheByKey returns null when cache not found', () => {
    const manifest: CacheManifest = {
        version: 1,
        caches: [
            { key: 'cache1.tzst', hash: 'abc123', timestamp: 1000, lastAccessed: 1000 },
            { key: 'cache2.tzst', hash: 'def456', timestamp: 2000, lastAccessed: 2000 }
        ]
    };

    expect(findCacheByKey(manifest, 'cache3.tzst')).toBeNull();
});

test('findCacheByKey returns matching cache entry', () => {
    const manifest: CacheManifest = {
        version: 1,
        caches: [
            { key: 'cache1.tzst', hash: 'abc123', timestamp: 1000, lastAccessed: 1000 },
            { key: 'cache2.tzst', hash: 'def456', timestamp: 2000, lastAccessed: 2000 }
        ]
    };

    const result = findCacheByKey(manifest, 'cache1.tzst');
    expect(result).not.toBeNull();
    expect(result?.key).toBe('cache1.tzst');
    expect(result?.hash).toBe('abc123');
});

test('findCacheByPrefix returns null when no caches match prefix', () => {
    const manifest: CacheManifest = {
        version: 1,
        caches: [
            { key: 'cache1.tzst', hash: 'abc123', timestamp: 1000, lastAccessed: 1000 },
            { key: 'cache2.tzst', hash: 'def456', timestamp: 2000, lastAccessed: 2000 }
        ]
    };

    expect(findCacheByPrefix(manifest, 'other-')).toBeNull();
});

test('findCacheByPrefix returns most recently created cache', () => {
    const manifest: CacheManifest = {
        version: 1,
        caches: [
            { key: 'extensions-abc123.tzst', hash: 'abc123', timestamp: 1000, lastAccessed: 1000 },
            { key: 'extensions-def456.tzst', hash: 'def456', timestamp: 3000, lastAccessed: 3000 },
            { key: 'extensions-ghi789.tzst', hash: 'ghi789', timestamp: 2000, lastAccessed: 2000 }
        ]
    };

    const result = findCacheByPrefix(manifest, 'extensions-');
    expect(result).not.toBeNull();
    expect(result?.key).toBe('extensions-def456.tzst');
    expect(result?.timestamp).toBe(3000);
});

test('findCacheByPrefix handles empty caches array', () => {
    const manifest: CacheManifest = {
        version: 1,
        caches: []
    };

    expect(findCacheByPrefix(manifest, 'extensions-')).toBeNull();
});

test('findCacheByKey handles empty caches array', () => {
    const manifest: CacheManifest = {
        version: 1,
        caches: []
    };

    expect(findCacheByKey(manifest, 'cache1.tzst')).toBeNull();
});