@ac-kit/integration-test-util 0.4.5 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,54 @@
1
1
  # @ac-kit/integration-test-util
2
2
 
3
+ ## 0.5.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [34e8cfa]
8
+ - Updated dependencies [34e8cfa]
9
+ - Updated dependencies [34e8cfa]
10
+ - Updated dependencies [34e8cfa]
11
+ - Updated dependencies [34e8cfa]
12
+ - Updated dependencies [34e8cfa]
13
+ - Updated dependencies [34e8cfa]
14
+ - Updated dependencies [34e8cfa]
15
+ - Updated dependencies [34e8cfa]
16
+ - Updated dependencies [34e8cfa]
17
+ - Updated dependencies [34e8cfa]
18
+ - Updated dependencies [34e8cfa]
19
+ - Updated dependencies [34e8cfa]
20
+ - Updated dependencies [34e8cfa]
21
+ - Updated dependencies [34e8cfa]
22
+ - Updated dependencies [34e8cfa]
23
+ - Updated dependencies [34e8cfa]
24
+ - Updated dependencies [34e8cfa]
25
+ - @ac-kit/core@0.12.0
26
+ - @ac-kit/node@0.11.0
27
+ - @ac-kit/crypto-random@0.2.4
28
+ - @ac-kit/cmd-docker@0.2.9
29
+
30
+ ## 0.5.0
31
+
32
+ ### Minor Changes
33
+
34
+ - 065cf8d: Write a temporary file tree, and serve HTTP on the loopback, for a test.
35
+
36
+ - `createFileTreeFixture(files, options?)` writes each file (a path relative to the fixture root, `/`-separated, to its text, written as UTF-8, or bytes) under a fresh `<prefix>-XXXXXX` directory (prefix `fixture` by default; a prefix that is not one path component — a separator, NUL or control character, `.`, `..`, empty — throws `TypeError`) in `options.parentDirectory` (resolved against the working directory, created when missing) or the OS temporary directory, creating the directories on the way. A path resolving to the root itself or outside it (`""`, `../x`, an absolute path) throws `PathEscapeError` from `@ac-kit/node` before anything is written; on that or any write error the directory is removed before the error is thrown, and when removing it fails too a `SuppressedError` (removal error, suppressed setup error, message naming the directory) is thrown instead. The result has `root` and an idempotent `dispose()`, and is `AsyncDisposable`.
37
+ - `startHttpTestServer(handler)` serves HTTP/1.1 on `127.0.0.1`, on a free port, handing Node's own request and response to `handler` (sync or async), and gives its `port`, `requests` (each request target as received, in arrival order, recorded before the handler runs), `errors` (what the handler threw or rejected with; the response is then a `500`, or destroyed when already started) and an idempotent `close()` that also closes open connections; it is `AsyncDisposable`. Rejects when it cannot listen.
38
+
39
+ ### Patch Changes
40
+
41
+ - Updated dependencies [065cf8d]
42
+ - Updated dependencies [065cf8d]
43
+ - Updated dependencies [065cf8d]
44
+ - Updated dependencies [065cf8d]
45
+ - Updated dependencies [065cf8d]
46
+ - Updated dependencies [065cf8d]
47
+ - @ac-kit/core@0.11.0
48
+ - @ac-kit/node@0.10.0
49
+ - @ac-kit/crypto-random@0.2.3
50
+ - @ac-kit/cmd-docker@0.2.8
51
+
3
52
  ## 0.4.5
4
53
 
5
54
  ### Patch Changes
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # @ac-kit/integration-test-util
2
2
 
3
- Test helpers that drive and probe **external** services: starting a Docker
4
- container for a suite, and checking that a TCP or TLS endpoint is up and
5
- speaking the protocol you expect.
3
+ Test helpers that reach for the machine: starting a Docker container for a
4
+ suite, checking that a TCP or TLS endpoint is up and speaking the protocol you
5
+ expect, writing a temporary file tree, and serving HTTP on the loopback.
6
6
 
7
7
  ```ts
8
8
  import { initDockerSuite, isTcpPortOpen } from "@ac-kit/integration-test-util";
@@ -26,6 +26,36 @@ and `onContainerStopping`/`onContainerStopped` bracket each container, the
26
26
  Also exposes `tryTcpConnectAndReadBanner` and `tryTlsConnectAndReadBanner`, for
27
27
  waiting until a server is not merely listening but actually answering.
28
28
 
29
+ `createFileTreeFixture` writes files, from a path relative to its root to
30
+ their text or bytes, under a fresh directory (the OS temporary directory, or
31
+ `parentDirectory`), refusing a path that leaves it; `startHttpTestServer`
32
+ serves HTTP on `127.0.0.1`, on a free port, with Node's own request and
33
+ response (the handler may be async), and records every request target and,
34
+ in `errors`, whatever the handler threw, answering `500` instead. Both are
35
+ `AsyncDisposable`.
36
+
37
+ ```ts
38
+ import {
39
+ createFileTreeFixture,
40
+ startHttpTestServer,
41
+ } from "@ac-kit/integration-test-util";
42
+
43
+ await using fixture = await createFileTreeFixture({
44
+ "app/package.json": '{ "name": "app" }',
45
+ });
46
+ fixture.root; // "/tmp/fixture-XXXXXX"
47
+
48
+ await using server = await startHttpTestServer((request, response) => {
49
+ if (request.url === "/a.yml") {
50
+ response.writeHead(302, { location: "/b.yml" }).end();
51
+ } else {
52
+ response.writeHead(200, { "content-type": "text/yaml" }).end("a: 1\n");
53
+ }
54
+ });
55
+ await fetch(`http://127.0.0.1:${server.port}/a.yml`);
56
+ server.requests; // ["/a.yml", "/b.yml"]
57
+ ```
58
+
29
59
  Separate from `@ac-kit/test-util` on purpose: this package is free to take
30
60
  heavier dependencies because only a handful of suites need it, while
31
61
  `test-util` must stay light enough for every suite in the tree.
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The files of a fixture: a path relative to its root (`/`-separated, any
3
+ * missing directory created) to the file's content, a string written as UTF-8.
4
+ */
5
+ export type FileTreeFixtureFiles = Readonly<Record<string, string | Uint8Array>>;
6
+ export type FileTreeFixtureOptions = {
7
+ /**
8
+ * The directory to create the fixture in, created when missing; a relative
9
+ * one resolves against `process.cwd()`. Default: the OS temporary directory.
10
+ */
11
+ readonly parentDirectory?: string | undefined;
12
+ /**
13
+ * The start of the fixture directory's name, a single non-empty path
14
+ * component: no `/`, `\`, NUL or control character, not `.` or `..`. Default
15
+ * `fixture`.
16
+ */
17
+ readonly prefix?: string | undefined;
18
+ };
19
+ /** A directory of files a test reads, removed by `dispose`. */
20
+ export type FileTreeFixture = AsyncDisposable & {
21
+ /** The absolute path of the fixture directory. */
22
+ readonly root: string;
23
+ /** Removes the fixture directory and everything in it. Idempotent. */
24
+ dispose(): Promise<void>;
25
+ };
26
+ /**
27
+ * Writes `files` under a fresh directory, `<prefix>-XXXXXX` in
28
+ * {@link FileTreeFixtureOptions.parentDirectory}, for a test to read; dispose
29
+ * of it (`await using`, or `dispose` in an `afterEach`) to remove it.
30
+ *
31
+ * Every path is checked before anything is written. When one fails, or a write
32
+ * does, the directory is removed before the error is thrown; when removing it
33
+ * fails too, a `SuppressedError` is thrown instead, its `error` the removal
34
+ * failure, its `suppressed` the setup failure, its message naming the directory
35
+ * left behind.
36
+ *
37
+ * One directory creation and one write per file.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * await using fixture = await createFileTreeFixture({
42
+ * "app/package.json": JSON.stringify({ name: "app" }),
43
+ * "app/logo.png": new Uint8Array([0x89, 0x50, 0x4e, 0x47]),
44
+ * });
45
+ * await readFile(join(fixture.root, "app/package.json"), "utf8");
46
+ * ```;
47
+ *
48
+ * @throws {TypeError} When `prefix` is not a single path component.
49
+ * @throws {PathEscapeError} (from `@ac-kit/node`) When a path resolves to the
50
+ * fixture directory itself or outside it: `""`, `../x`, an absolute path.
51
+ */
52
+ export declare function createFileTreeFixture(files: FileTreeFixtureFiles, options?: FileTreeFixtureOptions): Promise<FileTreeFixture>;
53
+ //# sourceMappingURL=create-file-tree-fixture.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-file-tree-fixture.d.ts","sourceRoot":"","sources":["../../src/fs/create-file-tree-fixture.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG,QAAQ,CAC1C,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CACnC,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACpC;;;OAGG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAE9C;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACrC,CAAC;AAEF,+DAA+D;AAC/D,MAAM,MAAM,eAAe,GAAG,eAAe,GAAG;IAC/C,kDAAkD;IAClD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,sEAAsE;IACtE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB,CAAC;AAsBF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,qBAAqB,CAC1C,KAAK,EAAE,oBAAoB,EAC3B,OAAO,CAAC,EAAE,sBAAsB,GAC9B,OAAO,CAAC,eAAe,CAAC,CA+C1B"}
@@ -0,0 +1,84 @@
1
+ import { mkdir, rm, writeFile } from "node:fs/promises";
2
+ import { dirname, resolve } from "node:path";
3
+ import { assertPathWithin, createTempDir } from "@ac-kit/node";
4
+ const SLASH_ = 0x2f;
5
+ const BACKSLASH_ = 0x5c;
6
+ const FIRST_PRINTABLE_ = 0x20;
7
+ // No separator of either platform, NUL or C0 control: a prefix must stay one
8
+ // portable name inside the parent directory.
9
+ function isPathComponent_(name) {
10
+ if (name === "" || name === "." || name === "..") {
11
+ return false;
12
+ }
13
+ for (let index = 0; index < name.length; index++) {
14
+ const code = name.charCodeAt(index);
15
+ if (code < FIRST_PRINTABLE_ || code === SLASH_ || code === BACKSLASH_) {
16
+ return false;
17
+ }
18
+ }
19
+ return true;
20
+ }
21
+ /**
22
+ * Writes `files` under a fresh directory, `<prefix>-XXXXXX` in
23
+ * {@link FileTreeFixtureOptions.parentDirectory}, for a test to read; dispose
24
+ * of it (`await using`, or `dispose` in an `afterEach`) to remove it.
25
+ *
26
+ * Every path is checked before anything is written. When one fails, or a write
27
+ * does, the directory is removed before the error is thrown; when removing it
28
+ * fails too, a `SuppressedError` is thrown instead, its `error` the removal
29
+ * failure, its `suppressed` the setup failure, its message naming the directory
30
+ * left behind.
31
+ *
32
+ * One directory creation and one write per file.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * await using fixture = await createFileTreeFixture({
37
+ * "app/package.json": JSON.stringify({ name: "app" }),
38
+ * "app/logo.png": new Uint8Array([0x89, 0x50, 0x4e, 0x47]),
39
+ * });
40
+ * await readFile(join(fixture.root, "app/package.json"), "utf8");
41
+ * ```;
42
+ *
43
+ * @throws {TypeError} When `prefix` is not a single path component.
44
+ * @throws {PathEscapeError} (from `@ac-kit/node`) When a path resolves to the
45
+ * fixture directory itself or outside it: `""`, `../x`, an absolute path.
46
+ */
47
+ export async function createFileTreeFixture(files, options) {
48
+ const prefix = options?.prefix ?? "fixture";
49
+ if (!isPathComponent_(prefix)) {
50
+ throw new TypeError(`${JSON.stringify(prefix)} is not a single path component`);
51
+ }
52
+ const parentDirectory = options?.parentDirectory === undefined
53
+ ? undefined
54
+ : resolve(options.parentDirectory);
55
+ if (parentDirectory !== undefined) {
56
+ await mkdir(parentDirectory, { recursive: true });
57
+ }
58
+ const root = await createTempDir(prefix, parentDirectory);
59
+ const dispose = () => rm(root, { recursive: true, force: true });
60
+ try {
61
+ const writes = Object.entries(files).map(([relativePath, content]) => [
62
+ assertPathWithin(root, relativePath, {
63
+ from: root,
64
+ allowRoot: false,
65
+ }),
66
+ content,
67
+ ]);
68
+ for (const [path, content] of writes) {
69
+ await mkdir(dirname(path), { recursive: true });
70
+ await writeFile(path, content);
71
+ }
72
+ }
73
+ catch (error) {
74
+ try {
75
+ await dispose();
76
+ }
77
+ catch (cleanupError) {
78
+ throw new SuppressedError(cleanupError, error, `Fixture setup failed, then removing ${root} failed`);
79
+ }
80
+ throw error;
81
+ }
82
+ return { root, dispose, [Symbol.asyncDispose]: dispose };
83
+ }
84
+ //# sourceMappingURL=create-file-tree-fixture.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-file-tree-fixture.js","sourceRoot":"","sources":["../../src/fs/create-file-tree-fixture.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE7C,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAA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,CAAC,CAAC;IACnD,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,aAAa,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAC1D,MAAM,OAAO,GAAG,GAAkB,EAAE,CACnC,EAAE,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAE5C,IAAI,CAAC;QACJ,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CACvC,CAAC,CAAC,YAAY,EAAE,OAAO,CAAC,EAAE,EAAE,CAC3B;YACC,gBAAgB,CAAC,IAAI,EAAE,YAAY,EAAE;gBACpC,IAAI,EAAE,IAAI;gBACV,SAAS,EAAE,KAAK;aAChB,CAAC;YACF,OAAO;SACE,CACX,CAAC;QACF,KAAK,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,MAAM,EAAE,CAAC;YACtC,MAAM,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YAChD,MAAM,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAChC,CAAC;IACF,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,CAAC;YACJ,MAAM,OAAO,EAAE,CAAC;QACjB,CAAC;QAAC,OAAO,YAAY,EAAE,CAAC;YACvB,MAAM,IAAI,eAAe,CACxB,YAAY,EACZ,KAAK,EACL,uCAAuC,IAAI,SAAS,CACpD,CAAC;QACH,CAAC;QACD,MAAM,KAAK,CAAC;IACb,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,OAAO,EAAE,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,46 @@
1
+ import { type IncomingMessage, type ServerResponse } from "node:http";
2
+ /** An HTTP server a test talks to, on the IPv4 loopback. */
3
+ export type HttpTestServer = AsyncDisposable & {
4
+ /** The TCP port it listens on, on `127.0.0.1`. */
5
+ readonly port: number;
6
+ /**
7
+ * The request target of every request it received (`/path?query`, as written
8
+ * in the request line), in arrival order. Grows as requests arrive.
9
+ */
10
+ readonly requests: readonly string[];
11
+ /**
12
+ * What the handler threw or rejected with, in order. Grows as it fails;
13
+ * assert it is empty to make a handler failure fail the test.
14
+ */
15
+ readonly errors: readonly unknown[];
16
+ /**
17
+ * Stops listening and closes every connection, idle or not, then resolves.
18
+ * Idempotent.
19
+ */
20
+ close(): Promise<void>;
21
+ };
22
+ /**
23
+ * Serves HTTP/1.1 on the IPv4 loopback (`127.0.0.1`), on a free port, for a
24
+ * test; close it (`await using`, or `close` in an `afterEach`).
25
+ *
26
+ * The handler gets Node's own request and response, so it can misbehave as a
27
+ * test needs: truncate a body, never answer, destroy the socket. It may be
28
+ * async. When it throws or rejects, the error is added to
29
+ * {@link HttpTestServer.errors} rather than left uncaught, and the response is a
30
+ * bare `500` (any header it staged removed) if nothing was sent yet, or else is
31
+ * destroyed: the client is never left waiting on a failed handler.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * await using server = await startHttpTestServer((request, response) => {
36
+ * response.writeHead(200, { "content-type": "text/plain" }).end("ok");
37
+ * });
38
+ * await fetch(`http://127.0.0.1:${server.port}/a?b=1`);
39
+ * server.requests; // ["/a?b=1"]
40
+ * ```;
41
+ *
42
+ * @param handler Answers every request, after it is recorded.
43
+ * @throws {Error} When the server cannot listen (no IPv4 loopback).
44
+ */
45
+ export declare function startHttpTestServer(handler: (request: IncomingMessage, response: ServerResponse) => void | Promise<void>): Promise<HttpTestServer>;
46
+ //# sourceMappingURL=start-http-test-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start-http-test-server.d.ts","sourceRoot":"","sources":["../../src/http/start-http-test-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,MAAM,WAAW,CAAC;AAWnB,4DAA4D;AAC5D,MAAM,MAAM,cAAc,GAAG,eAAe,GAAG;IAC9C,kDAAkD;IAClD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IAErC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,SAAS,OAAO,EAAE,CAAC;IAEpC;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,mBAAmB,CACxC,OAAO,EAAE,CACR,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,cAAc,KACpB,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GACvB,OAAO,CAAC,cAAc,CAAC,CA0DzB"}
@@ -0,0 +1,83 @@
1
+ import { createServer, } from "node:http";
2
+ function isThenable_(value) {
3
+ return ((typeof value === "object" || typeof value === "function") &&
4
+ value !== null &&
5
+ typeof value.then === "function");
6
+ }
7
+ /**
8
+ * Serves HTTP/1.1 on the IPv4 loopback (`127.0.0.1`), on a free port, for a
9
+ * test; close it (`await using`, or `close` in an `afterEach`).
10
+ *
11
+ * The handler gets Node's own request and response, so it can misbehave as a
12
+ * test needs: truncate a body, never answer, destroy the socket. It may be
13
+ * async. When it throws or rejects, the error is added to
14
+ * {@link HttpTestServer.errors} rather than left uncaught, and the response is a
15
+ * bare `500` (any header it staged removed) if nothing was sent yet, or else is
16
+ * destroyed: the client is never left waiting on a failed handler.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * await using server = await startHttpTestServer((request, response) => {
21
+ * response.writeHead(200, { "content-type": "text/plain" }).end("ok");
22
+ * });
23
+ * await fetch(`http://127.0.0.1:${server.port}/a?b=1`);
24
+ * server.requests; // ["/a?b=1"]
25
+ * ```;
26
+ *
27
+ * @param handler Answers every request, after it is recorded.
28
+ * @throws {Error} When the server cannot listen (no IPv4 loopback).
29
+ */
30
+ export async function startHttpTestServer(handler) {
31
+ const requests = [];
32
+ const errors = [];
33
+ const fail = (response, error) => {
34
+ errors.push(error);
35
+ if (response.headersSent) {
36
+ response.destroy();
37
+ }
38
+ else {
39
+ // Headers the handler staged (a Content-Length) must not outlive it.
40
+ for (const name of response.getHeaderNames()) {
41
+ response.removeHeader(name);
42
+ }
43
+ response.writeHead(500).end();
44
+ }
45
+ };
46
+ const server = createServer((request, response) => {
47
+ requests.push(request.url ?? "");
48
+ try {
49
+ const handled = handler(request, response);
50
+ // A sync handler may return anything (a timer handle): only a
51
+ // thenable can fail later.
52
+ if (isThenable_(handled)) {
53
+ Promise.resolve(handled).catch((error) => fail(response, error));
54
+ }
55
+ }
56
+ catch (error) {
57
+ fail(response, error);
58
+ }
59
+ });
60
+ await new Promise((resolve, reject) => {
61
+ server.once("error", reject);
62
+ server.listen(0, "127.0.0.1", () => {
63
+ server.off("error", reject);
64
+ resolve();
65
+ });
66
+ });
67
+ let closed;
68
+ const close = () => {
69
+ closed ??= new Promise((resolve, reject) => {
70
+ server.close((error) => error === undefined ? resolve() : reject(error));
71
+ server.closeAllConnections();
72
+ });
73
+ return closed;
74
+ };
75
+ return {
76
+ port: server.address().port,
77
+ requests,
78
+ errors,
79
+ close,
80
+ [Symbol.asyncDispose]: close,
81
+ };
82
+ }
83
+ //# sourceMappingURL=start-http-test-server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start-http-test-server.js","sourceRoot":"","sources":["../../src/http/start-http-test-server.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,GAGZ,MAAM,WAAW,CAAC;AAGnB,SAAS,WAAW,CAAC,KAAc;IAClC,OAAO,CACN,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,UAAU,CAAC;QAC1D,KAAK,KAAK,IAAI;QACd,OAAQ,KAA4B,CAAC,IAAI,KAAK,UAAU,CACxD,CAAC;AACH,CAAC;AA0BD;;;;;;;;;;;;;;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O,EAAE,MAAM,EAAE,EAAE;QAC3C,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,WAAW,EAAE,GAAG,EAAE;YAClC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YAC5B,OAAO,EAAE,CAAC;QACX,CAAC,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,IAAI,MAAiC,CAAC;IACtC,MAAM,KAAK,GAAG,GAAkB,EAAE;QACjC,MAAM,KAAK,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAChD,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CACtB,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAC/C,CAAC;YACF,MAAM,CAAC,mBAAmB,EAAE,CAAC;QAC9B,CAAC,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC;IACf,CAAC,CAAC;IAEF,OAAO;QACN,IAAI,EAAG,MAAM,CAAC,OAAO,EAAkB,CAAC,IAAI;QAC5C,QAAQ;QACR,MAAM;QACN,KAAK;QACL,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,KAAK;KAC5B,CAAC;AACH,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "./docker/init-docker-suite.js";
2
+ export * from "./fs/create-file-tree-fixture.js";
3
+ export * from "./http/start-http-test-server.js";
2
4
  export * from "./net/is-tcp-port-open.js";
3
5
  export * from "./net/try-tcp-connect-n-read-banner.js";
4
6
  export * from "./net/try-tls-connect-n-read-banner.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wCAAwC,CAAC;AACvD,cAAc,wCAAwC,CAAC;AACvD,cAAc,yCAAyC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wCAAwC,CAAC;AACvD,cAAc,wCAAwC,CAAC;AACvD,cAAc,yCAAyC,CAAC"}
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "./docker/init-docker-suite.js";
2
+ export * from "./fs/create-file-tree-fixture.js";
3
+ export * from "./http/start-http-test-server.js";
2
4
  export * from "./net/is-tcp-port-open.js";
3
5
  export * from "./net/try-tcp-connect-n-read-banner.js";
4
6
  export * from "./net/try-tls-connect-n-read-banner.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wCAAwC,CAAC;AACvD,cAAc,wCAAwC,CAAC;AACvD,cAAc,yCAAyC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wCAAwC,CAAC;AACvD,cAAc,wCAAwC,CAAC;AACvD,cAAc,yCAAyC,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "@ac-kit/integration-test-util",
3
- "version": "0.4.5",
4
- "description": "Test helpers that drive and probe external services — Docker containers, TCP and TLS banner checks — from a package's integration tests.",
3
+ "version": "0.5.1",
4
+ "description": "Test helpers that reach for the machine — Docker containers, TCP and TLS banner checks, temporary file trees, a loopback HTTP server — from a package's integration tests.",
5
5
  "keywords": [
6
6
  "docker",
7
+ "fixture",
8
+ "http",
7
9
  "integration",
8
10
  "tcp",
9
11
  "testing"
@@ -38,23 +40,23 @@
38
40
  "prepack": "yarn build"
39
41
  },
40
42
  "dependencies": {
41
- "@ac-kit/cmd-docker": "^0.2.7",
42
- "@ac-kit/core": "^0.10.0",
43
- "@ac-kit/crypto-random": "^0.2.2",
44
- "@ac-kit/node": "^0.9.0",
43
+ "@ac-kit/cmd-docker": "^0.2.9",
44
+ "@ac-kit/core": "^0.12.0",
45
+ "@ac-kit/crypto-random": "^0.2.4",
46
+ "@ac-kit/node": "^0.11.0",
45
47
  "type-fest": "^6.0.0"
46
48
  },
47
49
  "devDependencies": {
48
50
  "@ac-kit/tsconfig": "0.1.0",
49
51
  "@ac-kit/typedoc-config": "0.1.0",
50
- "@ac-kit/vitest-config": "0.1.1",
51
- "@types/node": "26.6.6",
52
+ "@ac-kit/vitest-config": "0.2.0",
53
+ "@types/node": "26.6.11",
52
54
  "@typescript/native": "typescript@7.0.2",
53
55
  "rimraf": "6.1.4",
54
56
  "typedoc": "0.28.20",
55
57
  "typescript": "@typescript/typescript6@6.0.2",
56
- "vite": "8.3.8",
57
- "vitest": "5.1.1"
58
+ "vite": "8.4.0",
59
+ "vitest": "5.1.2"
58
60
  },
59
61
  "peerDependencies": {
60
62
  "vitest": ">=4.0.0"