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, writeFileSync, rmSync } from 'node:fs';
6import { join } from 'node:path';
7import { tmpdir } from 'node:os';
8
9describe('trust-gate', () => {
10 let tmp;
11
12 beforeEach(() => {
13 tmp = join(tmpdir(), '.test-trust-gate-' + Math.random().toString(36).slice(2));
14 mkdirSync(join(tmp, '.vit'), { recursive: true });
15 });
16
17 afterEach(() => {
18 rmSync(tmp, { recursive: true, force: true });
19 });
20
21 // Fresh import each test to avoid cached module state
22 async function loadModule() {
23 // Dynamic import with cache-busting
24 const mod = await import('../src/lib/trust-gate.js');
25 return mod;
26 }
27
28 describe('checkDangerousAccept', () => {
29 test('returns accepted false when no file exists', async () => {
30 const { checkDangerousAccept } = await loadModule();
31 expect(checkDangerousAccept(tmp)).toEqual({ accepted: false });
32 });
33
34 test('returns accepted true when file exists with valid JSON', async () => {
35 const { checkDangerousAccept } = await loadModule();
36 writeFileSync(join(tmp, '.vit', 'dangerous-accept'), JSON.stringify({ acceptedAt: '2026-03-26T14:30:00.000Z' }));
37 expect(checkDangerousAccept(tmp)).toEqual({ accepted: true });
38 });
39
40 test('returns accepted false when file is malformed JSON', async () => {
41 const { checkDangerousAccept } = await loadModule();
42 writeFileSync(join(tmp, '.vit', 'dangerous-accept'), 'not json');
43 expect(checkDangerousAccept(tmp)).toEqual({ accepted: false });
44 });
45
46 test('no TTL — old timestamps still accepted', async () => {
47 const { checkDangerousAccept } = await loadModule();
48 writeFileSync(join(tmp, '.vit', 'dangerous-accept'), JSON.stringify({ acceptedAt: '2020-01-01T00:00:00.000Z' }));
49 expect(checkDangerousAccept(tmp)).toEqual({ accepted: true });
50 });
51 });
52
53 describe('shouldBypassVet', () => {
54 test('returns bypass true with reason when flag active', async () => {
55 const { shouldBypassVet } = await loadModule();
56 writeFileSync(join(tmp, '.vit', 'dangerous-accept'), JSON.stringify({ acceptedAt: '2026-03-26T14:30:00.000Z' }));
57 expect(shouldBypassVet(tmp)).toEqual({ bypass: true, reason: 'dangerous-accept' });
58 });
59
60 test('returns bypass false when flag absent', async () => {
61 const { shouldBypassVet } = await loadModule();
62 expect(shouldBypassVet(tmp)).toEqual({ bypass: false });
63 });
64 });
65});