Repository navigation
Expand file tree
/
Copy pathsalt.test.ts
More file actions
53 lines (46 loc) · 1.93 KB
/
Copy pathsalt.test.ts
File metadata and controls
53 lines (46 loc) · 1.93 KB
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
import { DAY, HOUR } from '@pulse/core';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { openDatabase, type Db } from '../src/db/database.js';
import { SaltStore } from '../src/ingest/salt.js';
const DAY1 = Date.parse('2026-06-10T00:00:00Z');
const storedDays = (db: Db) =>
(db.prepare('SELECT day FROM salts ORDER BY day').all() as { day: number }[]).map(
(r) => r.day * DAY,
);
let dir: string | null = null;
afterEach(() => {
if (dir) rmSync(dir, { recursive: true, force: true });
dir = null;
});
describe('SaltStore', () => {
it("keeps today's and yesterday's salt and deletes older ones", () => {
const db = openDatabase(':memory:');
const salts = new SaltStore(db);
const first = salts.forTime(DAY1 + HOUR);
expect(first).toMatch(/^[0-9a-f]{32}$/);
expect(salts.forTime(DAY1 + 23 * HOUR)).toBe(first);
const second = salts.forTime(DAY1 + DAY + HOUR);
expect(second).not.toBe(first);
expect(storedDays(db)).toEqual([DAY1, DAY1 + DAY]);
expect(salts.forTime(DAY1 + 2 * HOUR)).toBe(first);
const third = salts.forTime(DAY1 + 2 * DAY + HOUR);
expect(new Set([first, second, third]).size).toBe(3);
expect(storedDays(db)).toEqual([DAY1 + DAY, DAY1 + 2 * DAY]);
});
it('survives a restart: the same day gets the same salt from the database', () => {
dir = mkdtempSync(join(tmpdir(), 'pulse-salt-'));
const path = join(dir, 'pulse.db');
const before = openDatabase(path);
const yesterday = new SaltStore(before).forTime(DAY1 + HOUR);
const today = new SaltStore(before).forTime(DAY1 + DAY + HOUR);
before.close();
const after = openDatabase(path);
const salts = new SaltStore(after);
expect(salts.forTime(DAY1 + DAY + 20 * HOUR)).toBe(today);
expect(salts.forTime(DAY1 + 5 * HOUR)).toBe(yesterday);
after.close();
});
});