File size: 2,203 Bytes
f500658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
	type ConformanceCase,
	createSessionRepoConformance,
} from "@earendil-works/pi-agent-core/harness/session/testing";
import { describe, it } from "vitest";
import { createNodeSqliteFactory, SqliteSessionRepo } from "../src/index.ts";

const NOW = 1_700_000_000_000;

function registerConformance(name: string, cases: readonly ConformanceCase[]): void {
	describe(name, () => {
		for (const group of new Set(cases.map((testCase) => testCase.group))) {
			describe(group, () => {
				for (const testCase of cases.filter((candidate) => candidate.group === group)) {
					it(testCase.name, () => testCase.run());
				}
			});
		}
	});
}

let currentDirectory: string | undefined;
let currentSharedDirectory: string | undefined;

async function createConformanceRepo() {
	currentDirectory = await mkdtemp(join(tmpdir(), "pi-sqlite-session-repo-conformance-"));
	return new SqliteSessionRepo({
		directory: currentDirectory,
		databaseFactory: createNodeSqliteFactory(),
		now: () => NOW,
	});
}

async function createSharedContainerConformanceRepo() {
	currentSharedDirectory = await mkdtemp(join(tmpdir(), "pi-sqlite-session-repo-shared-conformance-"));
	return new SqliteSessionRepo({
		directory: currentSharedDirectory,
		databasePath: join(currentSharedDirectory, "sessions.sqlite"),
		databaseFactory: createNodeSqliteFactory(),
		now: () => NOW,
	});
}

async function cleanupConformanceRepo() {
	if (currentDirectory === undefined) return;
	await rm(currentDirectory, { recursive: true, force: true });
	currentDirectory = undefined;
}

async function cleanupSharedContainerConformanceRepo() {
	if (currentSharedDirectory === undefined) return;
	await rm(currentSharedDirectory, { recursive: true, force: true });
	currentSharedDirectory = undefined;
}

registerConformance(
	"SqliteSessionRepo conformance",
	createSessionRepoConformance(createConformanceRepo, cleanupConformanceRepo),
);

registerConformance(
	"SqliteSessionRepo shared-container conformance",
	createSessionRepoConformance(createSharedContainerConformanceRepo, cleanupSharedContainerConformanceRepo),
);