A tool for parsing traffic on the jetstream and applying a moderation workstream based on regexp based rules
at main 4.6 kB view raw
1import { 2 chmodSync, 3 existsSync, 4 mkdirSync, 5 readFileSync, 6 rmSync, 7 unlinkSync, 8 writeFileSync, 9} from "node:fs"; 10import { join } from "node:path"; 11import { afterEach, beforeEach, describe, expect, it } from "vitest"; 12import type { SessionData } from "../session.js"; 13 14const TEST_DIR = join(process.cwd(), ".test-session"); 15const TEST_SESSION_PATH = join(TEST_DIR, ".session"); 16 17// Helper functions that mimic session.ts but use TEST_SESSION_PATH 18function testLoadSession(): SessionData | null { 19 try { 20 if (!existsSync(TEST_SESSION_PATH)) { 21 return null; 22 } 23 24 const data = readFileSync(TEST_SESSION_PATH, "utf-8"); 25 const session = JSON.parse(data) as SessionData; 26 27 if (!session.accessJwt || !session.refreshJwt || !session.did) { 28 return null; 29 } 30 31 return session; 32 } catch (error) { 33 return null; 34 } 35} 36 37function testSaveSession(session: SessionData): void { 38 try { 39 const data = JSON.stringify(session, null, 2); 40 writeFileSync(TEST_SESSION_PATH, data, "utf-8"); 41 chmodSync(TEST_SESSION_PATH, 0o600); 42 } catch (error) { 43 // Ignore errors for test 44 } 45} 46 47function testClearSession(): void { 48 try { 49 if (existsSync(TEST_SESSION_PATH)) { 50 unlinkSync(TEST_SESSION_PATH); 51 } 52 } catch (error) { 53 // Ignore errors for test 54 } 55} 56 57describe("session", () => { 58 beforeEach(() => { 59 // Create test directory 60 if (!existsSync(TEST_DIR)) { 61 mkdirSync(TEST_DIR, { recursive: true }); 62 } 63 }); 64 65 afterEach(() => { 66 // Clean up test directory 67 if (existsSync(TEST_DIR)) { 68 rmSync(TEST_DIR, { recursive: true, force: true }); 69 } 70 }); 71 72 describe("saveSession", () => { 73 it("should save session to file with proper permissions", () => { 74 const session: SessionData = { 75 accessJwt: "access-token", 76 refreshJwt: "refresh-token", 77 did: "did:plc:test123", 78 handle: "test.bsky.social", 79 active: true, 80 }; 81 82 testSaveSession(session); 83 84 expect(existsSync(TEST_SESSION_PATH)).toBe(true); 85 }); 86 87 it("should save all session fields correctly", () => { 88 const session: SessionData = { 89 accessJwt: "access-token", 90 refreshJwt: "refresh-token", 91 did: "did:plc:test123", 92 handle: "test.bsky.social", 93 email: "test@example.com", 94 emailConfirmed: true, 95 emailAuthFactor: false, 96 active: true, 97 status: "active", 98 }; 99 100 testSaveSession(session); 101 102 const loaded = testLoadSession(); 103 expect(loaded).toEqual(session); 104 }); 105 }); 106 107 describe("loadSession", () => { 108 it("should return null if session file does not exist", () => { 109 const session = testLoadSession(); 110 expect(session).toBeNull(); 111 }); 112 113 it("should load valid session from file", () => { 114 const session: SessionData = { 115 accessJwt: "access-token", 116 refreshJwt: "refresh-token", 117 did: "did:plc:test123", 118 handle: "test.bsky.social", 119 active: true, 120 }; 121 122 testSaveSession(session); 123 const loaded = testLoadSession(); 124 125 expect(loaded).toEqual(session); 126 }); 127 128 it("should return null for corrupted session file", () => { 129 writeFileSync(TEST_SESSION_PATH, "{ invalid json", "utf-8"); 130 131 const session = testLoadSession(); 132 expect(session).toBeNull(); 133 }); 134 135 it("should return null for session missing required fields", () => { 136 writeFileSync( 137 TEST_SESSION_PATH, 138 JSON.stringify({ accessJwt: "token" }), 139 "utf-8", 140 ); 141 142 const session = testLoadSession(); 143 expect(session).toBeNull(); 144 }); 145 146 it("should return null for session missing did", () => { 147 writeFileSync( 148 TEST_SESSION_PATH, 149 JSON.stringify({ 150 accessJwt: "access", 151 refreshJwt: "refresh", 152 handle: "test.bsky.social", 153 }), 154 "utf-8", 155 ); 156 157 const session = testLoadSession(); 158 expect(session).toBeNull(); 159 }); 160 }); 161 162 describe("clearSession", () => { 163 it("should remove session file if it exists", () => { 164 const session: SessionData = { 165 accessJwt: "access-token", 166 refreshJwt: "refresh-token", 167 did: "did:plc:test123", 168 handle: "test.bsky.social", 169 active: true, 170 }; 171 172 testSaveSession(session); 173 expect(existsSync(TEST_SESSION_PATH)).toBe(true); 174 175 testClearSession(); 176 expect(existsSync(TEST_SESSION_PATH)).toBe(false); 177 }); 178 179 it("should not throw if session file does not exist", () => { 180 expect(() => testClearSession()).not.toThrow(); 181 }); 182 }); 183});