mirror of
https://github.com/bookorbit/bookorbit.git
synced 2026-11-03 08:21:39 -06:00
346 lines
10 KiB
TypeScript
346 lines
10 KiB
TypeScript
import { randomUUID } from 'crypto';
|
|
import { hash } from 'bcryptjs';
|
|
import fastifyCookie from '@fastify/cookie';
|
|
import { ValidationPipe } from '@nestjs/common';
|
|
import { Test } from '@nestjs/testing';
|
|
import { FastifyAdapter, type NestFastifyApplication } from '@nestjs/platform-fastify';
|
|
import { mkdir } from 'fs/promises';
|
|
import { and, eq } from 'drizzle-orm';
|
|
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
|
import { DEFAULT_FORMAT_PRIORITY, type Permission } from '@projectx/types';
|
|
|
|
import { AppModule } from '../../../src/app.module';
|
|
import { DB } from '../../../src/db';
|
|
import * as schema from '../../../src/db/schema';
|
|
import { MetadataService } from '../../../src/modules/metadata/metadata.service';
|
|
import { BookBucketWatcherService } from '../../../src/modules/book-bucket/book-bucket-watcher.service';
|
|
import { seedLibrary, waitForScanCompletion } from '../app-harness';
|
|
import { createReaderStateIsolationFixtureRoot, type ReaderStateIsolationFixtureRoot } from './reader-state-isolation-fixture-builder';
|
|
|
|
type Db = NodePgDatabase<typeof schema>;
|
|
|
|
const ADMIN_SETUP_DTO = {
|
|
username: 'reader-state-e2e-admin',
|
|
name: 'Reader State E2E Admin',
|
|
email: 'reader-state-e2e-admin@example.com',
|
|
password: 'ReaderStateAdmin123',
|
|
};
|
|
|
|
interface EnvSnapshot {
|
|
booksPath: string | undefined;
|
|
bookBucketPath: string | undefined;
|
|
}
|
|
|
|
export interface ReaderStateIsolationE2EContext {
|
|
app: NestFastifyApplication;
|
|
db: Db;
|
|
adminToken: string;
|
|
fixture: ReaderStateIsolationFixtureRoot;
|
|
envSnapshot: EnvSnapshot;
|
|
}
|
|
|
|
export interface TestUserSession {
|
|
userId: number;
|
|
username: string;
|
|
password: string;
|
|
accessToken: string;
|
|
}
|
|
|
|
export interface CreatedLibrary {
|
|
libraryId: number;
|
|
libraryFolderId: number;
|
|
folderPath: string;
|
|
}
|
|
|
|
export interface LocatedBookFile {
|
|
libraryId: number;
|
|
bookId: number;
|
|
bookFileId: number;
|
|
absolutePath: string;
|
|
relPath: string | null;
|
|
format: string | null;
|
|
}
|
|
|
|
export function authHeader(token: string): Record<string, string> {
|
|
return { authorization: `Bearer ${token}` };
|
|
}
|
|
|
|
export async function createReaderStateIsolationE2EContext(): Promise<ReaderStateIsolationE2EContext> {
|
|
const fixture = await createReaderStateIsolationFixtureRoot();
|
|
const envSnapshot: EnvSnapshot = {
|
|
booksPath: process.env.BOOKS_PATH,
|
|
bookBucketPath: process.env.BOOK_BUCKET_PATH,
|
|
};
|
|
|
|
process.env.BOOKS_PATH = fixture.booksPath;
|
|
process.env.BOOK_BUCKET_PATH = fixture.bookBucketPath;
|
|
|
|
const moduleFixture = await Test.createTestingModule({
|
|
imports: [AppModule],
|
|
})
|
|
.overrideProvider(MetadataService)
|
|
.useValue(makeMetadataNoopMock())
|
|
.compile();
|
|
|
|
const app = moduleFixture.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
|
|
app.setGlobalPrefix('api/v1');
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
transform: true,
|
|
}),
|
|
);
|
|
await app.register(fastifyCookie as never);
|
|
await app.init();
|
|
await app.getHttpAdapter().getInstance().ready();
|
|
|
|
await stopBookBucketWatcher(app);
|
|
|
|
const db = app.get<Db>(DB);
|
|
const adminToken = await getAdminToken(app, db);
|
|
|
|
return {
|
|
app,
|
|
db,
|
|
adminToken,
|
|
fixture,
|
|
envSnapshot,
|
|
};
|
|
}
|
|
|
|
export async function closeReaderStateIsolationE2EContext(ctx: ReaderStateIsolationE2EContext): Promise<void> {
|
|
await ctx.app.close();
|
|
await ctx.fixture.cleanup();
|
|
restoreEnv(ctx.envSnapshot);
|
|
}
|
|
|
|
export async function createLibraryWithFolder(
|
|
ctx: ReaderStateIsolationE2EContext,
|
|
options: {
|
|
mode?: 'book_per_file' | 'book_per_folder';
|
|
allowedFormats?: string[];
|
|
name?: string;
|
|
} = {},
|
|
): Promise<CreatedLibrary> {
|
|
const folderPath = `${ctx.fixture.booksPath}/library-${randomUUID()}`;
|
|
await mkdir(folderPath, { recursive: true });
|
|
|
|
const [library] = await ctx.db
|
|
.insert(schema.libraries)
|
|
.values({
|
|
name: options.name ?? `reader-state-library-${randomUUID()}`,
|
|
watch: false,
|
|
organizationMode: options.mode ?? 'book_per_file',
|
|
allowedFormats: options.allowedFormats ?? [],
|
|
excludePatterns: [],
|
|
formatPriority: [...DEFAULT_FORMAT_PRIORITY],
|
|
})
|
|
.returning({ id: schema.libraries.id });
|
|
|
|
const [libraryFolder] = await ctx.db
|
|
.insert(schema.libraryFolders)
|
|
.values({
|
|
libraryId: library.id,
|
|
path: folderPath,
|
|
})
|
|
.returning({ id: schema.libraryFolders.id });
|
|
|
|
return {
|
|
libraryId: library.id,
|
|
libraryFolderId: libraryFolder.id,
|
|
folderPath,
|
|
};
|
|
}
|
|
|
|
export async function triggerAndWaitForLibraryScan(
|
|
ctx: ReaderStateIsolationE2EContext,
|
|
libraryId: number,
|
|
timeoutMs = 45_000,
|
|
): Promise<typeof schema.scanJobs.$inferSelect> {
|
|
const response = await ctx.app.inject({
|
|
method: 'POST',
|
|
url: `/api/v1/scanner/libraries/${libraryId}/scan`,
|
|
headers: authHeader(ctx.adminToken),
|
|
});
|
|
|
|
if (response.statusCode !== 202) {
|
|
throw new Error(`Scan endpoint failed: ${response.statusCode} ${response.body}`);
|
|
}
|
|
|
|
const body = response.json() as { jobId?: number };
|
|
if (!body.jobId) {
|
|
throw new Error(`Scan endpoint returned no jobId: ${response.body}`);
|
|
}
|
|
|
|
return waitForScanCompletion(ctx.db, body.jobId, timeoutMs);
|
|
}
|
|
|
|
export async function locateBookByAbsolutePath(ctx: ReaderStateIsolationE2EContext, absolutePath: string): Promise<LocatedBookFile> {
|
|
const [row] = await ctx.db
|
|
.select({
|
|
libraryId: schema.books.libraryId,
|
|
bookId: schema.books.id,
|
|
bookFileId: schema.bookFiles.id,
|
|
absolutePath: schema.bookFiles.absolutePath,
|
|
relPath: schema.bookFiles.relPath,
|
|
format: schema.bookFiles.format,
|
|
})
|
|
.from(schema.bookFiles)
|
|
.innerJoin(schema.books, eq(schema.books.id, schema.bookFiles.bookId))
|
|
.where(and(eq(schema.books.status, 'present'), eq(schema.bookFiles.absolutePath, absolutePath)))
|
|
.limit(1);
|
|
|
|
if (!row) {
|
|
throw new Error(`No present book file found for path "${absolutePath}"`);
|
|
}
|
|
|
|
return row;
|
|
}
|
|
|
|
export async function createUserAndLogin(
|
|
ctx: ReaderStateIsolationE2EContext,
|
|
options: {
|
|
permissions?: Permission[];
|
|
isSuperuser?: boolean;
|
|
isDefaultPassword?: boolean;
|
|
active?: boolean;
|
|
username?: string;
|
|
password?: string;
|
|
email?: string;
|
|
} = {},
|
|
): Promise<TestUserSession> {
|
|
const suffix = randomUUID().replaceAll('-', '');
|
|
const username = options.username ?? `reader-state-user-${suffix}`;
|
|
const password = options.password ?? 'ReaderStateUser123';
|
|
const email = options.email ?? `${username}@example.com`;
|
|
const passwordHash = await hash(password, 12);
|
|
|
|
const [created] = await ctx.db
|
|
.insert(schema.users)
|
|
.values({
|
|
username,
|
|
name: `Reader State User ${suffix}`,
|
|
email,
|
|
passwordHash,
|
|
active: options.active ?? true,
|
|
isSuperuser: options.isSuperuser ?? false,
|
|
isDefaultPassword: options.isDefaultPassword ?? false,
|
|
provisioningMethod: 'local',
|
|
})
|
|
.returning({ id: schema.users.id });
|
|
|
|
const permissions = options.permissions ?? [];
|
|
if (permissions.length > 0) {
|
|
await ctx.db.insert(schema.userPermissions).values(permissions.map((permissionName) => ({ userId: created.id, permissionName })));
|
|
}
|
|
|
|
const accessToken = await loginForToken(ctx.app, username, password);
|
|
if (!accessToken) {
|
|
throw new Error(`Login failed for ${username}`);
|
|
}
|
|
|
|
return {
|
|
userId: created.id,
|
|
username,
|
|
password,
|
|
accessToken,
|
|
};
|
|
}
|
|
|
|
export async function grantLibraryAccess(
|
|
ctx: ReaderStateIsolationE2EContext,
|
|
userId: number,
|
|
libraryId: number,
|
|
accessLevel: 'viewer' | 'editor' | 'owner' = 'viewer',
|
|
): Promise<void> {
|
|
await ctx.db
|
|
.insert(schema.userLibraryAccess)
|
|
.values({ userId, libraryId, accessLevel })
|
|
.onConflictDoUpdate({
|
|
target: [schema.userLibraryAccess.userId, schema.userLibraryAccess.libraryId],
|
|
set: { accessLevel },
|
|
});
|
|
}
|
|
|
|
async function stopBookBucketWatcher(app: NestFastifyApplication): Promise<void> {
|
|
const watcher = app.get(BookBucketWatcherService);
|
|
await watcher.onModuleDestroy();
|
|
}
|
|
|
|
async function getAdminToken(app: NestFastifyApplication, db: Db): Promise<string> {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/api/v1/auth/setup',
|
|
payload: ADMIN_SETUP_DTO,
|
|
});
|
|
|
|
if (response.statusCode === 409) {
|
|
const existingToken = await loginForToken(app, ADMIN_SETUP_DTO.username, ADMIN_SETUP_DTO.password);
|
|
if (existingToken) return existingToken;
|
|
|
|
const suffix = randomUUID().replaceAll('-', '');
|
|
const fallbackUsername = `reader-state-e2e-admin-${suffix}`;
|
|
const passwordHash = await hash(ADMIN_SETUP_DTO.password, 12);
|
|
|
|
await db.insert(schema.users).values({
|
|
username: fallbackUsername,
|
|
name: 'Reader State E2E Admin',
|
|
email: `${fallbackUsername}@example.com`,
|
|
passwordHash,
|
|
isSuperuser: true,
|
|
isDefaultPassword: false,
|
|
provisioningMethod: 'local',
|
|
});
|
|
|
|
const fallbackToken = await loginForToken(app, fallbackUsername, ADMIN_SETUP_DTO.password);
|
|
if (fallbackToken) return fallbackToken;
|
|
throw new Error('Setup is already complete and fallback admin login failed');
|
|
}
|
|
|
|
if (response.statusCode !== 201) {
|
|
throw new Error(`Unable to complete setup: ${response.statusCode} ${response.body}`);
|
|
}
|
|
|
|
const body = response.json() as { accessToken?: string };
|
|
if (!body.accessToken) {
|
|
throw new Error('Setup succeeded but accessToken is missing');
|
|
}
|
|
|
|
return body.accessToken;
|
|
}
|
|
|
|
async function loginForToken(app: NestFastifyApplication, username: string, password: string): Promise<string | null> {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/api/v1/auth/login',
|
|
payload: { username, password },
|
|
});
|
|
if (response.statusCode !== 200) return null;
|
|
const body = response.json() as { accessToken?: string };
|
|
return body.accessToken ?? null;
|
|
}
|
|
|
|
function makeMetadataNoopMock(): Pick<
|
|
MetadataService,
|
|
'extractAndSave' | 'refreshCoverForBook' | 'extractAudioFileDuration' | 'aggregateAudioDuration' | 'extractAudioChaptersAndNarrators'
|
|
> {
|
|
return {
|
|
extractAndSave: () => Promise.resolve(undefined),
|
|
refreshCoverForBook: () => Promise.resolve(false),
|
|
extractAudioFileDuration: () => Promise.resolve(undefined),
|
|
aggregateAudioDuration: () => Promise.resolve(undefined),
|
|
extractAudioChaptersAndNarrators: () => Promise.resolve(undefined),
|
|
};
|
|
}
|
|
|
|
function restoreEnv(snapshot: EnvSnapshot): void {
|
|
if (snapshot.booksPath === undefined) delete process.env.BOOKS_PATH;
|
|
else process.env.BOOKS_PATH = snapshot.booksPath;
|
|
|
|
if (snapshot.bookBucketPath === undefined) delete process.env.BOOK_BUCKET_PATH;
|
|
else process.env.BOOK_BUCKET_PATH = snapshot.bookBucketPath;
|
|
}
|
|
|
|
export { seedLibrary };
|