Skip to content

Commit 4776f6c

Browse files
committed
split verify-data-integrity file
1 parent e848b6f commit 4776f6c

6 files changed

Lines changed: 647 additions & 554 deletions

File tree

apps/backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"codegen-docs:watch": "pnpm run with-env tsx watch --exclude '**/node_modules/**' --clear-screen=false scripts/generate-openapi-fumadocs.ts",
4242
"generate-keys": "pnpm run with-env tsx scripts/generate-keys.ts",
4343
"db-seed-script": "pnpm run db:seed",
44-
"verify-data-integrity": "pnpm run with-env:dev tsx scripts/verify-data-integrity.ts",
44+
"verify-data-integrity": "pnpm run with-env:dev tsx scripts/verify-data-integrity/index.ts",
4545
"run-email-queue": "pnpm run with-env:dev tsx scripts/run-email-queue.ts"
4646
},
4747
"prisma": {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
2+
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";
3+
import { deepPlainEquals, filterUndefined } from "@stackframe/stack-shared/dist/utils/objects";
4+
import { deindent } from "@stackframe/stack-shared/dist/utils/strings";
5+
6+
export type EndpointOutput = {
7+
status: number,
8+
responseJson: any,
9+
};
10+
11+
export type OutputData = Record<string, EndpointOutput[]>;
12+
13+
export type ExpectStatusCode = <T = any>(
14+
expectedStatusCode: number,
15+
endpoint: string,
16+
request: RequestInit,
17+
) => Promise<T>;
18+
19+
export function createApiHelpers(options: {
20+
currentOutputData: OutputData,
21+
targetOutputData?: OutputData,
22+
}) {
23+
const { currentOutputData, targetOutputData } = options;
24+
25+
function appendOutputData(endpoint: string, output: EndpointOutput) {
26+
if (!(endpoint in currentOutputData)) {
27+
currentOutputData[endpoint] = [];
28+
}
29+
const newLength = currentOutputData[endpoint].push(output);
30+
if (targetOutputData) {
31+
if (!(endpoint in targetOutputData)) {
32+
throw new StackAssertionError(deindent`
33+
Output data mismatch for endpoint ${endpoint}:
34+
Expected ${endpoint} to be in targetOutputData, but it is not.
35+
`, { endpoint });
36+
}
37+
if (targetOutputData[endpoint].length < newLength) {
38+
throw new StackAssertionError(deindent`
39+
Output data mismatch for endpoint ${endpoint}:
40+
Expected ${targetOutputData[endpoint].length} outputs but got at least ${newLength}.
41+
`, { endpoint });
42+
}
43+
if (!(deepPlainEquals(targetOutputData[endpoint][newLength - 1], output))) {
44+
throw new StackAssertionError(deindent`
45+
Output data mismatch for endpoint ${endpoint}:
46+
Expected output[${JSON.stringify(endpoint)}][${newLength - 1}] to be:
47+
${JSON.stringify(targetOutputData[endpoint][newLength - 1], null, 2)}
48+
but got:
49+
${JSON.stringify(output, null, 2)}.
50+
`, { endpoint });
51+
}
52+
}
53+
}
54+
55+
const expectStatusCode: ExpectStatusCode = async (expectedStatusCode, endpoint, request) => {
56+
const apiUrl = new URL(getEnvVariable("NEXT_PUBLIC_STACK_API_URL"));
57+
const response = await fetch(new URL(endpoint, apiUrl), {
58+
...request,
59+
headers: {
60+
"x-stack-disable-artificial-development-delay": "yes",
61+
"x-stack-development-disable-extended-logging": "yes",
62+
...filterUndefined(request.headers ?? {}),
63+
},
64+
});
65+
66+
const responseText = await response.text();
67+
68+
if (response.status !== expectedStatusCode) {
69+
throw new StackAssertionError(deindent`
70+
Expected status code ${expectedStatusCode} but got ${response.status} for ${endpoint}:
71+
72+
${responseText}
73+
`, { request, response });
74+
}
75+
76+
const responseJson = JSON.parse(responseText);
77+
const currentOutput: EndpointOutput = {
78+
status: response.status,
79+
responseJson,
80+
};
81+
82+
appendOutputData(endpoint, currentOutput);
83+
84+
return responseJson;
85+
};
86+
87+
return {
88+
appendOutputData,
89+
expectStatusCode,
90+
};
91+
}
92+

0 commit comments

Comments
 (0)