@zgeoff/rig 0.0.0 → 0.0.2

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.
Files changed (143) hide show
  1. package/README.md +87 -3
  2. package/dist/browser/browser-preload.d.ts +1 -0
  3. package/dist/browser/build-browser-run.d.ts +9 -0
  4. package/dist/browser/build-lane-environments.d.ts +8 -0
  5. package/dist/browser/build-recorded-command.d.ts +1 -0
  6. package/dist/browser/find-browser-run.d.ts +4 -0
  7. package/dist/browser/find-private-dir-problem.d.ts +2 -0
  8. package/dist/browser/has-browser-build-script.d.ts +1 -0
  9. package/dist/browser/has-error-code.d.ts +1 -0
  10. package/dist/browser/is-zombie-state.d.ts +1 -0
  11. package/dist/browser/lane-files.d.ts +13 -0
  12. package/dist/browser/make-browser-lane-hooks.d.ts +22 -0
  13. package/dist/browser/pick-chrome-dirs.d.ts +7 -0
  14. package/dist/browser/pick-exit-code-after-tests.d.ts +1 -0
  15. package/dist/browser/pick-step-after-build.d.ts +7 -0
  16. package/dist/browser/pick-stop-step.d.ts +17 -0
  17. package/dist/browser/probe-host-process.d.ts +1 -0
  18. package/dist/browser/probe-process.d.ts +6 -0
  19. package/dist/browser/probe-run.d.ts +9 -0
  20. package/dist/browser/process-group-still-running-error.d.ts +4 -0
  21. package/dist/browser/process-group-stop-error.d.ts +4 -0
  22. package/dist/browser/process-groups.d.ts +5 -0
  23. package/dist/browser/process-runtime.d.ts +15 -0
  24. package/dist/browser/read-dir-names.d.ts +1 -0
  25. package/dist/browser/read-process-table.d.ts +6 -0
  26. package/dist/browser/remove-exited-run-dirs.d.ts +2 -0
  27. package/dist/browser/remove-path.d.ts +1 -0
  28. package/dist/browser/run-in-page.d.ts +23 -0
  29. package/dist/browser/should-keep-run-folders.d.ts +1 -0
  30. package/dist/browser/start-browser.d.ts +9 -0
  31. package/dist/browser/start-process-group.d.ts +17 -0
  32. package/dist/browser/wire-browser-lane-hooks.d.ts +12 -0
  33. package/dist/build-diet-command.d.ts +1 -0
  34. package/dist/build-init-scripts.d.ts +3 -0
  35. package/dist/build-test-selection.d.ts +23 -0
  36. package/dist/build-unit-command.d.ts +2 -0
  37. package/dist/cli.d.ts +2 -0
  38. package/dist/define-rig.d.ts +2 -0
  39. package/dist/find-timeout-arg.d.ts +1 -0
  40. package/dist/format-no-match-message.d.ts +1 -0
  41. package/dist/format-no-tests-message.d.ts +1 -0
  42. package/dist/index.d.ts +6 -0
  43. package/dist/index.js +255 -0
  44. package/dist/init-files.d.ts +6 -0
  45. package/dist/is-command-name.d.ts +3 -0
  46. package/dist/lane-hooks.d.ts +13 -0
  47. package/dist/make-repo-lane-hooks.d.ts +12 -0
  48. package/dist/parse-duration.d.ts +1 -0
  49. package/dist/parse-lane-hook.d.ts +3 -0
  50. package/dist/parse-lane-start.d.ts +2 -0
  51. package/dist/parse-rig-config.d.ts +9 -0
  52. package/dist/parse-test-args.d.ts +5 -0
  53. package/dist/pick-e2e-dirs.d.ts +1 -0
  54. package/dist/pick-lane-selection.d.ts +2 -0
  55. package/dist/read-cli-args.d.ts +5 -0
  56. package/dist/read-memory-budget.d.ts +8 -0
  57. package/dist/read-rig-config.d.ts +6 -0
  58. package/dist/read-tracked-files.d.ts +1 -0
  59. package/dist/read-worker-count.d.ts +13 -0
  60. package/dist/resolve-diet-script.d.ts +1 -0
  61. package/dist/resolve-related-tests.d.ts +6 -0
  62. package/dist/run-cli.d.ts +9 -0
  63. package/dist/run-command.d.ts +6 -0
  64. package/dist/run-init.d.ts +8 -0
  65. package/dist/run-lane-hooks.d.ts +2 -0
  66. package/dist/run-lane.d.ts +18 -0
  67. package/dist/run-related-tests.d.ts +10 -0
  68. package/dist/run-unit-tests.d.ts +12 -0
  69. package/dist/run-unit.d.ts +8 -0
  70. package/dist/standard-lanes.d.ts +8 -0
  71. package/dist/system-clock.d.ts +5 -0
  72. package/dist/types.d.ts +42 -0
  73. package/package.json +41 -3
  74. package/src/browser/browser-preload.ts +36 -0
  75. package/src/browser/build-browser-run.ts +32 -0
  76. package/src/browser/build-lane-environments.ts +33 -0
  77. package/src/browser/build-recorded-command.ts +12 -0
  78. package/src/browser/find-browser-run.ts +15 -0
  79. package/src/browser/find-private-dir-problem.ts +21 -0
  80. package/src/browser/has-browser-build-script.ts +7 -0
  81. package/src/browser/has-error-code.ts +3 -0
  82. package/src/browser/is-zombie-state.ts +4 -0
  83. package/src/browser/lane-files.ts +56 -0
  84. package/src/browser/make-browser-lane-hooks.ts +228 -0
  85. package/src/browser/pick-chrome-dirs.ts +34 -0
  86. package/src/browser/pick-exit-code-after-tests.ts +5 -0
  87. package/src/browser/pick-step-after-build.ts +20 -0
  88. package/src/browser/pick-stop-step.ts +28 -0
  89. package/src/browser/probe-host-process.ts +11 -0
  90. package/src/browser/probe-process.ts +35 -0
  91. package/src/browser/probe-run.ts +38 -0
  92. package/src/browser/process-group-still-running-error.ts +9 -0
  93. package/src/browser/process-group-stop-error.ts +10 -0
  94. package/src/browser/process-groups.ts +45 -0
  95. package/src/browser/process-runtime.ts +40 -0
  96. package/src/browser/read-dir-names.ts +15 -0
  97. package/src/browser/read-process-table.ts +35 -0
  98. package/src/browser/remove-exited-run-dirs.ts +21 -0
  99. package/src/browser/remove-path.ts +5 -0
  100. package/src/browser/run-in-page.ts +103 -0
  101. package/src/browser/should-keep-run-folders.ts +7 -0
  102. package/src/browser/start-browser.ts +40 -0
  103. package/src/browser/start-process-group.ts +168 -0
  104. package/src/browser/wire-browser-lane-hooks.ts +88 -0
  105. package/src/build-diet-command.ts +11 -0
  106. package/src/build-init-scripts.ts +40 -0
  107. package/src/build-test-selection.ts +124 -0
  108. package/src/build-unit-command.ts +22 -0
  109. package/src/cli.ts +73 -0
  110. package/src/define-rig.ts +195 -0
  111. package/src/find-timeout-arg.ts +3 -0
  112. package/src/format-no-match-message.ts +3 -0
  113. package/src/format-no-tests-message.ts +3 -0
  114. package/src/index.ts +14 -0
  115. package/src/init-files.ts +15 -0
  116. package/src/is-command-name.ts +9 -0
  117. package/src/lane-hooks.ts +15 -0
  118. package/src/make-repo-lane-hooks.ts +70 -0
  119. package/src/parse-duration.ts +14 -0
  120. package/src/parse-lane-hook.ts +20 -0
  121. package/src/parse-lane-start.ts +30 -0
  122. package/src/parse-rig-config.ts +96 -0
  123. package/src/parse-test-args.ts +14 -0
  124. package/src/pick-e2e-dirs.ts +25 -0
  125. package/src/pick-lane-selection.ts +14 -0
  126. package/src/read-cli-args.ts +57 -0
  127. package/src/read-memory-budget.ts +65 -0
  128. package/src/read-rig-config.ts +32 -0
  129. package/src/read-tracked-files.ts +20 -0
  130. package/src/read-worker-count.ts +40 -0
  131. package/src/resolve-diet-script.ts +7 -0
  132. package/src/resolve-related-tests.ts +52 -0
  133. package/src/run-cli.ts +68 -0
  134. package/src/run-command.ts +36 -0
  135. package/src/run-init.ts +71 -0
  136. package/src/run-lane-hooks.ts +49 -0
  137. package/src/run-lane.ts +138 -0
  138. package/src/run-related-tests.ts +113 -0
  139. package/src/run-unit-tests.ts +42 -0
  140. package/src/run-unit.ts +68 -0
  141. package/src/standard-lanes.ts +23 -0
  142. package/src/system-clock.ts +10 -0
  143. package/src/types.ts +75 -0
package/dist/index.js ADDED
@@ -0,0 +1,255 @@
1
+ // src/is-command-name.ts
2
+ var commandNames = ["init", "related"];
3
+ function isCommandName(name) {
4
+ return commandNames.some((command) => command === name);
5
+ }
6
+
7
+ // src/parse-duration.ts
8
+ var durationPattern = /^(?<count>[1-9]\d*)(?<unit>ms|s)$/u;
9
+ function parseDuration(text) {
10
+ const groups = durationPattern.exec(text)?.groups;
11
+ if (groups === undefined) {
12
+ return null;
13
+ }
14
+ const count = Number(groups["count"]);
15
+ return groups["unit"] === "s" ? count * 1000 : count;
16
+ }
17
+
18
+ // src/standard-lanes.ts
19
+ var standardLanes = [
20
+ { name: "unit", suffix: null, limitMs: 5000, parallel: "workers" },
21
+ { name: "e2e", suffix: null, limitMs: 15000, parallel: 1 },
22
+ { name: "browser", suffix: ".browser", limitMs: 1e4, parallel: 1 },
23
+ { name: "docker", suffix: ".docker", limitMs: 15000, parallel: 1 },
24
+ { name: "host", suffix: ".host", limitMs: 15000, parallel: 1 },
25
+ { name: "agent-sdk", suffix: ".agent-sdk", limitMs: 5000, parallel: 1 },
26
+ { name: "model-api", suffix: ".model-api", limitMs: 120000, parallel: 1 }
27
+ ];
28
+
29
+ // src/define-rig.ts
30
+ var laneNamePattern = /^[a-z][a-z0-9-]*$/u;
31
+ function defineRig(declaration) {
32
+ const declared = Object.entries(declaration.lanes ?? {});
33
+ for (const [name] of declared) {
34
+ if (!laneNamePattern.test(name)) {
35
+ throw new Error(`rig.config.ts: the lane name "${name}" must be lowercase letters, digits and dashes, starting with a letter`);
36
+ }
37
+ if (isCommandName(name)) {
38
+ throw new Error(`rig.config.ts: the lane name "${name}" is a rig command, so \`rig ${name}\` could never run the lane`);
39
+ }
40
+ }
41
+ const lanes = [
42
+ ...standardLanes.map((standard) => resolveStandardLane(standard, declaration.lanes?.[standard.name])),
43
+ ...declared.filter(([name]) => !standardLanes.some((standard) => standard.name === name)).map(([name, laneDeclaration]) => resolveProjectLane(name, laneDeclaration))
44
+ ];
45
+ assertDistinctSuffixes(lanes);
46
+ return { lanes };
47
+ }
48
+ function resolveStandardLane(standard, declaration) {
49
+ const declared = declaration ?? {};
50
+ assertDirectoryLaneDeclaration(standard, declared);
51
+ if (standard.name === "browser" && (declared.start !== undefined || declared.stop !== undefined)) {
52
+ throw new Error('rig.config.ts: lane "browser" declares a start or a stop, but the browser lane has its hooks built in');
53
+ }
54
+ return {
55
+ name: standard.name,
56
+ suffix: declared.suffix === undefined ? standard.suffix : readSuffix(standard.name, declared.suffix),
57
+ limitMs: declared.limit === undefined ? standard.limitMs : readLimit(standard.name, declared.limit),
58
+ parallel: standard.parallel,
59
+ memory: readMemory(standard.name, declared.memory),
60
+ start: readModulePath(standard.name, "start", declared.start),
61
+ stop: readModulePath(standard.name, "stop", declared.stop)
62
+ };
63
+ }
64
+ var directoryLaneFields = new Map([
65
+ ["unit", ["suffix", "limit", "start", "stop"]],
66
+ ["e2e", ["suffix"]]
67
+ ]);
68
+ function assertDirectoryLaneDeclaration(standard, declaration) {
69
+ for (const field of directoryLaneFields.get(standard.name) ?? []) {
70
+ if (declaration[field] !== undefined) {
71
+ throw new Error(`rig.config.ts: lane "${standard.name}" declares ${field}, which the ${standard.name} lane does not take`);
72
+ }
73
+ }
74
+ }
75
+ var suffixPattern = /^\.[a-z0-9][a-z0-9-]*$/u;
76
+ var directoryLaneNames = standardLanes.filter((standard) => standard.suffix === null).map((standard) => standard.name);
77
+ function readSuffix(lane, suffix) {
78
+ if (!suffixPattern.test(suffix)) {
79
+ throw new Error(`rig.config.ts: lane "${lane}" has the suffix "${suffix}", which must be a dot and then lowercase letters, digits and dashes`);
80
+ }
81
+ const directoryLane = directoryLaneNames.find((name) => suffix === `.${name}`);
82
+ if (directoryLane !== undefined) {
83
+ throw new Error(`rig.config.ts: lane "${lane}" has the suffix "${suffix}", which the ${directoryLane} lane reserves, because the lint rules key that lane by its name`);
84
+ }
85
+ return suffix;
86
+ }
87
+ function readLimit(lane, limit) {
88
+ const limitMs = parseDuration(limit);
89
+ if (limitMs === null) {
90
+ throw new Error(`rig.config.ts: lane "${lane}" has the limit "${limit}", which is no duration such as '15s' or '1500ms'`);
91
+ }
92
+ return limitMs;
93
+ }
94
+ var memoryPattern = /^[1-9]\d*[KMG]?$/u;
95
+ function readMemory(lane, memory) {
96
+ if (memory === undefined) {
97
+ return null;
98
+ }
99
+ if (!memoryPattern.test(memory)) {
100
+ throw new Error(`rig.config.ts: lane "${lane}" has the memory "${memory}", which is no budget such as '512M' or '8G'`);
101
+ }
102
+ return memory;
103
+ }
104
+ function readModulePath(lane, field, path) {
105
+ if (path === undefined) {
106
+ return null;
107
+ }
108
+ if (path === "" || path.startsWith("/")) {
109
+ throw new Error(`rig.config.ts: lane "${lane}" has the ${field} "${path}", which must be a path relative to the repo root`);
110
+ }
111
+ return path;
112
+ }
113
+ function resolveProjectLane(name, declaration) {
114
+ if (declaration.limit === undefined) {
115
+ throw new Error(`rig.config.ts: lane "${name}" needs a limit, such as '15s'`);
116
+ }
117
+ return {
118
+ name,
119
+ suffix: declaration.suffix === undefined ? `.${name}` : readSuffix(name, declaration.suffix),
120
+ limitMs: readLimit(name, declaration.limit),
121
+ parallel: 1,
122
+ memory: readMemory(name, declaration.memory),
123
+ start: readModulePath(name, "start", declaration.start),
124
+ stop: readModulePath(name, "stop", declaration.stop)
125
+ };
126
+ }
127
+ function assertDistinctSuffixes(lanes) {
128
+ const owners = new Map;
129
+ for (const lane of lanes) {
130
+ if (lane.suffix === null) {
131
+ continue;
132
+ }
133
+ const owner = owners.get(lane.suffix);
134
+ if (owner !== undefined) {
135
+ throw new Error(`rig.config.ts: lanes "${owner}" and "${lane.name}" both use the suffix "${lane.suffix}"`);
136
+ }
137
+ owners.set(lane.suffix, lane.name);
138
+ }
139
+ }
140
+ // src/parse-rig-config.ts
141
+ function parseRigConfig(value) {
142
+ if (typeof value !== "object" || value === null || !("lanes" in value)) {
143
+ return { kind: "refused", reason: "the default export has no lanes" };
144
+ }
145
+ const lanes = value.lanes;
146
+ if (!Array.isArray(lanes)) {
147
+ return { kind: "refused", reason: "its lanes are no list" };
148
+ }
149
+ const resolved = [];
150
+ for (const [index, lane] of lanes.entries()) {
151
+ const parsed = parseResolvedLane(lane, index);
152
+ if (parsed.kind === "refused") {
153
+ return parsed;
154
+ }
155
+ resolved.push(parsed.lane);
156
+ }
157
+ return { kind: "parsed", config: { lanes: resolved } };
158
+ }
159
+ function parseResolvedLane(value, index) {
160
+ if (typeof value !== "object" || value === null) {
161
+ return { kind: "refused", reason: `lane ${index} is no object` };
162
+ }
163
+ const name = Reflect.get(value, "name");
164
+ const suffix = Reflect.get(value, "suffix");
165
+ const limitMs = Reflect.get(value, "limitMs");
166
+ const parallel = Reflect.get(value, "parallel");
167
+ const memory = Reflect.get(value, "memory");
168
+ const start = Reflect.get(value, "start");
169
+ const stop = Reflect.get(value, "stop");
170
+ if (typeof name !== "string") {
171
+ return { kind: "refused", reason: `lane ${index} has no name` };
172
+ }
173
+ if (!isNullableString(suffix)) {
174
+ return refuseField(name, "suffix", suffix);
175
+ }
176
+ if (typeof limitMs !== "number") {
177
+ return refuseField(name, "limitMs", limitMs);
178
+ }
179
+ if (!isParallelism(parallel)) {
180
+ return refuseField(name, "parallel", parallel);
181
+ }
182
+ if (!isNullableString(memory)) {
183
+ return refuseField(name, "memory", memory);
184
+ }
185
+ if (!isNullableString(start)) {
186
+ return refuseField(name, "start", start);
187
+ }
188
+ if (!isNullableString(stop)) {
189
+ return refuseField(name, "stop", stop);
190
+ }
191
+ return { kind: "parsed", lane: { name, suffix, limitMs, parallel, memory, start, stop } };
192
+ }
193
+ function isNullableString(value) {
194
+ return value === null || typeof value === "string";
195
+ }
196
+ function refuseField(lane, field, value) {
197
+ return {
198
+ kind: "refused",
199
+ reason: `lane "${lane}" has the ${field} ${JSON.stringify(value)}, which defineRig never resolves`
200
+ };
201
+ }
202
+ function isParallelism(value) {
203
+ return value === "workers" || typeof value === "number" && Number.isInteger(value) && value > 0;
204
+ }
205
+ // src/browser/run-in-page.ts
206
+ async function runInPage(view, pageFunction, argument, schema) {
207
+ const source = pageFunction.toString();
208
+ if (!isFunctionExpression(source)) {
209
+ throw new TypeError("runInPage takes an arrow function or a function expression, never a method or a bound function");
210
+ }
211
+ const call = argument === undefined ? `(${source})()` : `(${source})(JSON.parse(${JSON.stringify(formatArgumentJSON(argument))}))`;
212
+ const value = await view.evaluate(call);
213
+ const result = await schema["~standard"].validate(value);
214
+ if (result.issues !== undefined) {
215
+ throw new Error(`the page's result fails its schema: ${formatIssues(result.issues)}`, {
216
+ cause: result.issues
217
+ });
218
+ }
219
+ return result.value;
220
+ }
221
+ function isFunctionExpression(source) {
222
+ const pageCodeTranspiler = new Bun.Transpiler({ loader: "js" });
223
+ try {
224
+ pageCodeTranspiler.transformSync(`(${source});`);
225
+ return true;
226
+ } catch {
227
+ return false;
228
+ }
229
+ }
230
+ function formatArgumentJSON(argument) {
231
+ const json = JSON.stringify(argument);
232
+ if (typeof json !== "string" || !isJSONUnchanged(json, argument)) {
233
+ throw new TypeError("runInPage takes an argument that JSON carries to the page unchanged");
234
+ }
235
+ return json;
236
+ }
237
+ function isJSONUnchanged(json, argument) {
238
+ const parsed = JSON.parse(json);
239
+ return Bun.deepEquals(parsed, argument);
240
+ }
241
+ function formatIssues(issues) {
242
+ return issues.map((issue) => {
243
+ const path = (issue.path ?? []).map((segment) => formatPathSegment(segment)).join(".");
244
+ return path === "" ? issue.message : `${path}: ${issue.message}`;
245
+ }).join("; ");
246
+ }
247
+ function formatPathSegment(segment) {
248
+ const key = typeof segment === "object" ? segment.key : segment;
249
+ return String(key);
250
+ }
251
+ export {
252
+ defineRig,
253
+ parseRigConfig,
254
+ runInPage
255
+ };
@@ -0,0 +1,6 @@
1
+ export interface InitFiles {
2
+ readonly readJSON: (path: string) => Promise<unknown>;
3
+ readonly writeText: (path: string, text: string) => Promise<void>;
4
+ readonly probeFile: (path: string) => Promise<boolean>;
5
+ }
6
+ export declare const initFiles: InitFiles;
@@ -0,0 +1,3 @@
1
+ export declare const commandNames: readonly ['init', 'related'];
2
+ export type CommandName = (typeof commandNames)[number];
3
+ export declare function isCommandName(name: string): name is CommandName;
@@ -0,0 +1,13 @@
1
+ export type LaneEnvironment = Readonly<Record<string, string | undefined>>;
2
+ export type LaneStartOutcome = {
3
+ readonly kind: 'proceed';
4
+ readonly environment: LaneEnvironment;
5
+ } | {
6
+ readonly kind: 'finish';
7
+ readonly exitCode: number;
8
+ };
9
+ export interface LaneHooks {
10
+ readonly start: () => Promise<LaneStartOutcome>;
11
+ readonly run: (testArgs: readonly string[], environment: LaneEnvironment) => Promise<number>;
12
+ readonly stop: () => Promise<void>;
13
+ }
@@ -0,0 +1,12 @@
1
+ import type { LaneHooks } from './lane-hooks.ts';
2
+ import type { CommandOptions } from './run-command.ts';
3
+ import type { LaneContext } from './types.ts';
4
+ export interface RepoLaneOptions {
5
+ readonly context: LaneContext;
6
+ readonly dietScript: string;
7
+ readonly parallel: number;
8
+ readonly signal: AbortSignal;
9
+ readonly importModule: (path: string) => Promise<unknown>;
10
+ readonly runCommand: (command: readonly string[], options: CommandOptions) => Promise<number>;
11
+ }
12
+ export declare function makeRepoLaneHooks(options: RepoLaneOptions): LaneHooks;
@@ -0,0 +1 @@
1
+ export declare function parseDuration(text: string): number | null;
@@ -0,0 +1,3 @@
1
+ import type { LaneContext } from './types.ts';
2
+ export type LaneHook = (context: LaneContext) => Promise<unknown>;
3
+ export declare function parseLaneHook(loaded: unknown, path: string): LaneHook;
@@ -0,0 +1,2 @@
1
+ import type { LaneStart } from './types.ts';
2
+ export declare function parseLaneStart(value: unknown, path: string): LaneStart;
@@ -0,0 +1,9 @@
1
+ import type { RigConfig } from './types.ts';
2
+ export type ParsedRigConfig = {
3
+ readonly kind: 'parsed';
4
+ readonly config: RigConfig;
5
+ } | {
6
+ readonly kind: 'refused';
7
+ readonly reason: string;
8
+ };
9
+ export declare function parseRigConfig(value: unknown): ParsedRigConfig;
@@ -0,0 +1,5 @@
1
+ export interface TestArgs {
2
+ readonly flags: readonly string[];
3
+ readonly paths: readonly string[];
4
+ }
5
+ export declare function parseTestArgs(args: readonly string[]): TestArgs;
@@ -0,0 +1 @@
1
+ export declare function pickE2EDirs(trackedFiles: readonly string[]): readonly string[];
@@ -0,0 +1,2 @@
1
+ import type { ResolvedLane } from './types.ts';
2
+ export declare function pickLaneSelection(lane: ResolvedLane, e2eDirs: readonly string[]): readonly string[];
@@ -0,0 +1,5 @@
1
+ export interface CLIArgs {
2
+ readonly command: string;
3
+ readonly extraArgs: readonly string[];
4
+ }
5
+ export declare function readCLIArgs(argv: readonly string[]): Promise<CLIArgs | string>;
@@ -0,0 +1,8 @@
1
+ export interface MemoryBudgetSources {
2
+ readonly totalMemory: number;
3
+ readonly procRoot: string;
4
+ readonly cgroupRoot: string;
5
+ readonly readText: (path: string) => Promise<string | undefined>;
6
+ }
7
+ export declare function readMemoryBudget(sources: Readonly<MemoryBudgetSources>): Promise<number>;
8
+ export declare function readOptionalText(path: string): Promise<string | undefined>;
@@ -0,0 +1,6 @@
1
+ import type { RigConfig } from './types.ts';
2
+ export interface RigConfigSources {
3
+ readonly probeFile: (path: string) => boolean;
4
+ readonly importModule: (path: string) => Promise<unknown>;
5
+ }
6
+ export declare function readRigConfig(repoRoot: string, dependencies: RigConfigSources): Promise<RigConfig>;
@@ -0,0 +1 @@
1
+ export declare function readTrackedFiles(repoRoot: string): Promise<readonly string[]>;
@@ -0,0 +1,13 @@
1
+ import type { MemoryBudgetSources } from './read-memory-budget.ts';
2
+ export interface WorkerCountSources extends MemoryBudgetSources {
3
+ readonly env: Readonly<Record<string, string | undefined>>;
4
+ readonly cores: number;
5
+ }
6
+ export type WorkerCount = {
7
+ readonly kind: 'count';
8
+ readonly count: number;
9
+ } | {
10
+ readonly kind: 'invalid';
11
+ readonly message: string;
12
+ };
13
+ export declare function readWorkerCount(sources: Readonly<WorkerCountSources>): Promise<WorkerCount>;
@@ -0,0 +1 @@
1
+ export declare function resolveDietScript(): string;
@@ -0,0 +1,6 @@
1
+ import type { RigConfig } from './types.ts';
2
+ export interface RelatedTestSources {
3
+ readonly cwd: string;
4
+ readonly probeFile: (path: string) => boolean;
5
+ }
6
+ export declare function resolveRelatedTests(paths: readonly string[], config: RigConfig, sources: Readonly<RelatedTestSources>): string[];
@@ -0,0 +1,9 @@
1
+ import type { InitDeps } from './run-init.ts';
2
+ import type { LaneDeps } from './run-lane.ts';
3
+ import type { RelatedTestDeps } from './run-related-tests.ts';
4
+ import type { UnitDeps } from './run-unit.ts';
5
+ import type { RigConfig, RigRun } from './types.ts';
6
+ export interface CLIDeps extends LaneDeps, UnitDeps, RelatedTestDeps, InitDeps {
7
+ readonly readConfig: (repoRoot: string) => Promise<RigConfig>;
8
+ }
9
+ export declare function runCLI(argv: readonly string[], run: RigRun, deps: CLIDeps): Promise<number>;
@@ -0,0 +1,6 @@
1
+ export interface CommandOptions {
2
+ readonly cwd: string;
3
+ readonly environment: Readonly<Record<string, string | undefined>>;
4
+ readonly signal: AbortSignal;
5
+ }
6
+ export declare function runCommand(command: readonly string[], options: CommandOptions): Promise<number>;
@@ -0,0 +1,8 @@
1
+ import type { InitFiles } from './init-files.ts';
2
+ import type { RigConfig, RigOutput } from './types.ts';
3
+ export interface InitDeps {
4
+ readonly readTrackedFiles: (repoRoot: string) => Promise<readonly string[]>;
5
+ readonly initFiles: InitFiles;
6
+ readonly output: RigOutput;
7
+ }
8
+ export declare function runInit(repoRoot: string, config: RigConfig, deps: InitDeps): Promise<number>;
@@ -0,0 +1,2 @@
1
+ import type { LaneHooks } from './lane-hooks.ts';
2
+ export declare function runLaneHooks(testArgs: readonly string[], hooks: LaneHooks): Promise<number>;
@@ -0,0 +1,18 @@
1
+ import type { BrowserLaneWiring } from './browser/wire-browser-lane-hooks.ts';
2
+ import type { LaneHooks } from './lane-hooks.ts';
3
+ import type { CommandOptions } from './run-command.ts';
4
+ import type { ResolvedLane, RigConfig, RigOutput, RigRun } from './types.ts';
5
+ export interface LaneDeps {
6
+ readonly dietScript: string;
7
+ readonly output: RigOutput;
8
+ readonly readTrackedFiles: (repoRoot: string) => Promise<readonly string[]>;
9
+ readonly importModule: (path: string) => Promise<unknown>;
10
+ readonly runCommand: (command: readonly string[], options: CommandOptions) => Promise<number>;
11
+ readonly wireBrowserLaneHooks: (wiring: BrowserLaneWiring) => Promise<LaneHooks>;
12
+ }
13
+ export interface LaneRequest {
14
+ readonly config: RigConfig;
15
+ readonly lane: ResolvedLane;
16
+ readonly extraArgs: readonly string[];
17
+ }
18
+ export declare function runLane(request: LaneRequest, run: RigRun, deps: LaneDeps): Promise<number>;
@@ -0,0 +1,10 @@
1
+ import type { WorkerCount } from './read-worker-count.ts';
2
+ import type { RelatedTestSources } from './resolve-related-tests.ts';
3
+ import type { UnitRunDeps } from './run-unit-tests.ts';
4
+ import type { RigConfig, RigOutput, RigRun } from './types.ts';
5
+ export interface RelatedTestDeps extends UnitRunDeps {
6
+ readonly sources: Readonly<RelatedTestSources>;
7
+ readonly readWorkerCount: () => Promise<WorkerCount>;
8
+ readonly output: RigOutput;
9
+ }
10
+ export declare function runRelatedTests(args: readonly string[], config: RigConfig, run: RigRun, deps: Readonly<RelatedTestDeps>): Promise<number>;
@@ -0,0 +1,12 @@
1
+ import type { CommandOptions } from './run-command.ts';
2
+ import type { RigConfig, RigRun } from './types.ts';
3
+ export interface UnitRequest {
4
+ readonly workerCount: number;
5
+ readonly extraArgs: readonly string[];
6
+ }
7
+ export interface UnitRunDeps {
8
+ readonly dietScript: string;
9
+ readonly readTrackedFiles: (repoRoot: string) => Promise<readonly string[]>;
10
+ readonly runCommand: (command: readonly string[], options: CommandOptions) => Promise<number>;
11
+ }
12
+ export declare function runUnitTests(config: RigConfig, request: UnitRequest, run: RigRun, deps: UnitRunDeps): Promise<number>;
@@ -0,0 +1,8 @@
1
+ import type { WorkerCount } from './read-worker-count.ts';
2
+ import type { UnitRunDeps } from './run-unit-tests.ts';
3
+ import type { RigConfig, RigOutput, RigRun } from './types.ts';
4
+ export interface UnitDeps extends UnitRunDeps {
5
+ readonly readWorkerCount: () => Promise<WorkerCount>;
6
+ readonly output: RigOutput;
7
+ }
8
+ export declare function runUnit(config: RigConfig, extraArgs: readonly string[], run: RigRun, deps: UnitDeps): Promise<number>;
@@ -0,0 +1,8 @@
1
+ import type { LaneParallelism } from './types.ts';
2
+ export interface StandardLane {
3
+ readonly name: string;
4
+ readonly suffix: string | null;
5
+ readonly limitMs: number;
6
+ readonly parallel: LaneParallelism;
7
+ }
8
+ export declare const standardLanes: readonly StandardLane[];
@@ -0,0 +1,5 @@
1
+ export interface Clock {
2
+ readonly now: () => number;
3
+ readonly sleep: (durationMs: number) => Promise<void>;
4
+ }
5
+ export declare const systemClock: Clock;
@@ -0,0 +1,42 @@
1
+ export interface LaneDeclaration {
2
+ readonly suffix?: string;
3
+ readonly limit?: string;
4
+ readonly memory?: string;
5
+ readonly start?: string;
6
+ readonly stop?: string;
7
+ }
8
+ export interface RigDeclaration {
9
+ readonly lanes?: Readonly<Record<string, LaneDeclaration>>;
10
+ }
11
+ export type LaneParallelism = 'workers' | number;
12
+ export interface ResolvedLane {
13
+ readonly name: string;
14
+ readonly suffix: string | null;
15
+ readonly limitMs: number;
16
+ readonly parallel: LaneParallelism;
17
+ readonly memory: string | null;
18
+ readonly start: string | null;
19
+ readonly stop: string | null;
20
+ }
21
+ export interface RigConfig {
22
+ readonly lanes: readonly ResolvedLane[];
23
+ }
24
+ export interface LaneContext {
25
+ readonly repoRoot: string;
26
+ readonly lane: ResolvedLane;
27
+ readonly runID: string;
28
+ readonly environment: Readonly<Record<string, string | undefined>>;
29
+ }
30
+ export interface LaneStart {
31
+ readonly environment?: Readonly<Record<string, string>>;
32
+ }
33
+ export interface RigRun {
34
+ readonly repoRoot: string;
35
+ readonly environment: Readonly<Record<string, string | undefined>>;
36
+ readonly signal: AbortSignal;
37
+ readonly runID: string;
38
+ }
39
+ export interface RigOutput {
40
+ readonly out: (text: string) => void;
41
+ readonly err: (text: string) => void;
42
+ }
package/package.json CHANGED
@@ -1,17 +1,55 @@
1
1
  {
2
2
  "name": "@zgeoff/rig",
3
- "version": "0.0.0",
4
- "description": "Run a repo's test lanes, the browser lane included, under one command",
3
+ "version": "0.0.2",
4
+ "description": "Run every test lane of a repo from one declaration of its limits, with a browser lane built in",
5
+ "keywords": [
6
+ "bun",
7
+ "lanes",
8
+ "test"
9
+ ],
5
10
  "license": "MIT",
6
11
  "repository": {
7
12
  "type": "git",
8
13
  "url": "git+https://github.com/zgeoff/tools.git",
9
14
  "directory": "packages/rig"
10
15
  },
16
+ "bin": {
17
+ "rig": "./src/cli.ts"
18
+ },
11
19
  "files": [
12
- "README.md"
20
+ "src",
21
+ "!src/**/*.test.ts",
22
+ "dist"
13
23
  ],
24
+ "type": "module",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "bun": "./src/index.ts",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
14
32
  "publishConfig": {
15
33
  "access": "public"
34
+ },
35
+ "scripts": {
36
+ "build": "rm -rf dist && bun build src/index.ts --outdir dist --target node --packages external && tsc -p tsconfig.build.json",
37
+ "typecheck": "tsrs"
38
+ },
39
+ "dependencies": {
40
+ "@zgeoff/diet": "0.1.2",
41
+ "@zgeoff/std": "0.3.0",
42
+ "gunshi": "1.0.0-rc.1",
43
+ "ts-pattern": "5.9.2"
44
+ },
45
+ "devDependencies": {
46
+ "@types/bun": "1.4.3",
47
+ "tiny-invariant": "1.3.3",
48
+ "tsrs": "0.18.0-ts7.1.0-dev.20260929",
49
+ "typescript": "7.0.2",
50
+ "zod": "4.6.7"
51
+ },
52
+ "engines": {
53
+ "bun": ">=1.3"
16
54
  }
17
55
  }
@@ -0,0 +1,36 @@
1
+ // A cold Chrome start on a fresh CI runner can run past the test timeout, so the preload starts
2
+ // Chrome before the file's first test. Bun spawns one Chrome per process, on the options of the
3
+ // first view, and every later view in the file reuses it.
4
+
5
+ import { hostname } from 'node:os';
6
+ import { laneFiles } from './lane-files.ts';
7
+ import { startBrowser } from './start-browser.ts';
8
+
9
+ await startBrowser({
10
+ // The lane starts each test process in the repo's root.
11
+ repoRoot: process.cwd(),
12
+ hostName: hostname(),
13
+ environment: process.env,
14
+
15
+ // `bun test --parallel=1` stops each file's Chrome and loads this module again for the next
16
+ // file. A new Chrome on the last one's profile can start before the last one lets go of it.
17
+ profileName: crypto.randomUUID(),
18
+ readDirNames: laneFiles.readDirNames,
19
+ openView: openChromeView,
20
+ });
21
+
22
+ // Bun.WebView never removes the profile it makes under `bun test` (oven-sh/bun#45389), so the view
23
+ // takes one in the run's folder, which the lane removes. The sandbox needs user namespaces that a
24
+ // container lacks. A killed Chrome leaves its shared memory files, which go to the lane's TMPDIR.
25
+ async function openChromeView(profileDir: string): Promise<void> {
26
+ const view = new Bun.WebView({
27
+ backend: { type: 'chrome', url: false, argv: ['--no-sandbox', '--disable-dev-shm-usage'] },
28
+ dataStore: { directory: profileDir },
29
+ });
30
+
31
+ try {
32
+ await view.navigate('about:blank');
33
+ } finally {
34
+ view.close();
35
+ }
36
+ }