@impz/testing 0.0.0-stage → 4.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Geoff Whatley
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,99 @@
1
- # Temporary Holding Version
1
+ # @impz/testing
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A real [impd](https://github.com/zgeoff/impz), the imp host daemon, for the tests of code that
4
+ drives impd through [`@impz/client`](https://www.npmjs.com/package/@impz/client). impd runs its own
5
+ startup, API, broker and lifecycle in the test process, on stand-ins for the VMs and the host. A
6
+ test starts one in about 50 ms, so code that talks to impd needs no KVM host.
7
+
8
+ It runs on Bun only, under `bun test`: impd uses `bun:sqlite`. Each release matches the
9
+ `@impz/client` release of the same version.
10
+
11
+ ```sh
12
+ bun add --dev @impz/testing
13
+ ```
14
+
15
+ ## Start and stop
16
+
17
+ ```ts
18
+ import { onTestFinished, test } from 'bun:test';
19
+ import { startTestImpd } from '@impz/testing';
20
+
21
+ test('it creates an imp', async () => {
22
+ const impd = await startTestImpd();
23
+
24
+ onTestFinished(impd.stop);
25
+
26
+ await impd.seedImage('ubuntu');
27
+ await impd.client.imps.create({ name: 'box', image: 'ubuntu' });
28
+ });
29
+ ```
30
+
31
+ `startTestImpd(options)` takes:
32
+
33
+ - `env`: impd settings, as the `IMP_*` variables impd reads at start.
34
+ - `onLog`: a callback for each line impd logs. impd writes nothing to stderr.
35
+
36
+ It returns:
37
+
38
+ | Field | What it is |
39
+ | --------------------- | ------------------------------------------------------------------- |
40
+ | `url` | impd's API on a loopback port, for a client the test builds itself |
41
+ | `client` | an `@impz/client` client on `url` with `TEST_TOKEN`, the root token |
42
+ | `seedImage(name)` | adds an image of that name, which `imps.create` boots |
43
+ | `startGuest(impName)` | starts the stand-in guest of a created imp, for reverse forwards |
44
+ | `fireTicker(label)` | runs one of impd's periodic jobs now; no job runs on its own |
45
+ | `stop()` | stops impd and removes its data |
46
+
47
+ The client sleeps and wakes an imp as it does on a real host: `client.imps.sleep({ name })` and
48
+ `client.imps.wake({ name })`.
49
+
50
+ ## The stand-in guest
51
+
52
+ An imp on this impd runs no VM, so nothing runs inside it. `startGuest(impName)` answers impd for
53
+ the imp's guest agent, and its `connect(port)` plays a process in the imp that dials a reverse
54
+ forward on that guest port. It returns a Node `Duplex`: what the test writes reaches the forward's
55
+ local end, and what that end sends back is what the test reads.
56
+
57
+ ```ts
58
+ import { openReverseForward } from '@impz/client';
59
+ import { TEST_TOKEN } from '@impz/testing';
60
+
61
+ const guest = await impd.startGuest('box');
62
+
63
+ const forward = openReverseForward({
64
+ baseUrl: impd.url,
65
+ token: TEST_TOKEN,
66
+ name: 'box',
67
+ guest: { network: 'tcp', port: 4000 },
68
+ connect: (url, headers) => new WebSocket(url, { headers: { ...headers } }),
69
+ onConnection: (accept) => {
70
+ // relay to the local end, as the app under test does
71
+ },
72
+ });
73
+
74
+ await forward.listening;
75
+
76
+ const stream = await guest.connect(4000);
77
+ ```
78
+
79
+ The guest copies these behaviours of the real guest agent's reverse forwards:
80
+
81
+ - Bytes relay both ways between the process and the forward's local end.
82
+ - The process's end of its side reaches the local end as an EOF, and the local end's EOF ends what
83
+ the process reads.
84
+ - A process that resets its connection drops the relay with no EOF.
85
+ - A forward that asks for port 0 gets a free port, and a second forward on a port in use fails with
86
+ `LISTEN_FAILED`.
87
+ - When the imp sleeps or goes away, the guest drops its connections, so a forward ends as `lost`. A
88
+ forward opened after the wake listens on the guest again.
89
+
90
+ It does not copy:
91
+
92
+ - unix socket forwards and ssh-agent forwarding, which it answers with `UNKNOWN_OP`
93
+ - the refusal of a port below 1024 to an image whose user is not root
94
+ - the limit of 16 clients waiting for impd, and the 10 s after which an unpaired client closes
95
+
96
+ ## What this impd does not do
97
+
98
+ It boots no VM, runs no command in an imp, and reaches no network: an exec, a file copy or an egress
99
+ probe needs a real impd host. The broker refuses every upstream.
@@ -0,0 +1,3 @@
1
+ export { startTestImpd } from './start-test-impd';
2
+ export { TEST_TOKEN } from './test-token';
3
+ export type { TestGuest, TestImpd, TestImpdOptions } from './types';