import { describe, it, expect } from 'vitest' import { grantAllows, resolveSession, } from '../../skills/data-portal/template/functions/api/_lib/session' import type { D1Database } from '../../skills/data-portal/template/functions/api/_lib/types' function db(row: Record | null) { return { prepare() { const stmt = { bind() { return stmt }, async first() { return row as T | null }, async run() { return { meta: { changes: 0 } } }, async all() { return { results: [] as T[] } }, } return stmt }, } as unknown as D1Database } describe('resolveSession folders', () => { it('splits a comma grant into an array', async () => { const s = await resolveSession( db({ ownerId: 'alice', accountId: 'acc-a', folders: 'quotes,output' }), 'sess', 1, ) expect(s?.folders).toEqual(['quotes', 'output']) }) it('returns an empty array for an empty grant', async () => { const s = await resolveSession(db({ ownerId: 'alice', accountId: 'acc-a', folders: '' }), 'sess', 1) expect(s?.folders).toEqual([]) }) it('returns an empty array when the column is absent', async () => { const s = await resolveSession(db({ ownerId: 'alice', accountId: 'acc-a' }), 'sess', 1) expect(s?.folders).toEqual([]) }) }) describe('grantAllows', () => { it('admits everything when the grant is empty', () => { expect(grantAllows([], 'documents/secret.pdf')).toBe(true) }) it('admits a path whose top segment is granted', () => { expect(grantAllows(['quotes'], 'quotes/CR2969/quote.pdf')).toBe(true) }) it('refuses a path whose top segment is not granted', () => { expect(grantAllows(['quotes'], 'documents/secret.pdf')).toBe(false) }) it('matches the whole segment, so `quote` does not admit `quotes`', () => { expect(grantAllows(['quote'], 'quotes/x.pdf')).toBe(false) }) })