Files
pchelpmaster/tests/support/clients.ts
T
kamaskandClaude Sonnet 5.5 1109f68e11 test(api): type-check harness and cover automigrate and auth isolation
- add tsconfig.tests.json and run tsc in test:api
- move vitest ProvidedContext types to provided-context.d.ts
- rename vitest.api.config.ts to .mts
- kill signal-test fixture with SIGTERM first so cleanup still runs
- add tests: no files in pb_migrations, independent user/guest clients
- docs: drop superuser env vars from tests in README, note tsc in testing.md

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EvmtStx737hu8gXe15e55W
2026-10-26 19:46:45 +03:00

38 lines
1.3 KiB
TypeScript

// 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';
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;
}