|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import type { |
| 3 | + StorageAdapter, |
| 4 | + StorageParameters, |
| 5 | + StorageRunResult, |
| 6 | +} from '@framers/sql-storage-adapter'; |
| 7 | +import { Brain } from '../Brain.js'; |
| 8 | + |
| 9 | +class RecordingPostgresAdapter implements StorageAdapter { |
| 10 | + readonly kind = 'postgres' as const; |
| 11 | + readonly execStatements: string[] = []; |
| 12 | + |
| 13 | + async open(): Promise<void> {} |
| 14 | + |
| 15 | + async close(): Promise<void> {} |
| 16 | + |
| 17 | + async run(_sql: string, _params?: StorageParameters): Promise<StorageRunResult> { |
| 18 | + return { changes: 1 }; |
| 19 | + } |
| 20 | + |
| 21 | + async get<T = unknown>(sql: string, _params?: StorageParameters): Promise<T | null> { |
| 22 | + if (sql.includes('information_schema.tables') || sql.includes('information_schema.columns')) { |
| 23 | + return { exists: false } as T; |
| 24 | + } |
| 25 | + return null; |
| 26 | + } |
| 27 | + |
| 28 | + async all<T = unknown>(_sql: string, _params?: StorageParameters): Promise<T[]> { |
| 29 | + return []; |
| 30 | + } |
| 31 | + |
| 32 | + async exec(sql: string): Promise<void> { |
| 33 | + this.execStatements.push(sql); |
| 34 | + } |
| 35 | + |
| 36 | + async transaction<T>(fn: (trx: StorageAdapter) => Promise<T>): Promise<T> { |
| 37 | + return fn(this); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +describe('Brain Postgres schema initialization', () => { |
| 42 | + it('emits Postgres-compatible DDL for fresh brain schemas', async () => { |
| 43 | + const adapter = new RecordingPostgresAdapter(); |
| 44 | + |
| 45 | + await Brain.openWithAdapter(adapter, { brainId: 'pg-brain' }); |
| 46 | + |
| 47 | + const ddl = adapter.execStatements.join('\n'); |
| 48 | + expect(ddl).not.toContain('AUTOINCREMENT'); |
| 49 | + expect(ddl).not.toMatch(/\bBLOB\b/); |
| 50 | + expect(ddl).toContain('GENERATED ALWAYS AS IDENTITY PRIMARY KEY'); |
| 51 | + expect(ddl).toContain('BYTEA'); |
| 52 | + }); |
| 53 | +}); |
0 commit comments