0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
CI / Shell Format Check (push) Waiting to run
CI / Check Ruby (3.4) (push) Waiting to run
CI / CI Config (push) Waiting to run
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Blocked by required conditions
CI / Build on Node ${{ matrix.node }} (push) Blocked by required conditions
CI / Style Check (push) Waiting to run
CI / Generate Assets (push) Waiting to run
CI / Check Python (3.14) (push) Waiting to run
CI / Check Python (3.9) (push) Waiting to run
CI / Build Docs (push) Waiting to run
CI / Code Scan Action (push) Waiting to run
CI / Site tests (push) Waiting to run
CI / webui tests (push) Waiting to run
CI / Run Integration Tests (push) Waiting to run
CI / Run Smoke Tests (push) Waiting to run
CI / Go Tests (push) Waiting to run
CI / Share Test (push) Waiting to run
CI / Redteam (Production API) (push) Waiting to run
CI / Redteam (Staging API) (push) Waiting to run
CI / GitHub Actions Lint (push) Waiting to run
CI / Check Ruby (3.0) (push) Waiting to run
release-please / release-please (push) Waiting to run
release-please / build (push) Blocked by required conditions
release-please / publish-npm (push) Blocked by required conditions
release-please / publish-npm-backfill (push) Waiting to run
release-please / docker (push) Blocked by required conditions
release-please / publish-code-scan-action (push) Blocked by required conditions
release-please / attest-code-scan-action (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
223 lines
7.4 KiB
TypeScript
223 lines
7.4 KiB
TypeScript
import * as path from 'path';
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const mockDb = { prepare: vi.fn() };
|
|
const mockMigrate = vi.fn();
|
|
const mockGetDb = vi.fn().mockResolvedValue(mockDb);
|
|
const mockCloseDbIfOpen = vi.fn();
|
|
const mockGetDirectory = vi.fn();
|
|
const mockLogger = {
|
|
debug: vi.fn(),
|
|
error: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
};
|
|
|
|
vi.mock('../src/database/index', () => ({
|
|
closeDbIfOpen: mockCloseDbIfOpen,
|
|
getDb: mockGetDb,
|
|
}));
|
|
|
|
vi.mock('../src/esm', () => ({
|
|
getDirectory: mockGetDirectory,
|
|
}));
|
|
|
|
vi.mock('../src/logger', () => ({
|
|
default: mockLogger,
|
|
}));
|
|
|
|
vi.mock('drizzle-orm/libsql/migrator', () => ({
|
|
migrate: mockMigrate,
|
|
}));
|
|
|
|
function makeBindingError(target = 'darwin-arm64'): NodeJS.ErrnoException {
|
|
const error: NodeJS.ErrnoException = new Error(
|
|
`Cannot find module '@libsql/${target}'\nRequire stack:\n- /app/node_modules/libsql/index.js`,
|
|
);
|
|
error.code = 'MODULE_NOT_FOUND';
|
|
return error;
|
|
}
|
|
|
|
describe('migrate', () => {
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
vi.clearAllMocks();
|
|
// Reset mock implementations to defaults
|
|
mockGetDb.mockResolvedValue(mockDb);
|
|
mockMigrate.mockReset();
|
|
mockGetDirectory.mockReset();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe('runDbMigrations', () => {
|
|
it('should run migrations successfully from source directory', async () => {
|
|
const sourceDir = '/project/src';
|
|
mockGetDirectory.mockReturnValue(sourceDir);
|
|
|
|
const { runDbMigrations } = await import('../src/migrate');
|
|
await runDbMigrations();
|
|
|
|
expect(mockGetDb).toHaveBeenCalledTimes(1);
|
|
expect(mockMigrate).toHaveBeenCalledTimes(1);
|
|
expect(mockMigrate).toHaveBeenCalledWith(mockDb, {
|
|
migrationsFolder: path.join(sourceDir, '..', 'drizzle'),
|
|
});
|
|
expect(mockLogger.debug).toHaveBeenCalledWith(
|
|
expect.stringContaining('Running database migrations from:'),
|
|
);
|
|
expect(mockLogger.debug).toHaveBeenCalledWith('Database migrations completed');
|
|
});
|
|
|
|
it('should run migrations from bundled dist/src directory', async () => {
|
|
const bundledDir = '/project/dist/src/server';
|
|
mockGetDirectory.mockReturnValue(bundledDir);
|
|
|
|
const { runDbMigrations } = await import('../src/migrate');
|
|
await runDbMigrations();
|
|
|
|
expect(mockMigrate).toHaveBeenCalledWith(mockDb, {
|
|
migrationsFolder: path.join('/project/', 'dist', 'drizzle'),
|
|
});
|
|
});
|
|
|
|
it('should run migrations from bundled server directory (dist/server/src)', async () => {
|
|
const bundledDir = '/project/dist/server/src/some/path';
|
|
mockGetDirectory.mockReturnValue(bundledDir);
|
|
|
|
const { runDbMigrations } = await import('../src/migrate');
|
|
await runDbMigrations();
|
|
|
|
expect(mockMigrate).toHaveBeenCalledWith(mockDb, {
|
|
migrationsFolder: path.join('/project/', 'dist', 'promptfoo', 'drizzle'),
|
|
});
|
|
});
|
|
|
|
it('should reject with error when getDb fails', async () => {
|
|
const dbError = new Error('Database connection failed');
|
|
mockGetDb.mockRejectedValue(dbError);
|
|
mockGetDirectory.mockReturnValue('/project/src');
|
|
|
|
const { runDbMigrations } = await import('../src/migrate');
|
|
await expect(runDbMigrations()).rejects.toThrow('Database connection failed');
|
|
|
|
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
expect.stringContaining('Database migration failed:'),
|
|
);
|
|
expect(mockMigrate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should log libsql binding miss with structured context instead of "migration failed"', async () => {
|
|
const bindingError = makeBindingError('linux-x64-gnu');
|
|
mockGetDb.mockRejectedValue(bindingError);
|
|
mockGetDirectory.mockReturnValue('/project/src');
|
|
|
|
const { runDbMigrations } = await import('../src/migrate');
|
|
await expect(runDbMigrations()).rejects.toBe(bindingError);
|
|
|
|
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
'SQLite dependency failed to load because the libsql platform binding is missing.',
|
|
{
|
|
platform: `${process.platform}-${process.arch}`,
|
|
missingPackage: '@libsql/linux-x64-gnu',
|
|
},
|
|
);
|
|
expect(mockMigrate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should demote binding-miss log to debug when suppressBindingErrorLogging is set', async () => {
|
|
const bindingError = makeBindingError('darwin-arm64');
|
|
mockGetDb.mockRejectedValue(bindingError);
|
|
mockGetDirectory.mockReturnValue('/project/src');
|
|
|
|
const { runDbMigrations } = await import('../src/migrate');
|
|
await expect(runDbMigrations({ suppressBindingErrorLogging: true })).rejects.toBe(
|
|
bindingError,
|
|
);
|
|
|
|
expect(mockLogger.error).not.toHaveBeenCalled();
|
|
expect(mockLogger.debug).toHaveBeenCalledWith(
|
|
'SQLite dependency failed to load because the libsql platform binding is missing.',
|
|
{
|
|
platform: `${process.platform}-${process.arch}`,
|
|
missingPackage: '@libsql/darwin-arm64',
|
|
},
|
|
);
|
|
});
|
|
|
|
it('should reject with error when migrate fails', async () => {
|
|
const migrationError = new Error('Migration failed: syntax error');
|
|
mockMigrate.mockImplementation(() => {
|
|
throw migrationError;
|
|
});
|
|
mockGetDirectory.mockReturnValue('/project/src');
|
|
|
|
const { runDbMigrations } = await import('../src/migrate');
|
|
await expect(runDbMigrations()).rejects.toThrow('Migration failed: syntax error');
|
|
|
|
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
expect.stringContaining('Database migration failed:'),
|
|
);
|
|
});
|
|
|
|
it('should pass the correct database instance to migrate', async () => {
|
|
const specificDb = { id: 'test-db-instance', prepare: vi.fn() };
|
|
mockGetDb.mockResolvedValue(specificDb);
|
|
mockGetDirectory.mockReturnValue('/project/src');
|
|
|
|
const { runDbMigrations } = await import('../src/migrate');
|
|
await runDbMigrations();
|
|
|
|
expect(mockMigrate).toHaveBeenCalledWith(specificDb, expect.any(Object));
|
|
});
|
|
});
|
|
|
|
describe('runDbMigrationsFromCli', () => {
|
|
let originalExitCode: number | string | null | undefined;
|
|
|
|
beforeEach(() => {
|
|
originalExitCode = process.exitCode;
|
|
process.exitCode = undefined;
|
|
mockGetDirectory.mockReturnValue('/project/src');
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.exitCode = originalExitCode;
|
|
});
|
|
|
|
it('should set exitCode to 0 after successful direct execution', async () => {
|
|
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => undefined) as never);
|
|
const { runDbMigrationsFromCli } = await import('../src/migrate');
|
|
|
|
try {
|
|
await runDbMigrationsFromCli();
|
|
|
|
expect(process.exitCode).toBe(0);
|
|
expect(mockCloseDbIfOpen).toHaveBeenCalledTimes(1);
|
|
expect(exitSpy).not.toHaveBeenCalled();
|
|
} finally {
|
|
exitSpy.mockRestore();
|
|
}
|
|
});
|
|
|
|
it('should set exitCode to 1 after failed direct execution', async () => {
|
|
mockGetDb.mockRejectedValue(new Error('Database connection failed'));
|
|
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => undefined) as never);
|
|
const { runDbMigrationsFromCli } = await import('../src/migrate');
|
|
|
|
try {
|
|
await runDbMigrationsFromCli();
|
|
|
|
expect(process.exitCode).toBe(1);
|
|
expect(mockCloseDbIfOpen).toHaveBeenCalledTimes(1);
|
|
expect(exitSpy).not.toHaveBeenCalled();
|
|
} finally {
|
|
exitSpy.mockRestore();
|
|
}
|
|
});
|
|
});
|
|
});
|