dreamteamer 0.47.0 → 0.48.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.
@@ -0,0 +1,29 @@
1
+ // The typed contract of `import … from 'dreamteamer/testing'` (src/testing.js): a module's own test
2
+ // suite on the real engine. Synchronous throughout. Policy (D64): on 0.x a breaking change to this
3
+ // export lands in a minor, announced one release ahead in UPDATING.md.
4
+
5
+ /** One engine run: its exit status (null when it was ended on `timeoutMs`), stdout and stderr. */
6
+ export interface Result { status: number | null; stdout: string; stderr: string }
7
+ export interface RunOptions {
8
+ /** Laid over the process environment (and the synthetic git identity) for this run. */
9
+ env?: Record<string, string>;
10
+ /** Ends the run by SIGKILL after this many milliseconds; `status` is then null. Default 60 000. */
11
+ timeoutMs?: number;
12
+ /** Text handed to the run's stdin (a hook payload, a prompt's answer). */
13
+ input?: string;
14
+ }
15
+ /** A workspace `createWorkspace` made: its root, the engine it runs on, and `run` bound to it. */
16
+ export interface Workspace { root: string; engine: string; dt(args: string[], opts?: RunOptions): Result }
17
+
18
+ /** The package root of the engine running this file — never one found on PATH. */
19
+ export const ENGINE: string;
20
+ /** `dt init` in a fresh temp folder on `engine`, else `DT_ENGINE_DIR`, else `ENGINE`. Throws if init fails. */
21
+ export function createWorkspace(opts?: { engine?: string }): Workspace;
22
+ /** `npm pack` of a module folder; the tarball's path. */
23
+ export function pack(moduleDir: string): string;
24
+ /** `dt install` of a name, a tarball path or a module folder (packed first). Exit 3 is a result. */
25
+ export function install(ws: Workspace, spec: string, opts?: RunOptions): Result;
26
+ /** The CLI of the workspace's engine, run in the workspace. */
27
+ export function run(ws: Workspace, args: string[], opts?: RunOptions): Result;
28
+ /** Does the running engine (or `DT_ENGINE_DIR`'s) satisfy the semver range? */
29
+ export function engineMeets(range: string): boolean;
package/src/testing.js ADDED
@@ -0,0 +1,86 @@
1
+ // `dreamteamer/testing` — what a module's own test suite needs to test itself on the real engine,
2
+ // with nothing but `node --test` and this import (cr-dt-007, G2). Every helper spawns the engine the
3
+ // way a consumer runs it: `init` in a fresh folder, `dt install` of the module packed with `npm pack`
4
+ // (so the package's `files` list is what is tested), then `dt` verbs. Nothing imports src/ by path, and
5
+ // the engine is THIS file's package — never a `dreamteamer` that happens to be on PATH.
6
+ //
7
+ // createWorkspace({ engine? }) a fresh, committed workspace on the engine (DT_ENGINE_DIR wins when set)
8
+ // pack(moduleDir) the tarball npm would publish
9
+ // install(ws, spec, opts?) `dt install` of a name, a tarball, or a folder (packed first)
10
+ // run(ws, args, opts?) the CLI in the workspace — { status, stdout, stderr }; status null on timeout
11
+ // engineMeets(range) does the running engine satisfy a semver range
12
+ //
13
+ // Synchronous throughout, so a test reads top to bottom. Temp folders are removed at exit unless
14
+ // DT_KEEP_FIXTURES is set. Policy (D64): on 0.x a breaking change to this export lands in a minor,
15
+ // announced one release ahead in UPDATING.md.
16
+ import fs from 'node:fs';
17
+ import os from 'node:os';
18
+ import path from 'node:path';
19
+ import { spawnSync } from 'node:child_process';
20
+ import { fileURLToPath } from 'node:url';
21
+ import { satisfies } from './semver.js';
22
+ import { resolveNpm, childEnv } from './checkout.js';
23
+ import { engineRun, freshWorkspace } from './check-alone.js';
24
+
25
+ /** The engine running this file: its package root. */
26
+ export const ENGINE = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
27
+ const VERSION = JSON.parse(fs.readFileSync(path.join(ENGINE, 'package.json'), 'utf8')).version;
28
+
29
+ const made = [];
30
+ let hooked = false;
31
+ function tempDir(prefix) {
32
+ const dir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), `dreamteamer-testing-${prefix}-`)));
33
+ made.push(dir);
34
+ if (!hooked) {
35
+ hooked = true;
36
+ process.on('exit', () => { if (!process.env.DT_KEEP_FIXTURES) for (const d of made) fs.rmSync(d, { recursive: true, force: true }); });
37
+ }
38
+ return dir;
39
+ }
40
+
41
+ /** The CLI of the workspace's engine, in the workspace. */
42
+ export function run(ws, args, opts = {}) {
43
+ return engineRun(ws.engine, ws.root, args, { timeoutMs: 60_000, ...opts });
44
+ }
45
+
46
+ /**
47
+ * A fresh workspace made by the engine's own `init`: the engine declared as a `file:` dependency and
48
+ * linked before init, so init finds it present and asks no registry; git-initialised and committed.
49
+ * `engine` (a package root) wins, then `DT_ENGINE_DIR`, then the engine running this file.
50
+ */
51
+ export function createWorkspace({ engine } = {}) {
52
+ const eng = engine ?? (process.env.DT_ENGINE_DIR ? path.resolve(process.env.DT_ENGINE_DIR) : ENGINE);
53
+ const root = tempDir('ws');
54
+ const ws = { root, engine: eng, dt: (args, opts) => run(ws, args, opts) };
55
+ freshWorkspace(root, eng);
56
+ return ws;
57
+ }
58
+
59
+ /** `npm pack` of a module folder into a temp folder; the tarball's path. */
60
+ export function pack(moduleDir) {
61
+ const npm = resolveNpm();
62
+ if (!npm) throw new Error('no npm beside this node or on PATH — pack needs it');
63
+ const dest = tempDir('pack');
64
+ const r = spawnSync(npm, ['pack', '--silent', '--pack-destination', dest], { cwd: path.resolve(moduleDir), encoding: 'utf8', timeout: 120_000, env: childEnv() });
65
+ if (r.status !== 0) throw new Error(`npm pack failed in ${moduleDir} (exit ${r.status}):\n${r.stderr}${r.stdout}`);
66
+ const tgz = fs.readdirSync(dest).filter((f) => f.endsWith('.tgz'));
67
+ if (tgz.length !== 1) throw new Error(`npm pack left ${tgz.length} tarballs in ${dest}`);
68
+ return path.join(dest, tgz[0]);
69
+ }
70
+
71
+ /** `dt install` of a name, a tarball path, or a module folder (packed first, so its `files` list is
72
+ * what is installed). Never throws on the engine's exit: 3 (an input still needed) is a result. */
73
+ export function install(ws, spec, opts = {}) {
74
+ const isFolder = !spec.endsWith('.tgz') && fs.existsSync(spec) && fs.statSync(spec).isDirectory();
75
+ const target = isFolder ? pack(spec) : spec;
76
+ return run(ws, ['install', target, '--no-input'], { timeoutMs: 300_000, ...opts });
77
+ }
78
+
79
+ /** Does the running engine (or DT_ENGINE_DIR's) satisfy `range`? */
80
+ export function engineMeets(range) {
81
+ let version = VERSION;
82
+ if (process.env.DT_ENGINE_DIR) {
83
+ try { version = JSON.parse(fs.readFileSync(path.join(path.resolve(process.env.DT_ENGINE_DIR), 'package.json'), 'utf8')).version; } catch { /* the running engine's */ }
84
+ }
85
+ return satisfies(version, range) === true;
86
+ }