@astralarium/repo-config-vitest 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/base.d.ts +20 -0
- package/dist/base.js +57 -0
- package/dist/dom.d.ts +9 -0
- package/dist/dom.js +38 -0
- package/dist/fetch/app-fetch.d.ts +38 -0
- package/dist/fetch/app-fetch.js +83 -0
- package/dist/fetch/index.d.ts +5 -0
- package/dist/fetch/index.js +5 -0
- package/dist/fetch/requesters.d.ts +20 -0
- package/dist/fetch/requesters.js +26 -0
- package/dist/fetch/server-fn.d.ts +33 -0
- package/dist/fetch/server-fn.js +66 -0
- package/dist/global-setup-db.d.ts +26 -0
- package/dist/global-setup-db.js +29 -0
- package/dist/harness/app-fixture.d.ts +43 -0
- package/dist/harness/app-fixture.js +55 -0
- package/dist/harness/app-harness.d.ts +82 -0
- package/dist/harness/app-harness.js +143 -0
- package/dist/harness/app-process.d.ts +1 -0
- package/dist/harness/app-process.js +89 -0
- package/dist/harness/index.d.ts +3 -0
- package/dist/harness/index.js +3 -0
- package/dist/harness/worker-fixture.d.ts +14 -0
- package/dist/harness/worker-fixture.js +18 -0
- package/dist/harness/worker-process.d.ts +1 -0
- package/dist/harness/worker-process.js +35 -0
- package/dist/harness/workerd-connections.d.ts +9 -0
- package/dist/harness/workerd-connections.js +20 -0
- package/dist/pages/checks.d.ts +13 -0
- package/dist/pages/checks.js +64 -0
- package/dist/pages/classes.d.ts +53 -0
- package/dist/pages/classes.js +96 -0
- package/dist/pages/dehydrated.d.ts +50 -0
- package/dist/pages/dehydrated.js +108 -0
- package/dist/pages/head.d.ts +38 -0
- package/dist/pages/head.js +111 -0
- package/dist/pages/index.d.ts +9 -0
- package/dist/pages/index.js +9 -0
- package/dist/pages/runner.d.ts +29 -0
- package/dist/pages/runner.js +64 -0
- package/dist/pages/types.d.ts +39 -0
- package/dist/pages/types.js +1 -0
- package/dist/pages/viewer.d.ts +7 -0
- package/dist/pages/viewer.js +23 -0
- package/dist/react.d.ts +9 -0
- package/dist/react.js +23 -0
- package/dist/setup-react.d.ts +1 -0
- package/dist/setup-react.js +14 -0
- package/dist/start/client-compiler.d.ts +4 -0
- package/dist/start/client-compiler.js +16 -0
- package/dist/start/index.d.ts +3 -0
- package/dist/start/index.js +3 -0
- package/dist/start/library-build.d.ts +20 -0
- package/dist/start/library-build.js +26 -0
- package/dist/tls.d.ts +21 -0
- package/dist/tls.js +84 -0
- package/package.json +84 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Astralarium
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,51 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@astralarium/repo-config-vitest`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vitest presets and test harness.
|
|
4
|
+
|
|
5
|
+
| Key | Value | Description |
|
|
6
|
+
| ---------- | ---------------- | ------------------------------ |
|
|
7
|
+
| `name` | string | Vitest project name |
|
|
8
|
+
| `include` | globs | test files |
|
|
9
|
+
| `database` | `true` | test database |
|
|
10
|
+
| `dist` | `true` | production build; disables dev |
|
|
11
|
+
| `server` | `true` | app server; implies `database` |
|
|
12
|
+
| `setup` | `(context) => …` | additional app fixtures |
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
// test/app.ts
|
|
16
|
+
export const testApp = defineTestApp({
|
|
17
|
+
projects: [
|
|
18
|
+
{ name: "unit", include: ["src/**/*.test.ts"] },
|
|
19
|
+
{ name: "ssr", include: ["test/ssr.test.ts"], server: true, dist: true },
|
|
20
|
+
],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// test/global-setup.ts
|
|
24
|
+
export default function setup(root: TestProject) {
|
|
25
|
+
return setupTestApp(root, testApp);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// vitest.config.ts
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
test: {
|
|
31
|
+
globalSetup: ["./test/global-setup.ts"],
|
|
32
|
+
projects: testProjects(testApp),
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
testPages(pages, [
|
|
39
|
+
expectHead(
|
|
40
|
+
{ title: "About - App", property: { "og:url": "/about" } },
|
|
41
|
+
{ origin },
|
|
42
|
+
),
|
|
43
|
+
]);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
// ./start: client-bundle view of a Start module graph
|
|
48
|
+
const { code, modules, imports } = await libraryBuild(entry, {
|
|
49
|
+
plugins: startClientCompiler(),
|
|
50
|
+
});
|
|
51
|
+
```
|
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type TestProjectInlineConfiguration, type ViteUserConfig } from "vitest/config";
|
|
2
|
+
/** Package-dir config: Vitest defaults; its `vitest.config.ts` lists the
|
|
3
|
+
* package in `workspaceProjects`. */
|
|
4
|
+
declare const config: ViteUserConfig;
|
|
5
|
+
export default config;
|
|
6
|
+
/**
|
|
7
|
+
* Projects of a package with database tests: `name` runs every
|
|
8
|
+
* test file outside `test/`; `<name>-postgres` runs `test/**` over the
|
|
9
|
+
* database `test/global-setup.ts` provides (`setupTestDatabase`).
|
|
10
|
+
*/
|
|
11
|
+
export declare function databaseProjects(name: string): TestProjectInlineConfiguration[];
|
|
12
|
+
/**
|
|
13
|
+
* Root `test.projects`: workspace package dirs holding a `vitest.config.ts`,
|
|
14
|
+
* sorted. Vitest expands each config's own `test.projects` (e.g.
|
|
15
|
+
* `databaseProjects`) under its package.
|
|
16
|
+
*
|
|
17
|
+
* @param root Workspace dir the patterns resolve against.
|
|
18
|
+
* @param patterns Package dir globs; a `!` prefix excludes.
|
|
19
|
+
*/
|
|
20
|
+
export declare function workspaceProjects(root: string, patterns: string[]): Promise<string[]>;
|
package/dist/base.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { glob } from "node:fs/promises";
|
|
3
|
+
import { join, matchesGlob, resolve } from "node:path";
|
|
4
|
+
import { configDefaults, defineConfig, } from "vitest/config";
|
|
5
|
+
/** Package-dir config: Vitest defaults; its `vitest.config.ts` lists the
|
|
6
|
+
* package in `workspaceProjects`. */
|
|
7
|
+
const config = defineConfig({});
|
|
8
|
+
export default config;
|
|
9
|
+
/** Test files over a package's database. */
|
|
10
|
+
const DATABASE_TESTS = "test/**/*.test.ts";
|
|
11
|
+
/**
|
|
12
|
+
* Projects of a package with database tests: `name` runs every
|
|
13
|
+
* test file outside `test/`; `<name>-postgres` runs `test/**` over the
|
|
14
|
+
* database `test/global-setup.ts` provides (`setupTestDatabase`).
|
|
15
|
+
*/
|
|
16
|
+
export function databaseProjects(name) {
|
|
17
|
+
return [
|
|
18
|
+
{
|
|
19
|
+
test: {
|
|
20
|
+
name,
|
|
21
|
+
exclude: [...configDefaults.exclude, DATABASE_TESTS],
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
test: {
|
|
26
|
+
name: `${name}-postgres`,
|
|
27
|
+
include: [DATABASE_TESTS],
|
|
28
|
+
globalSetup: ["./test/global-setup.ts"],
|
|
29
|
+
testTimeout: 15_000,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
/** Config file a package dir holds. */
|
|
35
|
+
const CONFIG_FILE = "vitest.config.ts";
|
|
36
|
+
/**
|
|
37
|
+
* Root `test.projects`: workspace package dirs holding a `vitest.config.ts`,
|
|
38
|
+
* sorted. Vitest expands each config's own `test.projects` (e.g.
|
|
39
|
+
* `databaseProjects`) under its package.
|
|
40
|
+
*
|
|
41
|
+
* @param root Workspace dir the patterns resolve against.
|
|
42
|
+
* @param patterns Package dir globs; a `!` prefix excludes.
|
|
43
|
+
*/
|
|
44
|
+
export async function workspaceProjects(root, patterns) {
|
|
45
|
+
const include = patterns.filter((pattern) => !pattern.startsWith("!"));
|
|
46
|
+
const exclude = patterns
|
|
47
|
+
.filter((pattern) => pattern.startsWith("!"))
|
|
48
|
+
.map((pattern) => pattern.slice(1));
|
|
49
|
+
const dirs = [];
|
|
50
|
+
for await (const dir of glob(include, { cwd: root })) {
|
|
51
|
+
if (exclude.some((pattern) => matchesGlob(dir, pattern)))
|
|
52
|
+
continue;
|
|
53
|
+
if (existsSync(join(root, dir, CONFIG_FILE)))
|
|
54
|
+
dirs.push(resolve(root, dir));
|
|
55
|
+
}
|
|
56
|
+
return dirs.sort();
|
|
57
|
+
}
|
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Stubs the global `matchMedia`: the queries in `matching` match. Returns
|
|
2
|
+
* a switch for one query's match and a count of its listeners;
|
|
3
|
+
* `vi.unstubAllGlobals` restores the original. */
|
|
4
|
+
export declare function stubMatchMedia(...matching: string[]): {
|
|
5
|
+
/** Sets whether `media` matches and notifies its listeners. */
|
|
6
|
+
setMatches(media: string, matches: boolean): void;
|
|
7
|
+
/** `media`'s subscribed listeners. */
|
|
8
|
+
listenerCount: (media: string) => number;
|
|
9
|
+
};
|
package/dist/dom.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Browser API stubs for jsdom tests. */
|
|
2
|
+
import { vi } from "vitest";
|
|
3
|
+
/** Stubs the global `matchMedia`: the queries in `matching` match. Returns
|
|
4
|
+
* a switch for one query's match and a count of its listeners;
|
|
5
|
+
* `vi.unstubAllGlobals` restores the original. */
|
|
6
|
+
export function stubMatchMedia(...matching) {
|
|
7
|
+
const matched = new Set(matching);
|
|
8
|
+
const listeners = new Map();
|
|
9
|
+
const listenersOf = (media) => {
|
|
10
|
+
let set = listeners.get(media);
|
|
11
|
+
if (!set) {
|
|
12
|
+
set = new Set();
|
|
13
|
+
listeners.set(media, set);
|
|
14
|
+
}
|
|
15
|
+
return set;
|
|
16
|
+
};
|
|
17
|
+
vi.stubGlobal("matchMedia", (media) => ({
|
|
18
|
+
media,
|
|
19
|
+
get matches() {
|
|
20
|
+
return matched.has(media);
|
|
21
|
+
},
|
|
22
|
+
addEventListener: (_type, listener) => listenersOf(media).add(listener),
|
|
23
|
+
removeEventListener: (_type, listener) => listenersOf(media).delete(listener),
|
|
24
|
+
}));
|
|
25
|
+
return {
|
|
26
|
+
/** Sets whether `media` matches and notifies its listeners. */
|
|
27
|
+
setMatches(media, matches) {
|
|
28
|
+
if (matches)
|
|
29
|
+
matched.add(media);
|
|
30
|
+
else
|
|
31
|
+
matched.delete(media);
|
|
32
|
+
for (const listener of listenersOf(media))
|
|
33
|
+
listener();
|
|
34
|
+
},
|
|
35
|
+
/** `media`'s subscribed listeners. */
|
|
36
|
+
listenerCount: (media) => listenersOf(media).size,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type RequestInit } from "undici";
|
|
2
|
+
/**
|
|
3
|
+
* Client address per test file: per-IP rate limits see files running in
|
|
4
|
+
* parallel against one server as distinct clients.
|
|
5
|
+
*/
|
|
6
|
+
export declare const CLIENT_IP: string;
|
|
7
|
+
/**
|
|
8
|
+
* `fetch` against the test server under any public origin: the URL keeps
|
|
9
|
+
* the public host (so `Host`, cookies and origin checks see it) while every
|
|
10
|
+
* connection goes to `serverOrigin`, whose fixture certificate is trusted
|
|
11
|
+
* through the connector's `ca`. Redirects return as responses.
|
|
12
|
+
*/
|
|
13
|
+
export declare function appRequester(serverOrigin: string, publicOrigin: string): ((path: string, init?: RequestInit) => Promise<import("undici").Response>) & {
|
|
14
|
+
close: () => Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
/** An `appRequester` request function; `close` ends its connections. */
|
|
17
|
+
export type AppRequester = ReturnType<typeof appRequester>;
|
|
18
|
+
/** An `http2Requester` request. */
|
|
19
|
+
export interface Http2Init {
|
|
20
|
+
/** Default `GET`. */
|
|
21
|
+
method?: string;
|
|
22
|
+
/** Lowercase names; these override the defaults. */
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
body?: string;
|
|
25
|
+
}
|
|
26
|
+
/** An `http2Requester` response, read in full. */
|
|
27
|
+
export interface Http2Response {
|
|
28
|
+
status: number;
|
|
29
|
+
text(): Promise<string>;
|
|
30
|
+
json(): Promise<unknown>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* `fetch`-like HTTP/2 requests against the test server under
|
|
34
|
+
* `publicOrigin`: `:authority` carries the public host (no `Host` header),
|
|
35
|
+
* `origin` the public origin, and the connection goes to `serverOrigin`.
|
|
36
|
+
* One connection per request.
|
|
37
|
+
*/
|
|
38
|
+
export declare function http2Requester(serverOrigin: string, publicOrigin: string): (path: string, init?: Http2Init) => Promise<Http2Response>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { connect as connectHttp2 } from "node:http2";
|
|
2
|
+
import { text } from "node:stream/consumers";
|
|
3
|
+
import { Agent, buildConnector, fetch, Headers, } from "undici";
|
|
4
|
+
import { testTls } from "../tls.js";
|
|
5
|
+
/**
|
|
6
|
+
* Client address per test file: per-IP rate limits see files running in
|
|
7
|
+
* parallel against one server as distinct clients.
|
|
8
|
+
*/
|
|
9
|
+
export const CLIENT_IP = `10.${Array.from({ length: 3 }, () => Math.floor(Math.random() * 256)).join(".")}`;
|
|
10
|
+
/**
|
|
11
|
+
* `fetch` against the test server under any public origin: the URL keeps
|
|
12
|
+
* the public host (so `Host`, cookies and origin checks see it) while every
|
|
13
|
+
* connection goes to `serverOrigin`, whose fixture certificate is trusted
|
|
14
|
+
* through the connector's `ca`. Redirects return as responses.
|
|
15
|
+
*/
|
|
16
|
+
export function appRequester(serverOrigin, publicOrigin) {
|
|
17
|
+
const server = new URL(serverOrigin);
|
|
18
|
+
const connect = buildConnector({
|
|
19
|
+
servername: server.hostname,
|
|
20
|
+
ca: testTls().cert,
|
|
21
|
+
});
|
|
22
|
+
const agent = new Agent({
|
|
23
|
+
connections: 16,
|
|
24
|
+
connect: (options, callback) => connect({
|
|
25
|
+
...options,
|
|
26
|
+
hostname: server.hostname,
|
|
27
|
+
host: server.hostname,
|
|
28
|
+
port: server.port,
|
|
29
|
+
servername: server.hostname,
|
|
30
|
+
}, callback),
|
|
31
|
+
});
|
|
32
|
+
const request = (path, init = {}) => {
|
|
33
|
+
const headers = new Headers(init.headers);
|
|
34
|
+
if (!headers.has("cf-connecting-ip"))
|
|
35
|
+
headers.set("cf-connecting-ip", CLIENT_IP);
|
|
36
|
+
return fetch(new URL(path, publicOrigin), {
|
|
37
|
+
...init,
|
|
38
|
+
headers,
|
|
39
|
+
dispatcher: agent,
|
|
40
|
+
redirect: "manual",
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
return Object.assign(request, { close: () => agent.close() });
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `fetch`-like HTTP/2 requests against the test server under
|
|
47
|
+
* `publicOrigin`: `:authority` carries the public host (no `Host` header),
|
|
48
|
+
* `origin` the public origin, and the connection goes to `serverOrigin`.
|
|
49
|
+
* One connection per request.
|
|
50
|
+
*/
|
|
51
|
+
export function http2Requester(serverOrigin, publicOrigin) {
|
|
52
|
+
const authority = new URL(publicOrigin).host;
|
|
53
|
+
const ca = testTls().cert;
|
|
54
|
+
return async (path, init = {}) => {
|
|
55
|
+
const client = connectHttp2(serverOrigin, { ca });
|
|
56
|
+
try {
|
|
57
|
+
const { status, body } = await new Promise((resolve, reject) => {
|
|
58
|
+
client.on("error", reject);
|
|
59
|
+
const stream = client.request({
|
|
60
|
+
origin: publicOrigin,
|
|
61
|
+
"cf-connecting-ip": CLIENT_IP,
|
|
62
|
+
...init.headers,
|
|
63
|
+
":authority": authority,
|
|
64
|
+
":path": path,
|
|
65
|
+
":method": init.method ?? "GET",
|
|
66
|
+
});
|
|
67
|
+
stream.on("error", reject);
|
|
68
|
+
stream.on("response", (headers) => {
|
|
69
|
+
text(stream).then((body) => resolve({ status: Number(headers[":status"]), body }), reject);
|
|
70
|
+
});
|
|
71
|
+
stream.end(init.body);
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
status,
|
|
75
|
+
text: () => Promise.resolve(body),
|
|
76
|
+
json: () => Promise.resolve(JSON.parse(body)),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
client.destroy();
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type AppRequester, http2Requester } from "./app-fetch.ts";
|
|
2
|
+
/** Requesters addressing the test server under one public origin. */
|
|
3
|
+
export interface LocalOriginRequesters {
|
|
4
|
+
/** Public origin the requests carry. */
|
|
5
|
+
origin: string;
|
|
6
|
+
/** One requester per protocol. */
|
|
7
|
+
requesters: {
|
|
8
|
+
"HTTP/1.1": AppRequester;
|
|
9
|
+
"HTTP/2": ReturnType<typeof http2Requester>;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/** Requester against the app server under its public origin; the file's
|
|
13
|
+
* `afterAll` closes it. */
|
|
14
|
+
export declare function serverRequester(serverOrigin?: string, publicOrigin?: string): AppRequester;
|
|
15
|
+
/**
|
|
16
|
+
* HTTP/1.1 and HTTP/2 requesters against `serverOrigin` under each of
|
|
17
|
+
* `origins` (a dev server project's app server and local origins by default);
|
|
18
|
+
* the file's `afterAll` closes the HTTP/1.1 agents.
|
|
19
|
+
*/
|
|
20
|
+
export declare function localOriginRequesters(serverOrigin?: string, origins?: readonly string[]): LocalOriginRequesters[];
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Requesters scoped to a test file: its `afterAll` closes them. */
|
|
2
|
+
import { afterAll, inject } from "vitest";
|
|
3
|
+
import { appRequester, http2Requester, } from "./app-fetch.js";
|
|
4
|
+
/** Requester against the app server under its public origin; the file's
|
|
5
|
+
* `afterAll` closes it. */
|
|
6
|
+
export function serverRequester(serverOrigin = inject("ssrOrigin"), publicOrigin = inject("siteOrigin")) {
|
|
7
|
+
const request = appRequester(serverOrigin, publicOrigin);
|
|
8
|
+
afterAll(request.close);
|
|
9
|
+
return request;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* HTTP/1.1 and HTTP/2 requesters against `serverOrigin` under each of
|
|
13
|
+
* `origins` (a dev server project's app server and local origins by default);
|
|
14
|
+
* the file's `afterAll` closes the HTTP/1.1 agents.
|
|
15
|
+
*/
|
|
16
|
+
export function localOriginRequesters(serverOrigin = inject("ssrOrigin"), origins = inject("localOrigins")) {
|
|
17
|
+
const entries = origins.map((origin) => ({
|
|
18
|
+
origin,
|
|
19
|
+
requesters: {
|
|
20
|
+
"HTTP/1.1": appRequester(serverOrigin, origin),
|
|
21
|
+
"HTTP/2": http2Requester(serverOrigin, origin),
|
|
22
|
+
},
|
|
23
|
+
}));
|
|
24
|
+
afterAll(() => Promise.all(entries.map(({ requesters }) => requesters["HTTP/1.1"].close())));
|
|
25
|
+
return entries;
|
|
26
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { RequestInit, Response } from "undici";
|
|
2
|
+
/** Path of the server function `name` exported by `file` (relative to the
|
|
3
|
+
* app root) under `base`. Builds name each function by a hash; dev
|
|
4
|
+
* servers by its module and export. */
|
|
5
|
+
export declare function serverFnPath(file: string, name: string, mode?: "build" | "dev", base?: string): string;
|
|
6
|
+
/** Serialized call payload carrying `data`. */
|
|
7
|
+
export declare function serverFnPayload(data: unknown): string;
|
|
8
|
+
/** A server function response's outcome. */
|
|
9
|
+
export interface ServerFnOutcome {
|
|
10
|
+
result?: unknown;
|
|
11
|
+
error?: unknown;
|
|
12
|
+
}
|
|
13
|
+
/** Decodes a serialized server function response. */
|
|
14
|
+
export declare function serverFnOutcome(response: Response): Promise<ServerFnOutcome>;
|
|
15
|
+
/** `fetch` of the app under test by path (`appRequester`). */
|
|
16
|
+
type Requester = (path: string, init?: RequestInit) => Promise<Response>;
|
|
17
|
+
/** How `serverFnCallers` names functions, as `serverFnPath` does. */
|
|
18
|
+
interface ServerFnNaming {
|
|
19
|
+
mode?: "build" | "dev";
|
|
20
|
+
/** The app's `serverFns.base`. */
|
|
21
|
+
base?: string;
|
|
22
|
+
}
|
|
23
|
+
/** Callers of the app's server functions as a session: `call` `POST`s,
|
|
24
|
+
* `query` `GET`s. Each expects success and returns the result. */
|
|
25
|
+
export declare function serverFnCallers(request: Requester, origin: string, { mode, base }?: ServerFnNaming): {
|
|
26
|
+
call: (file: string, name: string, data: unknown, session: {
|
|
27
|
+
cookie: string;
|
|
28
|
+
}) => Promise<unknown>;
|
|
29
|
+
query: (file: string, name: string, data: unknown, session: {
|
|
30
|
+
cookie: string;
|
|
31
|
+
}) => Promise<unknown>;
|
|
32
|
+
};
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** Raw HTTP calls to a Start app's server functions. */
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { fromCrossJSON, toJSON } from "seroval";
|
|
4
|
+
import { expect } from "vitest";
|
|
5
|
+
/** Path prefix of server-function calls: Start's default `serverFns.base`. */
|
|
6
|
+
const SERVER_FN_BASE = "/_serverFn";
|
|
7
|
+
/** Path of the server function `name` exported by `file` (relative to the
|
|
8
|
+
* app root) under `base`. Builds name each function by a hash; dev
|
|
9
|
+
* servers by its module and export. */
|
|
10
|
+
export function serverFnPath(file, name, mode = "build", base = SERVER_FN_BASE) {
|
|
11
|
+
const handler = `${name}_createServerFn_handler`;
|
|
12
|
+
const id = mode === "build"
|
|
13
|
+
? createHash("sha256").update(`${file}--${handler}`).digest("hex")
|
|
14
|
+
: Buffer.from(JSON.stringify({
|
|
15
|
+
file: `/${file}?tss-serverfn-split`,
|
|
16
|
+
export: handler,
|
|
17
|
+
})).toString("base64url");
|
|
18
|
+
return `${base}/${id}`;
|
|
19
|
+
}
|
|
20
|
+
/** Serialized call payload carrying `data`. */
|
|
21
|
+
export function serverFnPayload(data) {
|
|
22
|
+
return JSON.stringify(toJSON({ data }));
|
|
23
|
+
}
|
|
24
|
+
/** Decodes a serialized server function response. */
|
|
25
|
+
export async function serverFnOutcome(response) {
|
|
26
|
+
return fromCrossJSON((await response.json()), { refs: new Map() });
|
|
27
|
+
}
|
|
28
|
+
/** A successful response's result; fails on a non-200 status or an error
|
|
29
|
+
* outcome. */
|
|
30
|
+
async function resultOf(response) {
|
|
31
|
+
expect(response.status).toBe(200);
|
|
32
|
+
const { result, error } = await serverFnOutcome(response);
|
|
33
|
+
expect(error).toBeUndefined();
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
/** Callers of the app's server functions as a session: `call` `POST`s,
|
|
37
|
+
* `query` `GET`s. Each expects success and returns the result. */
|
|
38
|
+
export function serverFnCallers(request, origin, { mode = "build", base = SERVER_FN_BASE } = {}) {
|
|
39
|
+
/** `POST`s `data` to the server function as `session`. */
|
|
40
|
+
async function call(file, name, data, session) {
|
|
41
|
+
const response = await request(serverFnPath(file, name, mode, base), {
|
|
42
|
+
method: "POST",
|
|
43
|
+
headers: {
|
|
44
|
+
"x-tsr-serverFn": "true",
|
|
45
|
+
origin,
|
|
46
|
+
"content-type": "application/json",
|
|
47
|
+
cookie: session.cookie,
|
|
48
|
+
},
|
|
49
|
+
body: serverFnPayload(data),
|
|
50
|
+
});
|
|
51
|
+
return resultOf(response);
|
|
52
|
+
}
|
|
53
|
+
/** `GET`s the server function with `data` as `session`. */
|
|
54
|
+
async function query(file, name, data, session) {
|
|
55
|
+
const payload = encodeURIComponent(serverFnPayload(data));
|
|
56
|
+
const response = await request(`${serverFnPath(file, name, mode, base)}?payload=${payload}`, {
|
|
57
|
+
headers: {
|
|
58
|
+
"x-tsr-serverFn": "true",
|
|
59
|
+
"sec-fetch-site": "same-origin",
|
|
60
|
+
cookie: session.cookie,
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
return resultOf(response);
|
|
64
|
+
}
|
|
65
|
+
return { call, query };
|
|
66
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type DatabaseProvider, type ResourceScope, type TestContainer, type TestDatabase } from "@astralarium/repo-dev";
|
|
2
|
+
import type { TestProject } from "vitest/node";
|
|
3
|
+
declare module "vitest" {
|
|
4
|
+
interface ProvidedContext {
|
|
5
|
+
/** Project database as the app connects to it. */
|
|
6
|
+
dbAppConnectionString: string;
|
|
7
|
+
/** Project database as the database owner. */
|
|
8
|
+
dbAdminConnectionString: string;
|
|
9
|
+
/** Container running the instance; provided when it runs in one. */
|
|
10
|
+
dbContainer?: TestContainer;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** What a test run needs of a `DatabaseProvider`: its disposable
|
|
14
|
+
* instance. */
|
|
15
|
+
export type TestDatabaseSource<Instance extends TestDatabase = TestDatabase> = Pick<DatabaseProvider<Instance>, "startTestInstance">;
|
|
16
|
+
/** Starts the run's shared instance of `database`, named after `root`'s
|
|
17
|
+
* directory. */
|
|
18
|
+
export declare function startTestDatabase<Instance extends TestDatabase>(resources: ResourceScope, root: TestProject, database: TestDatabaseSource<Instance>): Promise<Instance>;
|
|
19
|
+
/** Creates one project's private database and provides
|
|
20
|
+
* `dbAppConnectionString`, `dbAdminConnectionString` and, for a
|
|
21
|
+
* containerized instance, `dbContainer`. */
|
|
22
|
+
export declare function provideTestDatabase(project: TestProject, instance: TestDatabase): Promise<string>;
|
|
23
|
+
/** Vitest global setup for a package over a database alone: starts the
|
|
24
|
+
* run's instance, creates `project`'s database and provides its
|
|
25
|
+
* connection strings. */
|
|
26
|
+
export declare function setupTestDatabase(project: TestProject, database: TestDatabaseSource): Promise<() => Promise<void>>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Database fixtures for a Vitest run: the shared instance and each
|
|
2
|
+
* project's private database. */
|
|
3
|
+
import { basename } from "node:path";
|
|
4
|
+
import { setupResources, } from "@astralarium/repo-dev";
|
|
5
|
+
/** Starts the run's shared instance of `database`, named after `root`'s
|
|
6
|
+
* directory. */
|
|
7
|
+
export function startTestDatabase(resources, root, database) {
|
|
8
|
+
return database.startTestInstance(resources, basename(root.config.root));
|
|
9
|
+
}
|
|
10
|
+
/** Creates one project's private database and provides
|
|
11
|
+
* `dbAppConnectionString`, `dbAdminConnectionString` and, for a
|
|
12
|
+
* containerized instance, `dbContainer`. */
|
|
13
|
+
export async function provideTestDatabase(project, instance) {
|
|
14
|
+
const connectionString = await instance.createDatabase(project.name);
|
|
15
|
+
project.provide("dbAppConnectionString", connectionString);
|
|
16
|
+
project.provide("dbAdminConnectionString", instance.adminConnectionString(project.name));
|
|
17
|
+
if (instance.container)
|
|
18
|
+
project.provide("dbContainer", instance.container);
|
|
19
|
+
return connectionString;
|
|
20
|
+
}
|
|
21
|
+
/** Vitest global setup for a package over a database alone: starts the
|
|
22
|
+
* run's instance, creates `project`'s database and provides its
|
|
23
|
+
* connection strings. */
|
|
24
|
+
export function setupTestDatabase(project, database) {
|
|
25
|
+
return setupResources(async (resources) => {
|
|
26
|
+
const instance = await startTestDatabase(resources, project, database);
|
|
27
|
+
await provideTestDatabase(project, instance);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type ResourceScope } from "@astralarium/repo-dev";
|
|
2
|
+
import type { TestDatabaseSource } from "../global-setup-db.ts";
|
|
3
|
+
/** Default `AppFixture.configFile`. */
|
|
4
|
+
export declare const VITE_CONFIG = "vite.config.ts";
|
|
5
|
+
/** Modes `app-process` runs an app in: production build, dev server,
|
|
6
|
+
* preview. */
|
|
7
|
+
export declare const APP_MODES: readonly ["build", "dev", "production"];
|
|
8
|
+
/** One of `APP_MODES`. */
|
|
9
|
+
export type AppMode = (typeof APP_MODES)[number];
|
|
10
|
+
/** Connection string a server gets: the project database; undefined for
|
|
11
|
+
* an app without one. */
|
|
12
|
+
export type AppDatabaseUrl<Database> = Database extends TestDatabaseSource ? string : undefined;
|
|
13
|
+
/** An app's build and servers, as the harness launches them. */
|
|
14
|
+
export interface AppFixture<Database extends TestDatabaseSource | undefined = TestDatabaseSource | undefined> {
|
|
15
|
+
/** App directory. */
|
|
16
|
+
root: string;
|
|
17
|
+
/** Vite config relative to `root`; `vite.config.ts` by default. */
|
|
18
|
+
configFile?: string;
|
|
19
|
+
/** Client output of the production build relative to `root`;
|
|
20
|
+
* `dist/client` by default. */
|
|
21
|
+
distDir?: string;
|
|
22
|
+
/** Public HTTPS origin fixtures and tests address the app under. */
|
|
23
|
+
siteOrigin: string;
|
|
24
|
+
/** Paths a started server answers 2xx before it counts as ready. */
|
|
25
|
+
probes: readonly string[];
|
|
26
|
+
/** Wrangler environment of the build and servers. */
|
|
27
|
+
cloudflareEnv: string;
|
|
28
|
+
/**
|
|
29
|
+
* Process environment of a server over the project database.
|
|
30
|
+
*
|
|
31
|
+
* @param localOrigins Origins the dev server answers: `siteOrigin`
|
|
32
|
+
* plus `TestApp.localOrigins`; empty for production.
|
|
33
|
+
*/
|
|
34
|
+
environment(mode: "dev" | "production", databaseUrl: AppDatabaseUrl<Database>, localOrigins: readonly string[]): Record<string, string>;
|
|
35
|
+
}
|
|
36
|
+
/** Builds the app once for the run; resolves to its client output
|
|
37
|
+
* directory. */
|
|
38
|
+
export declare function buildApp(app: AppFixture, resources: ResourceScope): Promise<string>;
|
|
39
|
+
/** Starts a server over `databaseUrl` answering `localOrigins`
|
|
40
|
+
* (`siteOrigin` plus `TestApp.localOrigins` for dev; empty for
|
|
41
|
+
* production) and re-probes it from this process; resolves to its
|
|
42
|
+
* loopback origin. */
|
|
43
|
+
export declare function startApp(app: AppFixture, resources: ResourceScope, mode: "dev" | "production", databaseUrl: string | undefined, localOrigins: readonly string[]): Promise<string>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Web app processes a test run owns: the production build, dev and
|
|
2
|
+
* preview servers. */
|
|
3
|
+
import { extname, resolve } from "node:path";
|
|
4
|
+
import { appProcessEnv, forkProcess, } from "@astralarium/repo-dev";
|
|
5
|
+
import { appRequester } from "../fetch/app-fetch.js";
|
|
6
|
+
import { testTlsEnv } from "../tls.js";
|
|
7
|
+
/** Default `AppFixture.configFile`. */
|
|
8
|
+
export const VITE_CONFIG = "vite.config.ts";
|
|
9
|
+
/** Default `AppFixture.distDir`: client output of the Cloudflare Vite
|
|
10
|
+
* plugin. */
|
|
11
|
+
const CLIENT_DIST = "dist/client";
|
|
12
|
+
/** `app-process` beside this module, in this module's extension. */
|
|
13
|
+
const APP_PROCESS = new URL(`./app-process${extname(import.meta.url)}`, import.meta.url);
|
|
14
|
+
/** Modes `app-process` runs an app in: production build, dev server,
|
|
15
|
+
* preview. */
|
|
16
|
+
export const APP_MODES = ["build", "dev", "production"];
|
|
17
|
+
/** Forks `app-process` in `mode` under the launcher's process
|
|
18
|
+
* environment. */
|
|
19
|
+
function launchApp(app, resources, mode, env) {
|
|
20
|
+
return resources.own(forkProcess(APP_PROCESS, [
|
|
21
|
+
mode,
|
|
22
|
+
app.root,
|
|
23
|
+
app.configFile ?? VITE_CONFIG,
|
|
24
|
+
app.siteOrigin,
|
|
25
|
+
...app.probes,
|
|
26
|
+
], {
|
|
27
|
+
cwd: app.root,
|
|
28
|
+
env: appProcessEnv({ ...env, ...testTlsEnv() }, { mode, cloudflareEnv: app.cloudflareEnv }),
|
|
29
|
+
}, resources.signal));
|
|
30
|
+
}
|
|
31
|
+
/** Builds the app once for the run; resolves to its client output
|
|
32
|
+
* directory. */
|
|
33
|
+
export async function buildApp(app, resources) {
|
|
34
|
+
await launchApp(app, resources, "build", {}).exited;
|
|
35
|
+
return resolve(app.root, app.distDir ?? CLIENT_DIST);
|
|
36
|
+
}
|
|
37
|
+
/** Starts a server over `databaseUrl` answering `localOrigins`
|
|
38
|
+
* (`siteOrigin` plus `TestApp.localOrigins` for dev; empty for
|
|
39
|
+
* production) and re-probes it from this process; resolves to its
|
|
40
|
+
* loopback origin. */
|
|
41
|
+
export async function startApp(app, resources, mode, databaseUrl, localOrigins) {
|
|
42
|
+
const child = launchApp(app, resources, mode, app.environment(mode, databaseUrl, localOrigins));
|
|
43
|
+
const origin = await child.ready;
|
|
44
|
+
if (typeof origin !== "string")
|
|
45
|
+
throw new Error("Test app sent an invalid origin");
|
|
46
|
+
const request = appRequester(origin, app.siteOrigin);
|
|
47
|
+
resources.own({ dispose: request.close });
|
|
48
|
+
await Promise.all(app.probes.map(async (path) => {
|
|
49
|
+
const response = await request(path, { signal: resources.signal });
|
|
50
|
+
await response.body?.cancel();
|
|
51
|
+
if (!response.ok)
|
|
52
|
+
throw new Error(`${path}: HTTP ${response.status}`);
|
|
53
|
+
}));
|
|
54
|
+
return origin;
|
|
55
|
+
}
|