94 lines
3.0 KiB
TypeScript
94 lines
3.0 KiB
TypeScript
import Database from 'better-sqlite3';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { mkdtempSync, rmSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import path from 'node:path';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const temporaryDirectories: string[] = [];
|
|
|
|
afterEach(async () => {
|
|
const { closeDatabase } = await import('./database.js');
|
|
closeDatabase();
|
|
delete process.env.SENDEWERK_DB_PATH;
|
|
delete process.env.SENDEWERK_UPLOAD_DIR;
|
|
for (const directory of temporaryDirectories.splice(0)) {
|
|
rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
vi.resetModules();
|
|
});
|
|
|
|
describe('database setup', () => {
|
|
it('runs migrations and the seed repeatedly in an isolated database', () => {
|
|
const directory = mkdtempSync(path.join(tmpdir(), 'sendewerk-test-'));
|
|
temporaryDirectories.push(directory);
|
|
const databasePath = path.join(directory, 'test.sqlite');
|
|
const environment = {
|
|
...process.env,
|
|
SENDEWERK_DB_PATH: databasePath,
|
|
SENDEWERK_UPLOAD_DIR: path.join(directory, 'uploads')
|
|
};
|
|
|
|
for (let run = 0; run < 2; run += 1) {
|
|
execFileSync(process.execPath, ['scripts/migrate.mjs'], {
|
|
cwd: process.cwd(),
|
|
env: environment
|
|
});
|
|
execFileSync(process.execPath, ['scripts/seed.mjs'], {
|
|
cwd: process.cwd(),
|
|
env: environment
|
|
});
|
|
}
|
|
|
|
const database = new Database(databasePath, { readonly: true });
|
|
const counts = database
|
|
.prepare(
|
|
`SELECT
|
|
(SELECT count(*) FROM schema_migrations) AS migrations,
|
|
(SELECT count(*) FROM pieces) AS pieces,
|
|
(SELECT count(*) FROM shows) AS shows,
|
|
(SELECT count(*) FROM piece_usages) AS usages,
|
|
(SELECT count(*) FROM show_templates) AS templates,
|
|
(SELECT count(*) FROM show_sections WHERE is_reserve_bank = 1) AS reserve_banks`
|
|
)
|
|
.get() as {
|
|
migrations: number;
|
|
pieces: number;
|
|
shows: number;
|
|
usages: number;
|
|
templates: number;
|
|
reserve_banks: number;
|
|
};
|
|
database.close();
|
|
|
|
expect(counts).toEqual({
|
|
migrations: 11,
|
|
pieces: 6,
|
|
shows: 2,
|
|
usages: 5,
|
|
templates: 2,
|
|
reserve_banks: 2
|
|
});
|
|
expect(databasePath).not.toBe(path.resolve('.data/sendewerk.sqlite'));
|
|
});
|
|
|
|
it('does not count reserve pieces or explicit sections twice', async () => {
|
|
const directory = mkdtempSync(path.join(tmpdir(), 'sendewerk-duration-test-'));
|
|
temporaryDirectories.push(directory);
|
|
process.env.SENDEWERK_DB_PATH = path.join(directory, 'test.sqlite');
|
|
process.env.SENDEWERK_UPLOAD_DIR = path.join(directory, 'uploads');
|
|
|
|
execFileSync(process.execPath, ['scripts/migrate.mjs'], {
|
|
cwd: process.cwd(),
|
|
env: process.env
|
|
});
|
|
execFileSync(process.execPath, ['scripts/seed.mjs'], { cwd: process.cwd(), env: process.env });
|
|
const { getShow } = await import('./previews.js');
|
|
const show = getShow();
|
|
|
|
expect(show.sections.map((section) => section.durationSeconds)).toEqual([300, 1200, 3600]);
|
|
expect(show.totalDurationSeconds).toBe(5100);
|
|
expect(show.sections[1].usages.find((usage) => usage.isReserve)?.durationSeconds).toBe(540);
|
|
});
|
|
});
|