⚡ Zero-dependency plcbundle library exclusively for Bun
1import { beforeAll, afterAll } from 'bun:test';
2import { mkdirSync, rmSync } from 'fs';
3
4// Test directories
5export const TEST_DIR = './test-data';
6export const BUNDLES_DIR = `${TEST_DIR}/bundles`;
7export const TEMP_DIR = `${TEST_DIR}/temp`;
8
9// Setup test environment
10beforeAll(() => {
11 mkdirSync(BUNDLES_DIR, { recursive: true });
12 mkdirSync(TEMP_DIR, { recursive: true });
13});
14
15// Cleanup after all tests
16afterAll(() => {
17 try {
18 rmSync(TEST_DIR, { recursive: true, force: true });
19 } catch (error) {
20 // Ignore cleanup errors
21 }
22});
23
24// Create a mock bundle index
25export function createMockIndex() {
26 return {
27 version: "1.0",
28 last_bundle: 3,
29 updated_at: new Date().toISOString(),
30 total_size_bytes: 5000000,
31 bundles: [
32 {
33 bundle_number: 1,
34 start_time: "2024-01-01T00:00:00.000Z",
35 end_time: "2024-01-01T01:00:00.000Z",
36 operation_count: 10000,
37 did_count: 9500,
38 hash: "abc123",
39 content_hash: "def456",
40 parent: "",
41 compressed_hash: "ghi789",
42 compressed_size: 1500000,
43 uncompressed_size: 5000000,
44 cursor: "",
45 created_at: "2024-01-01T02:00:00.000Z",
46 },
47 {
48 bundle_number: 2,
49 start_time: "2024-01-01T01:00:00.000Z",
50 end_time: "2024-01-01T02:00:00.000Z",
51 operation_count: 10000,
52 did_count: 9600,
53 hash: "jkl012",
54 content_hash: "mno345",
55 parent: "abc123",
56 compressed_hash: "pqr678",
57 compressed_size: 1600000,
58 uncompressed_size: 5100000,
59 cursor: "2024-01-01T01:00:00.000Z",
60 created_at: "2024-01-01T03:00:00.000Z",
61 },
62 {
63 bundle_number: 3,
64 start_time: "2024-01-01T02:00:00.000Z",
65 end_time: "2024-01-01T03:00:00.000Z",
66 operation_count: 10000,
67 did_count: 9700,
68 hash: "stu901",
69 content_hash: "vwx234",
70 parent: "jkl012",
71 compressed_hash: "yza567",
72 compressed_size: 1900000,
73 uncompressed_size: 5200000,
74 cursor: "2024-01-01T02:00:00.000Z",
75 created_at: "2024-01-01T04:00:00.000Z",
76 },
77 ],
78 };
79}
80
81// Create mock operations
82export function createMockOperations(count: number = 100) {
83 const operations = [];
84
85 for (let i = 0; i < count; i++) {
86 operations.push({
87 did: `did:plc:${Math.random().toString(36).substring(7)}`,
88 cid: `bafyrei${Math.random().toString(36).substring(7)}`,
89 createdAt: new Date(Date.now() - i * 1000).toISOString(),
90 operation: {
91 type: "plc_operation",
92 alsoKnownAs: i % 3 === 0 ? [`at://${i}.test`] : [],
93 services: i % 2 === 0 ? {
94 atproto_pds: {
95 type: "AtprotoPersonalDataServer",
96 endpoint: "https://bsky.social",
97 },
98 } : {},
99 },
100 });
101 }
102
103 return operations;
104}
105
106// Create a mock compressed bundle
107export async function createMockBundle(bundleNum: number, operations: any[]) {
108 const jsonl = operations.map(op => JSON.stringify(op)).join('\n') + '\n';
109 const compressed = Bun.deflateSync(new TextEncoder().encode(jsonl));
110 return compressed;
111}