mirror of
https://github.com/kamask-dev/pchelpmaster.git
synced 2026-11-11 05:49:38 +00:00
test(api): add temporary pocketbase harness for api tests
`pnpm test:api` starts an isolated PocketBase with project migrations and hooks in a temp dir, provides SDK client helpers and cleans up after the run, on failure and on SIGINT/SIGTERM. The superuser is generated per instance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EvmtStx737hu8gXe15e55W
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b7b32a0457
commit
5d5b2af318
@@ -0,0 +1,45 @@
|
||||
// PocketBase SDK clients for tests; each helper returns its own instance with an in-memory auth store.
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import PocketBase, { BaseAuthStore } from 'pocketbase';
|
||||
import { inject } from 'vitest';
|
||||
|
||||
declare module 'vitest' {
|
||||
export interface ProvidedContext {
|
||||
pbUrl: string;
|
||||
pbDataDir: string;
|
||||
pbSuperuser: { email: string; password: string };
|
||||
}
|
||||
}
|
||||
|
||||
export interface TestUser {
|
||||
id: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export function asGuest(): PocketBase {
|
||||
return new PocketBase(inject('pbUrl'), new BaseAuthStore());
|
||||
}
|
||||
|
||||
export async function asSuperuser(): Promise<PocketBase> {
|
||||
const { email, password } = inject('pbSuperuser');
|
||||
const pb = asGuest();
|
||||
await pb.collection('_superusers').authWithPassword(email, password);
|
||||
return pb;
|
||||
}
|
||||
|
||||
export async function createUser(overrides: Record<string, unknown> = {}): Promise<TestUser> {
|
||||
const data = { email: `user-${randomUUID()}@test.local`, password: randomUUID(), ...overrides };
|
||||
const record = await (
|
||||
await asSuperuser()
|
||||
)
|
||||
.collection('users')
|
||||
.create({ ...data, passwordConfirm: data.password });
|
||||
return { id: record.id, email: String(data.email), password: String(data.password) };
|
||||
}
|
||||
|
||||
export async function asUser(user: TestUser): Promise<PocketBase> {
|
||||
const pb = asGuest();
|
||||
await pb.collection('users').authWithPassword(user.email, user.password);
|
||||
return pb;
|
||||
}
|
||||
Reference in New Issue
Block a user