Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DATABASE_URL=postgresql://USER:PASSWORD@HOST:5432/DATABASE?schema=public
TEST_DATABASE_URL=postgresql://USER:PASSWORD@HOST:5432/TEST_DATABASE?schema=public
JWT_SECRET=replace-with-a-secure-secret
JWT_REFRESH_SECRET=replace-with-a-secure-refresh-secret
PORT=3000
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
--health-retries 5
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/invoiceflow_test?schema=public
TEST_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/invoiceflow_test?schema=public
JWT_SECRET: test-jwt-secret-for-ci
JWT_REFRESH_SECRET: test-refresh-secret-for-ci

Expand All @@ -50,6 +51,7 @@ jobs:
- name: Setup test environment variables
run: |
echo "DATABASE_URL=postgresql://postgres:postgres@localhost:5432/invoiceflow_test?schema=public" >> $GITHUB_ENV
echo "TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/invoiceflow_test?schema=public" >> $GITHUB_ENV
echo "JWT_SECRET=test-jwt-secret-for-ci" >> $GITHUB_ENV
echo "JWT_REFRESH_SECRET=test-refresh-secret-for-ci" >> $GITHUB_ENV

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The client runs on `http://localhost:5173` and the server runs on `http://localh
- Server environment variables: `server/.env`
- Client environment variables: `client/.env` (if needed)
- The server expects a PostgreSQL `DATABASE_URL`, not a SQLite file path.
- Use a separate PostgreSQL database for `TEST_DATABASE_URL`; the test suite is destructive and refuses to run unless it is explicitly pointed at an isolated test database.

### Production Deployment
- **Backend (Render)**: Set the service root to `server`, then use `npm install && npm run deploy:build` for the build command and `npm start` for the start command.
Expand Down
31 changes: 31 additions & 0 deletions server/test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,38 @@ export const testUser = {
let testUserIdInternal: string;
let testAccessTokenInternal: string;

const assertIsolatedTestDatabase = () => {
const databaseUrl = process.env.DATABASE_URL;
const testDatabaseUrl = process.env.TEST_DATABASE_URL;

if (process.env.NODE_ENV !== "test" || !databaseUrl || !testDatabaseUrl) {
throw new Error(
"Refusing to run destructive tests without NODE_ENV=test, DATABASE_URL, and TEST_DATABASE_URL set.",
);
}

if (databaseUrl !== testDatabaseUrl) {
throw new Error(
"Refusing to run destructive tests unless DATABASE_URL matches TEST_DATABASE_URL.",
);
}

const parsedUrl = new URL(testDatabaseUrl);
const databaseName = decodeURIComponent(parsedUrl.pathname.replace(/^\//, ""));
const schemaName = parsedUrl.searchParams.get("schema") ?? "";
const isClearlyTestTarget =
databaseName.toLowerCase().includes("test") || schemaName.toLowerCase().includes("test");

if (!isClearlyTestTarget) {
throw new Error(
"Refusing to run destructive tests unless TEST_DATABASE_URL points to a database or schema containing 'test'.",
);
}
};

beforeAll(async () => {
assertIsolatedTestDatabase();

await prisma.aiUsageLog.deleteMany();
await prisma.lineItem.deleteMany();
await prisma.invoice.deleteMany();
Expand Down
Loading