From e4de27edf8bab7b15a1564e79eb04d96adb62399 Mon Sep 17 00:00:00 2001 From: TechEnthusGH <289884007+TechEnthusGH@users.noreply.github.com> Date: Wed, 3 Jun 2026 18:06:18 +0000 Subject: [PATCH 1/2] test: cover durable object bootstrap helpers --- src/do.test.ts | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/do.test.ts b/src/do.test.ts index 272c4e9..de7d3a9 100644 --- a/src/do.test.ts +++ b/src/do.test.ts @@ -96,6 +96,79 @@ describe('StarbaseDBDurableObject Tests', () => { expect(instance.storage).toBeDefined() }) + it('should bootstrap internal temporary tables during construction', () => { + vi.clearAllMocks() + + new StarbaseDBDurableObject(mockDurableObjectState, mockEnv) + + const executedSql = mockStorage.sql.exec.mock.calls.map(([sql]) => sql) + expect(executedSql).toHaveLength(4) + expect(executedSql[0]).toContain('CREATE TABLE IF NOT EXISTS tmp_cache') + expect(executedSql[1]).toContain( + 'CREATE TABLE IF NOT EXISTS tmp_allowlist_queries' + ) + expect(executedSql[2]).toContain( + 'CREATE TABLE IF NOT EXISTS tmp_allowlist_rejections' + ) + expect(executedSql[3]).toContain( + 'CREATE TABLE IF NOT EXISTS tmp_rls_policies' + ) + }) + + it('should expose bound Durable Object helper methods from init()', async () => { + const getAlarm = vi.fn().mockResolvedValue(123) + const setAlarm = vi.fn().mockResolvedValue(undefined) + const deleteAlarm = vi.fn().mockResolvedValue(undefined) + const storage = { + ...mockStorage, + getAlarm, + setAlarm, + deleteAlarm, + } + const initialized = new StarbaseDBDurableObject( + { storage, getTags: vi.fn().mockReturnValue([]) } as any, + mockEnv + ).init() + + await expect(initialized.getAlarm()).resolves.toBe(123) + await initialized.setAlarm(Date.now() + 2000) + await initialized.deleteAlarm() + await expect( + initialized.executeQuery({ sql: 'SELECT * FROM users' }) + ).resolves.toEqual([ + { id: 1, name: 'Alice' }, + { id: 2, name: 'Bob' }, + ]) + + expect(getAlarm).toHaveBeenCalledOnce() + expect(setAlarm).toHaveBeenCalledOnce() + expect(deleteAlarm).toHaveBeenCalledOnce() + }) + + it('should execute a raw query with params and return cursor metadata', async () => { + const result = await instance.executeQuery({ + sql: 'SELECT * FROM users WHERE id = ?', + params: [1], + isRaw: true, + }) + + expect(mockStorage.sql.exec).toHaveBeenCalledWith( + 'SELECT * FROM users WHERE id = ?', + 1 + ) + expect(result).toEqual({ + columns: ['id', 'name'], + rows: [ + [1, 'Alice'], + [2, 'Bob'], + ], + meta: { + rows_read: 2, + rows_written: 1, + }, + }) + }) + it('should execute a query and return results', async () => { const sql = 'SELECT * FROM users' const result = await instance.executeQuery({ sql }) From 9ced75c085ce8bf9cc13ac23a6991fa6a2159c57 Mon Sep 17 00:00:00 2001 From: TechEnthusGH <289884007+TechEnthusGH@users.noreply.github.com> Date: Wed, 3 Jun 2026 18:27:01 +0000 Subject: [PATCH 2/2] test: strengthen durable object coverage tests --- src/do.test.ts | 84 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 24 deletions(-) diff --git a/src/do.test.ts b/src/do.test.ts index de7d3a9..a8d8610 100644 --- a/src/do.test.ts +++ b/src/do.test.ts @@ -102,50 +102,89 @@ describe('StarbaseDBDurableObject Tests', () => { new StarbaseDBDurableObject(mockDurableObjectState, mockEnv) const executedSql = mockStorage.sql.exec.mock.calls.map(([sql]) => sql) - expect(executedSql).toHaveLength(4) - expect(executedSql[0]).toContain('CREATE TABLE IF NOT EXISTS tmp_cache') - expect(executedSql[1]).toContain( - 'CREATE TABLE IF NOT EXISTS tmp_allowlist_queries' - ) - expect(executedSql[2]).toContain( - 'CREATE TABLE IF NOT EXISTS tmp_allowlist_rejections' - ) - expect(executedSql[3]).toContain( - 'CREATE TABLE IF NOT EXISTS tmp_rls_policies' + expect(executedSql).toEqual( + expect.arrayContaining([ + expect.stringContaining('CREATE TABLE IF NOT EXISTS tmp_cache'), + expect.stringContaining( + 'CREATE TABLE IF NOT EXISTS tmp_allowlist_queries' + ), + expect.stringContaining( + 'CREATE TABLE IF NOT EXISTS tmp_allowlist_rejections' + ), + expect.stringContaining( + 'CREATE TABLE IF NOT EXISTS tmp_rls_policies' + ), + ]) ) + + const bootstrapSql = executedSql.join('\n') + expect(bootstrapSql).toContain('"query" TEXT UNIQUE NOT NULL') + expect(bootstrapSql).toContain('sql_statement TEXT NOT NULL') + expect(bootstrapSql).toContain('created_at TEXT DEFAULT') + expect(bootstrapSql).toContain('CHECK(actions IN') }) it('should expose bound Durable Object helper methods from init()', async () => { const getAlarm = vi.fn().mockResolvedValue(123) const setAlarm = vi.fn().mockResolvedValue(undefined) const deleteAlarm = vi.fn().mockResolvedValue(undefined) + const sqlExec = vi.fn().mockReturnValue({ + columnNames: ['count'], + raw: vi.fn().mockReturnValue([[3]]), + toArray: vi.fn().mockReturnValue([{ count: 3 }]), + rowsRead: 1, + rowsWritten: 0, + }) const storage = { - ...mockStorage, + sql: { + exec: sqlExec, + databaseSize: 4096, + }, getAlarm, setAlarm, deleteAlarm, } - const initialized = new StarbaseDBDurableObject( + const durableObject = new StarbaseDBDurableObject( { storage, getTags: vi.fn().mockReturnValue([]) } as any, mockEnv - ).init() + ) + const initialized = durableObject.init() + sqlExec.mockClear() await expect(initialized.getAlarm()).resolves.toBe(123) await initialized.setAlarm(Date.now() + 2000) await initialized.deleteAlarm() + await expect(initialized.getStatistics()).resolves.toEqual({ + databaseSize: 4096, + activeConnections: 0, + recentQueries: 3, + }) await expect( - initialized.executeQuery({ sql: 'SELECT * FROM users' }) - ).resolves.toEqual([ - { id: 1, name: 'Alice' }, - { id: 2, name: 'Bob' }, - ]) + initialized.executeQuery({ + sql: 'SELECT count(*) FROM tmp_query_log', + }) + ).resolves.toEqual([{ count: 3 }]) expect(getAlarm).toHaveBeenCalledOnce() expect(setAlarm).toHaveBeenCalledOnce() expect(deleteAlarm).toHaveBeenCalledOnce() + expect(sqlExec).toHaveBeenCalledWith( + expect.stringContaining('FROM tmp_query_log') + ) + expect(sqlExec).toHaveBeenCalledWith( + 'SELECT count(*) FROM tmp_query_log' + ) }) it('should execute a raw query with params and return cursor metadata', async () => { + mockStorage.sql.exec.mockReturnValueOnce({ + columnNames: ['id', 'name'], + raw: vi.fn().mockReturnValue([[1, 'Alice']]), + toArray: vi.fn(), + rowsRead: 1, + rowsWritten: 0, + }) + const result = await instance.executeQuery({ sql: 'SELECT * FROM users WHERE id = ?', params: [1], @@ -158,13 +197,10 @@ describe('StarbaseDBDurableObject Tests', () => { ) expect(result).toEqual({ columns: ['id', 'name'], - rows: [ - [1, 'Alice'], - [2, 'Bob'], - ], + rows: [[1, 'Alice']], meta: { - rows_read: 2, - rows_written: 1, + rows_read: 1, + rows_written: 0, }, }) })