@artemis-apps/test-kit 0.0.0-stage → 0.1.1
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/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/test-browser.d.ts +19 -0
- package/dist/test-browser.js +65 -0
- package/dist/test-database.d.ts +8 -0
- package/dist/test-database.js +17 -0
- package/dist/test-images.d.ts +11 -0
- package/dist/test-images.js +13 -0
- package/dist/test-server.d.ts +12 -0
- package/dist/test-server.js +16 -0
- package/package.json +46 -3
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface VisitedPage {
|
|
2
|
+
response: Response;
|
|
3
|
+
url: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* A scriptless browser for HTTP-level tests: a real cookie jar (tough-cookie),
|
|
7
|
+
* cross-origin redirects, and submission of auto-submitting HTML forms.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createTestBrowser(): {
|
|
10
|
+
request: (url: string, init?: RequestInit) => Promise<Response>;
|
|
11
|
+
navigate: (url: string, init?: RequestInit) => Promise<VisitedPage>;
|
|
12
|
+
sendJson(url: string, body: unknown, method?: string): Promise<Response>;
|
|
13
|
+
/**
|
|
14
|
+
* Does what a browser does with a page that submits its first form by script:
|
|
15
|
+
* posts the form's fields (plus `extraFields`) and follows the redirects.
|
|
16
|
+
*/
|
|
17
|
+
submitFirstForm(page: VisitedPage, extraFields?: Record<string, string>): Promise<VisitedPage>;
|
|
18
|
+
};
|
|
19
|
+
export type TestBrowser = ReturnType<typeof createTestBrowser>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { parse as parseHtml } from 'node-html-parser';
|
|
2
|
+
import { CookieJar } from 'tough-cookie';
|
|
3
|
+
const MAX_REDIRECTS = 15;
|
|
4
|
+
/**
|
|
5
|
+
* A scriptless browser for HTTP-level tests: a real cookie jar (tough-cookie),
|
|
6
|
+
* cross-origin redirects, and submission of auto-submitting HTML forms.
|
|
7
|
+
*/
|
|
8
|
+
export function createTestBrowser() {
|
|
9
|
+
const cookies = new CookieJar();
|
|
10
|
+
async function request(url, init = {}) {
|
|
11
|
+
const headers = new Headers(init.headers);
|
|
12
|
+
headers.set('origin', new URL(url).origin);
|
|
13
|
+
const cookie = await cookies.getCookieString(url);
|
|
14
|
+
if (cookie)
|
|
15
|
+
headers.set('cookie', cookie);
|
|
16
|
+
const response = await fetch(url, { ...init, headers, redirect: 'manual' });
|
|
17
|
+
for (const setCookie of response.headers.getSetCookie()) {
|
|
18
|
+
await cookies.setCookie(setCookie, url, { ignoreError: true });
|
|
19
|
+
}
|
|
20
|
+
return response;
|
|
21
|
+
}
|
|
22
|
+
/** Follows redirects across origins, returning the final response and URL. */
|
|
23
|
+
async function navigate(url, init) {
|
|
24
|
+
let currentUrl = url;
|
|
25
|
+
let response = await request(currentUrl, init);
|
|
26
|
+
for (let hops = 0; hops < MAX_REDIRECTS; hops += 1) {
|
|
27
|
+
const location = response.headers.get('location');
|
|
28
|
+
if (response.status < 300 || response.status >= 400 || !location) {
|
|
29
|
+
return { response, url: currentUrl };
|
|
30
|
+
}
|
|
31
|
+
currentUrl = new URL(location, currentUrl).toString();
|
|
32
|
+
response = await request(currentUrl);
|
|
33
|
+
}
|
|
34
|
+
throw new Error(`Too many redirects from ${url}.`);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
request,
|
|
38
|
+
navigate,
|
|
39
|
+
sendJson(url, body, method = 'POST') {
|
|
40
|
+
return request(url, {
|
|
41
|
+
method,
|
|
42
|
+
headers: { 'content-type': 'application/json' },
|
|
43
|
+
body: JSON.stringify(body),
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
/**
|
|
47
|
+
* Does what a browser does with a page that submits its first form by script:
|
|
48
|
+
* posts the form's fields (plus `extraFields`) and follows the redirects.
|
|
49
|
+
*/
|
|
50
|
+
async submitFirstForm(page, extraFields = {}) {
|
|
51
|
+
const form = parseHtml(await page.response.text()).querySelector('form');
|
|
52
|
+
if (!form)
|
|
53
|
+
throw new Error(`No form on ${page.url}.`);
|
|
54
|
+
const fields = new URLSearchParams(extraFields);
|
|
55
|
+
for (const input of form.querySelectorAll('input[name]')) {
|
|
56
|
+
fields.set(input.getAttribute('name') ?? '', input.getAttribute('value') ?? '');
|
|
57
|
+
}
|
|
58
|
+
return navigate(new URL(form.getAttribute('action') ?? page.url, page.url).toString(), {
|
|
59
|
+
method: form.getAttribute('method')?.toUpperCase() ?? 'GET',
|
|
60
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
61
|
+
body: fields,
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import SQLite from 'better-sqlite3';
|
|
2
|
+
/**
|
|
3
|
+
* In-memory database holding `tables`, created by drizzle-kit exactly like an app migration.
|
|
4
|
+
* For packages that contribute tables but own no migrations folder.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createTestDatabase<Tables extends Record<string, unknown>>(tables: Tables): Promise<import("drizzle-orm/better-sqlite3").BetterSQLite3Database<Tables> & {
|
|
7
|
+
$client: SQLite.Database;
|
|
8
|
+
}>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import SQLite from 'better-sqlite3';
|
|
2
|
+
import { generateSQLiteDrizzleJson, generateSQLiteMigration } from 'drizzle-kit/api';
|
|
3
|
+
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
|
4
|
+
/**
|
|
5
|
+
* In-memory database holding `tables`, created by drizzle-kit exactly like an app migration.
|
|
6
|
+
* For packages that contribute tables but own no migrations folder.
|
|
7
|
+
*/
|
|
8
|
+
export async function createTestDatabase(tables) {
|
|
9
|
+
const [empty, current] = await Promise.all([
|
|
10
|
+
generateSQLiteDrizzleJson({}, undefined, 'snake_case'),
|
|
11
|
+
generateSQLiteDrizzleJson(tables, undefined, 'snake_case'),
|
|
12
|
+
]);
|
|
13
|
+
const client = new SQLite(':memory:');
|
|
14
|
+
for (const statement of await generateSQLiteMigration(empty, current))
|
|
15
|
+
client.exec(statement);
|
|
16
|
+
return drizzle({ client, schema: tables, casing: 'snake_case' });
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface TestImageOptions {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
format?: 'png' | 'jpeg' | 'webp';
|
|
5
|
+
/** EXIF orientation written into the file (JPEG). */
|
|
6
|
+
orientation?: number;
|
|
7
|
+
}
|
|
8
|
+
/** A plain image, generated on the fly. */
|
|
9
|
+
export declare function createTestImage({ width, height, format, orientation, }: TestImageOptions): Promise<File>;
|
|
10
|
+
/** What sharp reads back from a picture, or from a response serving one. */
|
|
11
|
+
export declare function inspectImage(source: Blob | Response): Promise<import("sharp").Metadata>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import sharp from 'sharp';
|
|
2
|
+
/** A plain image, generated on the fly. */
|
|
3
|
+
export async function createTestImage({ width, height, format = 'png', orientation, }) {
|
|
4
|
+
let image = sharp({ create: { width, height, channels: 3, background: '#6f9cf2' } });
|
|
5
|
+
if (orientation)
|
|
6
|
+
image = image.withMetadata({ orientation });
|
|
7
|
+
const bytes = await image.toFormat(format).toBuffer();
|
|
8
|
+
return new File([new Uint8Array(bytes)], `picture.${format}`, { type: `image/${format}` });
|
|
9
|
+
}
|
|
10
|
+
/** What sharp reads back from a picture, or from a response serving one. */
|
|
11
|
+
export async function inspectImage(source) {
|
|
12
|
+
return sharp(Buffer.from(await source.arrayBuffer())).metadata();
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { serve } from '@hono/node-server';
|
|
2
|
+
type FetchHandler = Parameters<typeof serve>[0]['fetch'];
|
|
3
|
+
export interface TestServer {
|
|
4
|
+
url: string;
|
|
5
|
+
close(): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Listens on an ephemeral port. The handler is built once the URL is known,
|
|
9
|
+
* because servers such as the core need their public URL at construction.
|
|
10
|
+
*/
|
|
11
|
+
export declare function startTestServer(createHandler: (url: string) => FetchHandler): Promise<TestServer>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { serve } from '@hono/node-server';
|
|
2
|
+
/**
|
|
3
|
+
* Listens on an ephemeral port. The handler is built once the URL is known,
|
|
4
|
+
* because servers such as the core need their public URL at construction.
|
|
5
|
+
*/
|
|
6
|
+
export async function startTestServer(createHandler) {
|
|
7
|
+
let handler;
|
|
8
|
+
const server = serve({ fetch: (request, env) => handler?.(request, env), port: 0 });
|
|
9
|
+
await new Promise((resolve) => server.once('listening', resolve));
|
|
10
|
+
const url = `http://127.0.0.1:${server.address().port}`;
|
|
11
|
+
handler = createHandler(url);
|
|
12
|
+
return {
|
|
13
|
+
url,
|
|
14
|
+
close: () => new Promise((resolve) => server.close(() => resolve())),
|
|
15
|
+
};
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@artemis-apps/test-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"default": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./images": {
|
|
12
|
+
"types": "./dist/test-images.d.ts",
|
|
13
|
+
"default": "./dist/test-images.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@hono/node-server": "2.1.11",
|
|
18
|
+
"better-sqlite3": "13.2.0",
|
|
19
|
+
"drizzle-kit": "0.31.11",
|
|
20
|
+
"drizzle-orm": "0.45.4",
|
|
21
|
+
"node-html-parser": "9.0.7",
|
|
22
|
+
"sharp": "0.35.6",
|
|
23
|
+
"tough-cookie": "6.1.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/better-sqlite3": "9.6.0",
|
|
27
|
+
"@types/node": "26.6.11",
|
|
28
|
+
"hono": "4.13.23",
|
|
29
|
+
"typescript": "7.0.2",
|
|
30
|
+
"vitest": "5.1.2",
|
|
31
|
+
"@artemis-apps/tsconfig": "^0.1.1"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/artemis-apps/sdk.git",
|
|
36
|
+
"directory": "packages/test-kit"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc --project tsconfig.build.json",
|
|
46
|
+
"typecheck": "tsc --project tsconfig.json",
|
|
47
|
+
"test": "vitest run"
|
|
48
|
+
}
|
|
6
49
|
}
|
package/README.md
DELETED