mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
The repo had no automated tests. Every change was verified by hand. Adds four layers, 2023 tests in total, runnable with one command: - 1191 unit tests over the pure logic in lib/, including the full computeProjectAccess permission matrix and the billing gate - 167 component and hook tests in jsdom, covering the hooks that hold real logic rather than presentational wrappers - 647 API integration tests against a real Postgres, with only auth() mocked, including a data-driven sweep asserting that none of the 60 route modules answers 2xx to an unauthenticated caller - 18 Playwright specs driving a real browser against a real build Infrastructure: vitest.config.ts with three projects, a disposable Postgres and MinIO in docker-compose.test.yml, factories and helpers under tests/, scripts/test.sh as the single entry point, a pre-push hook running bun run verify, and CI split into check, test and e2e jobs. The test database is built with prisma db push plus a replay of the hand-written SQL, because prisma migrate deploy cannot build this schema from empty: the migration history has no captured baseline. This mirrors what scripts/docker-db-bootstrap.ts already does in production, and tests/setup/db-global.ts carries a drift guard so a new migration fails the run until someone reviews it. Production code is unchanged apart from one pure-function extraction out of use-video-player.ts, which was too large to test in jsdom. Several tests pin behaviour that looks wrong, each marked KNOWN BUG in place. TESTING.md section 12 records where the plan turned out to be wrong, and AGENTS.md now states which layer a change needs a test in.
96 lines
3.8 KiB
TypeScript
96 lines
3.8 KiB
TypeScript
import bcrypt from 'bcryptjs';
|
|
import { BillingSubscriptionStatus, type Prisma, type User } from '@prisma/client';
|
|
import { db } from '@/lib/db';
|
|
import { nextSeq } from './seq';
|
|
|
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
|
// Cheap on purpose. bcrypt at the production cost factor takes ~100ms, which is
|
|
// dead time repeated across every test that needs a user with a password.
|
|
const TEST_BCRYPT_ROUNDS = 4;
|
|
|
|
export interface CreateUserInput {
|
|
name?: string;
|
|
email?: string;
|
|
/** Plain text. Hashed before insert, so the row never holds it. */
|
|
password?: string;
|
|
emailVerified?: Date | null;
|
|
onboardingCompletedAt?: Date | null;
|
|
trialEndsAt?: Date | null;
|
|
billingTrialConsumedAt?: Date | null;
|
|
subscriptionStatus?: BillingSubscriptionStatus;
|
|
stripeCustomerId?: string | null;
|
|
stripeSubscriptionId?: string | null;
|
|
stripePriceId?: string | null;
|
|
stripeCurrentPeriodEnd?: Date | null;
|
|
stripeCancelAtPeriodEnd?: boolean;
|
|
stripeCancelAt?: Date | null;
|
|
billingAccessEndedAt?: Date | null;
|
|
}
|
|
|
|
/**
|
|
* A user with billing access, via a trial that ends in seven days.
|
|
*
|
|
* That default matters: OPENFRAME_ENABLE_STRIPE is true in .env.test, so
|
|
* hasBillingAccess() is armed and a user without any of trialEndsAt /
|
|
* stripeCurrentPeriodEnd / an active status is locked out of their own
|
|
* workspaces. Pass `trialEndsAt` in the past (or use createExpiredUser) to test
|
|
* that gate.
|
|
*/
|
|
export async function createUser(input: CreateUserInput = {}): Promise<User> {
|
|
const seq = nextSeq();
|
|
|
|
const data: Prisma.UserCreateInput = {
|
|
name: input.name ?? `User ${seq}`,
|
|
email: input.email ?? `user-${seq}@example.com`,
|
|
emailVerified: input.emailVerified === undefined ? new Date() : input.emailVerified,
|
|
onboardingCompletedAt:
|
|
input.onboardingCompletedAt === undefined ? new Date() : input.onboardingCompletedAt,
|
|
trialEndsAt:
|
|
input.trialEndsAt === undefined ? new Date(Date.now() + 7 * DAY_MS) : input.trialEndsAt,
|
|
billingTrialConsumedAt: input.billingTrialConsumedAt ?? null,
|
|
subscriptionStatus: input.subscriptionStatus ?? BillingSubscriptionStatus.FREE,
|
|
stripeCustomerId: input.stripeCustomerId ?? null,
|
|
stripeSubscriptionId: input.stripeSubscriptionId ?? null,
|
|
stripePriceId: input.stripePriceId ?? null,
|
|
stripeCurrentPeriodEnd: input.stripeCurrentPeriodEnd ?? null,
|
|
stripeCancelAtPeriodEnd: input.stripeCancelAtPeriodEnd ?? false,
|
|
stripeCancelAt: input.stripeCancelAt ?? null,
|
|
billingAccessEndedAt: input.billingAccessEndedAt ?? null,
|
|
};
|
|
|
|
if (input.password !== undefined) {
|
|
data.password = await bcrypt.hash(input.password, TEST_BCRYPT_ROUNDS);
|
|
}
|
|
|
|
return db.user.create({ data });
|
|
}
|
|
|
|
/**
|
|
* A user whose trial ran out 30 days ago and who has no subscription, so
|
|
* hasBillingAccess() is false and buildBillingAccessWhereInput() excludes them.
|
|
*/
|
|
export function createExpiredUser(input: CreateUserInput = {}): Promise<User> {
|
|
const trialEndsAt = new Date(Date.now() - 30 * DAY_MS);
|
|
return createUser({
|
|
...input,
|
|
trialEndsAt,
|
|
billingTrialConsumedAt: input.billingTrialConsumedAt ?? trialEndsAt,
|
|
billingAccessEndedAt: input.billingAccessEndedAt ?? trialEndsAt,
|
|
});
|
|
}
|
|
|
|
/** A user on a paid, active subscription rather than a trial. */
|
|
export function createSubscribedUser(input: CreateUserInput = {}): Promise<User> {
|
|
const seq = nextSeq();
|
|
return createUser({
|
|
...input,
|
|
subscriptionStatus: input.subscriptionStatus ?? BillingSubscriptionStatus.ACTIVE,
|
|
trialEndsAt: input.trialEndsAt ?? null,
|
|
stripeCustomerId: input.stripeCustomerId ?? `cus_test_${seq}`,
|
|
stripeSubscriptionId: input.stripeSubscriptionId ?? `sub_test_${seq}`,
|
|
stripePriceId: input.stripePriceId ?? process.env.STRIPE_PRICE_ID ?? 'price_test',
|
|
stripeCurrentPeriodEnd: input.stripeCurrentPeriodEnd ?? new Date(Date.now() + 30 * DAY_MS),
|
|
});
|
|
}
|