Files
opencode-manager/backend/test/db/secrets-agent-hidden-migration.test.ts
T

74 lines
2.6 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { Database } from 'bun:sqlite'
import { z } from 'zod'
import { migrate } from '../../src/db/migration-runner'
import { allMigrations } from '../../src/db/migrations'
import migration from '../../src/db/migrations/202612031800-secrets-agent-hidden'
import { selectRow } from '../../src/db/rows'
import { SecretsService } from '../../src/services/secrets'
import { encryptSecret } from '../../src/utils/crypto'
const MIGRATION_ID = '202612031800-secrets-agent-hidden'
const SecretAgentHiddenRowSchema = z.object({ agent_hidden: z.number(), allowed_hosts: z.string() })
const AppliedMigrationRowSchema = z.object({ id: z.string() })
function migrateBeforeAgentHidden(db: Database): void {
const index = allMigrations.findIndex((entry) => entry.id === MIGRATION_ID)
migrate(db, allMigrations.slice(0, index))
}
function insertSecret(db: Database, kind: 'secret' | 'variable', name: string, value: string): void {
const now = Date.now()
db.prepare(
'INSERT INTO secrets (kind, repo_id, environment, name, value, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)',
).run(kind, null, '', name, value, now, now)
}
describe('secrets agent hidden migration', () => {
let db: Database
beforeEach(() => {
db = new Database(':memory:')
})
afterEach(() => {
db.close()
})
it('adds the hidden columns with safe defaults for existing rows', () => {
migrateBeforeAgentHidden(db)
insertSecret(db, 'variable', 'LOG_LEVEL', 'debug')
migrate(db, allMigrations)
expect(
selectRow(db, SecretAgentHiddenRowSchema, 'SELECT agent_hidden, allowed_hosts FROM secrets WHERE name = ?', 'LOG_LEVEL'),
).toEqual({ agent_hidden: 0, allowed_hosts: '[]' })
expect(
selectRow(db, AppliedMigrationRowSchema, 'SELECT id FROM applied_migrations WHERE id = ?', MIGRATION_ID),
).toEqual({ id: MIGRATION_ID })
})
it('reads an existing secret as visible with no allowed hosts', () => {
migrateBeforeAgentHidden(db)
insertSecret(db, 'secret', 'TOKEN', encryptSecret('super-secret'))
migrate(db, allMigrations)
const service = new SecretsService(db)
const resolved = service.resolveValues(1, null)
expect(resolved.secrets.TOKEN).toBe('super-secret')
expect(resolved.hidden).toEqual({})
expect(service.list('secret', null)).toEqual([
expect.objectContaining({ name: 'TOKEN', agentHidden: false, allowedHosts: [] }),
])
})
it('does not re-add columns when applied again', () => {
migrate(db, allMigrations)
expect(() => migration.up(db)).not.toThrow()
})
})