|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'bun:test' |
| 2 | +import { Hono } from 'hono' |
| 3 | +import { Database } from 'bun:sqlite' |
| 4 | +import { migrate } from '../db/migration-runner' |
| 5 | +import { allMigrations } from '../db/migrations' |
| 6 | +import { createSessionPinRoutes } from './session-pins' |
| 7 | + |
| 8 | +function createTestApp(db: Database): Hono { |
| 9 | + const app = new Hono() |
| 10 | + app.route('/session-pins', createSessionPinRoutes(db)) |
| 11 | + return app |
| 12 | +} |
| 13 | + |
| 14 | +function createTestDb(): Database { |
| 15 | + const db = new Database(':memory:') |
| 16 | + migrate(db, allMigrations) |
| 17 | + return db |
| 18 | +} |
| 19 | + |
| 20 | +describe('session pins routes', () => { |
| 21 | + let db: Database |
| 22 | + let app: Hono |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + db = createTestDb() |
| 26 | + app = createTestApp(db) |
| 27 | + }) |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + db.close() |
| 31 | + }) |
| 32 | + |
| 33 | + it('GET / returns empty pins list initially', async () => { |
| 34 | + const res = await app.request('/session-pins') |
| 35 | + expect(res.status).toBe(200) |
| 36 | + const data = await res.json() as { pins: unknown[] } |
| 37 | + expect(data.pins).toEqual([]) |
| 38 | + }) |
| 39 | + |
| 40 | + it('PUT / creates a pin and returns updated list', async () => { |
| 41 | + const res = await app.request('/session-pins', { |
| 42 | + method: 'PUT', |
| 43 | + headers: { 'Content-Type': 'application/json' }, |
| 44 | + body: JSON.stringify({ sessionId: 'ses_1', directory: '/w/a', pinned: true }), |
| 45 | + }) |
| 46 | + expect(res.status).toBe(200) |
| 47 | + const data = await res.json() as { pins: Array<{ sessionId: string; directory: string }> } |
| 48 | + expect(data.pins).toHaveLength(1) |
| 49 | + expect(data.pins[0]).toMatchObject({ sessionId: 'ses_1', directory: '/w/a' }) |
| 50 | + }) |
| 51 | + |
| 52 | + it('GET / returns pins after creation', async () => { |
| 53 | + await app.request('/session-pins', { |
| 54 | + method: 'PUT', |
| 55 | + headers: { 'Content-Type': 'application/json' }, |
| 56 | + body: JSON.stringify({ sessionId: 'ses_1', directory: '/w/a', pinned: true }), |
| 57 | + }) |
| 58 | + |
| 59 | + const res = await app.request('/session-pins') |
| 60 | + expect(res.status).toBe(200) |
| 61 | + const data = await res.json() as { pins: unknown[] } |
| 62 | + expect(data.pins).toHaveLength(1) |
| 63 | + }) |
| 64 | + |
| 65 | + it('PUT / with pinned: false removes the pin', async () => { |
| 66 | + await app.request('/session-pins', { |
| 67 | + method: 'PUT', |
| 68 | + headers: { 'Content-Type': 'application/json' }, |
| 69 | + body: JSON.stringify({ sessionId: 'ses_1', directory: '/w/a', pinned: true }), |
| 70 | + }) |
| 71 | + |
| 72 | + const res = await app.request('/session-pins', { |
| 73 | + method: 'PUT', |
| 74 | + headers: { 'Content-Type': 'application/json' }, |
| 75 | + body: JSON.stringify({ sessionId: 'ses_1', directory: '/w/a', pinned: false }), |
| 76 | + }) |
| 77 | + expect(res.status).toBe(200) |
| 78 | + const data = await res.json() as { pins: unknown[] } |
| 79 | + expect(data.pins).toHaveLength(0) |
| 80 | + }) |
| 81 | + |
| 82 | + it('PUT / with invalid body returns 400', async () => { |
| 83 | + const res = await app.request('/session-pins', { |
| 84 | + method: 'PUT', |
| 85 | + headers: { 'Content-Type': 'application/json' }, |
| 86 | + body: JSON.stringify({ sessionId: '', directory: '/w/a', pinned: true }), |
| 87 | + }) |
| 88 | + expect(res.status).toBe(400) |
| 89 | + const data = await res.json() as { error: string } |
| 90 | + expect(data.error).toBe('Invalid request') |
| 91 | + }) |
| 92 | +}) |
0 commit comments