This repository has no description
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 sol pbc
3
4import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
5import { mkdirSync, rmSync, readFileSync, existsSync } from 'node:fs';
6import { tmpdir } from 'node:os';
7import { join } from 'node:path';
8
9// vit-dir functions use process.cwd(), so we save/restore it
10const originalCwd = process.cwd();
11
12describe('vit-dir', () => {
13 let tmpDir;
14
15 beforeEach(() => {
16 tmpDir = join(tmpdir(), '.test-vit-dir-' + Math.random().toString(36).slice(2));
17 mkdirSync(tmpDir, { recursive: true });
18 process.chdir(tmpDir);
19 });
20
21 afterEach(() => {
22 process.chdir(originalCwd);
23 rmSync(tmpDir, { recursive: true, force: true });
24 });
25
26 test('writeProjectConfig creates .vit/ and config.json', async () => {
27 const { writeProjectConfig } = await import('../src/lib/vit-dir.js');
28 writeProjectConfig({ beacon: 'vit:github.com/org/repo' });
29 expect(existsSync(join(tmpDir, '.vit'))).toBe(true);
30 const content = readFileSync(join(tmpDir, '.vit', 'config.json'), 'utf-8');
31 const parsed = JSON.parse(content);
32 expect(parsed.beacon).toBe('vit:github.com/org/repo');
33 });
34
35 test('readProjectConfig reads written config', async () => {
36 const { writeProjectConfig, readProjectConfig } = await import('../src/lib/vit-dir.js');
37 writeProjectConfig({ beacon: 'vit:github.com/org/repo' });
38 const config = readProjectConfig();
39 expect(config.beacon).toBe('vit:github.com/org/repo');
40 });
41
42 test('readProjectConfig returns {} when file missing', async () => {
43 const { readProjectConfig } = await import('../src/lib/vit-dir.js');
44 const config = readProjectConfig();
45 expect(config).toEqual({});
46 });
47
48 test('appendLog creates file and appends JSONL line', async () => {
49 const { appendLog } = await import('../src/lib/vit-dir.js');
50 appendLog('caps.jsonl', { ts: '2026-01-01T00:00:00Z', did: 'did:plc:test' });
51 const content = readFileSync(join(tmpDir, '.vit', 'caps.jsonl'), 'utf-8');
52 const lines = content.trim().split('\n');
53 expect(lines.length).toBe(1);
54 expect(JSON.parse(lines[0]).did).toBe('did:plc:test');
55 });
56
57 test('appendLog appends to existing file', async () => {
58 const { appendLog } = await import('../src/lib/vit-dir.js');
59 appendLog('caps.jsonl', { ts: '2026-01-01T00:00:00Z', n: 1 });
60 appendLog('caps.jsonl', { ts: '2026-01-02T00:00:00Z', n: 2 });
61 const content = readFileSync(join(tmpDir, '.vit', 'caps.jsonl'), 'utf-8');
62 const lines = content.trim().split('\n');
63 expect(lines.length).toBe(2);
64 expect(JSON.parse(lines[0]).n).toBe(1);
65 expect(JSON.parse(lines[1]).n).toBe(2);
66 });
67});