@sharedots/cli 0.1.0 → 0.1.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.
- package/dist/daemon-runner.d.ts +32 -0
- package/dist/daemon-runner.d.ts.map +1 -0
- package/dist/daemon-runner.js +132 -0
- package/dist/daemon-runner.js.map +1 -0
- package/dist/index.d.ts +1 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +292 -189
- package/dist/index.js.map +1 -1
- package/dist/job-runner.d.ts +81 -0
- package/dist/job-runner.d.ts.map +1 -0
- package/dist/job-runner.js +544 -0
- package/dist/job-runner.js.map +1 -0
- package/dist/parser.d.ts +5 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +81 -0
- package/dist/parser.js.map +1 -0
- package/dist/up-wizard.d.ts +21 -0
- package/dist/up-wizard.d.ts.map +1 -0
- package/dist/up-wizard.js +298 -0
- package/dist/up-wizard.js.map +1 -0
- package/package.json +4 -4
- package/src/cli.test.ts +50 -0
- package/src/daemon-runner.ts +165 -0
- package/src/index.ts +304 -193
- package/src/job-runner.test.ts +287 -0
- package/src/job-runner.ts +645 -0
- package/src/parser.ts +77 -0
- package/src/up-wizard.ts +339 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { existsSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
import type { ArtifactRef } from "@sharedots/protocol";
|
|
6
|
+
import {
|
|
7
|
+
buildPresetMatrixMultSpec,
|
|
8
|
+
buildPresetMonteCarloSpec,
|
|
9
|
+
buildPresetWasmSpec,
|
|
10
|
+
buildScriptJobSpec,
|
|
11
|
+
constructDashboardUrl,
|
|
12
|
+
openDashboardBridge,
|
|
13
|
+
runInteractiveJobRunner,
|
|
14
|
+
runScript,
|
|
15
|
+
} from "./job-runner.js";
|
|
16
|
+
|
|
17
|
+
describe("CLI Job Runner & Presets Generator", () => {
|
|
18
|
+
const dummyArtifact: ArtifactRef = {
|
|
19
|
+
artifactId: "art-test-12345",
|
|
20
|
+
sha256: "abc123def456",
|
|
21
|
+
sizeBytes: 1024,
|
|
22
|
+
filename: "workload.py",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
it("buildScriptJobSpec generates valid python JobSpecInput with partitions and artifacts", () => {
|
|
26
|
+
const spec = buildScriptJobSpec({
|
|
27
|
+
scriptName: "workload.py",
|
|
28
|
+
artifact: dummyArtifact,
|
|
29
|
+
partitions: 4,
|
|
30
|
+
args: ["--trials", "5000", "--mode", "fast"],
|
|
31
|
+
cpu: 2,
|
|
32
|
+
memory: "1GB",
|
|
33
|
+
gpu: true,
|
|
34
|
+
timeout: 120,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(spec.name).toBe("run-workload");
|
|
38
|
+
expect(spec.runtime).toBe("python");
|
|
39
|
+
expect(spec.execution.partitions).toBe(4);
|
|
40
|
+
expect(spec.resources.cpu).toBe(2);
|
|
41
|
+
expect(spec.resources.memory).toBe("1GB");
|
|
42
|
+
expect(spec.resources.gpu).toBe("required");
|
|
43
|
+
expect(spec.timeoutSec).toBe(120);
|
|
44
|
+
expect(spec.sharedArtifacts).toHaveLength(1);
|
|
45
|
+
expect(spec.sharedArtifacts[0]?.artifactId).toBe("art-test-12345");
|
|
46
|
+
expect(spec.tasks).toHaveLength(4);
|
|
47
|
+
|
|
48
|
+
const task0 = spec.tasks[0]!;
|
|
49
|
+
expect(task0.index).toBe(0);
|
|
50
|
+
expect(task0.entrypoint).toBe("workload.py");
|
|
51
|
+
expect(task0.args).toContain("--trials");
|
|
52
|
+
expect(task0.args).toContain("5000");
|
|
53
|
+
expect(task0.args).toContain("--partition");
|
|
54
|
+
expect(task0.args).toContain("0");
|
|
55
|
+
expect(task0.args).toContain("--partitions");
|
|
56
|
+
expect(task0.args).toContain("4");
|
|
57
|
+
expect(task0.inputs[0]?.artifactId).toBe("art-test-12345");
|
|
58
|
+
expect(task0.outputFilename).toBe("output_task_0.json");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("buildScriptJobSpec detects wasm runtime for .wasm files", () => {
|
|
62
|
+
const wasmArtifact: ArtifactRef = {
|
|
63
|
+
artifactId: "art-wasm-999",
|
|
64
|
+
sha256: "deadbeef",
|
|
65
|
+
sizeBytes: 2048,
|
|
66
|
+
filename: "compute.wasm",
|
|
67
|
+
};
|
|
68
|
+
const spec = buildScriptJobSpec({
|
|
69
|
+
scriptName: "compute.wasm",
|
|
70
|
+
artifact: wasmArtifact,
|
|
71
|
+
partitions: 2,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
expect(spec.name).toBe("run-compute");
|
|
75
|
+
expect(spec.runtime).toBe("wasm");
|
|
76
|
+
expect(spec.tasks).toHaveLength(2);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("buildPresetMonteCarloSpec generates distributed Monte Carlo sweep with seed distribution", () => {
|
|
80
|
+
const spec = buildPresetMonteCarloSpec({ trials: 50000, partitions: 5 });
|
|
81
|
+
expect(spec.name).toBe("monte-carlo-sweep-50000-trials");
|
|
82
|
+
expect(spec.runtime).toBe("python");
|
|
83
|
+
expect(spec.tasks).toHaveLength(5);
|
|
84
|
+
expect(spec.tasks[0]?.args).toContain("--trials");
|
|
85
|
+
expect(spec.tasks[0]?.args).toContain("10000");
|
|
86
|
+
expect(spec.tasks[0]?.args).toContain("--seed");
|
|
87
|
+
expect(spec.tasks[0]?.args).toContain("1000");
|
|
88
|
+
expect(spec.tasks[1]?.args).toContain("1042");
|
|
89
|
+
expect(spec.aggregation?.type).toBe("collect-json");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("buildPresetMatrixMultSpec generates block partitioned tasks", () => {
|
|
93
|
+
const spec = buildPresetMatrixMultSpec({ matrixSize: 2048, partitions: 4 });
|
|
94
|
+
expect(spec.name).toBe("distributed-matmul-2048x2048");
|
|
95
|
+
expect(spec.tasks).toHaveLength(4);
|
|
96
|
+
expect(spec.tasks[0]?.args).toContain("--size");
|
|
97
|
+
expect(spec.tasks[0]?.args).toContain("2048");
|
|
98
|
+
expect(spec.tasks[0]?.args).toContain("--row-start");
|
|
99
|
+
expect(spec.tasks[0]?.args).toContain("0");
|
|
100
|
+
expect(spec.tasks[0]?.args).toContain("--row-end");
|
|
101
|
+
expect(spec.tasks[0]?.args).toContain("512");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("buildPresetWasmSpec generates sandboxed wasm execution specs", () => {
|
|
105
|
+
const spec = buildPresetWasmSpec({ fuelLimit: 5000000, partitions: 3 });
|
|
106
|
+
expect(spec.name).toBe("wasm-sandboxed-compute-3-tasks");
|
|
107
|
+
expect(spec.runtime).toBe("wasm");
|
|
108
|
+
expect(spec.tasks).toHaveLength(3);
|
|
109
|
+
expect(spec.tasks[0]?.args).toContain("--fuel");
|
|
110
|
+
expect(spec.tasks[0]?.args).toContain("5000000");
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("Dashboard URL Construction & Control Bridge", () => {
|
|
115
|
+
it("constructs dashboard URL with room, token, and coordinator query parameters", () => {
|
|
116
|
+
const { localUrl, hostedUrl, effectiveUrl } = constructDashboardUrl({
|
|
117
|
+
coordinator: "https://sharedots.onrender.com",
|
|
118
|
+
roomId: "mesh-alpha",
|
|
119
|
+
token: "secret-token-xyz",
|
|
120
|
+
port: "3000",
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
expect(localUrl).toContain("http://localhost:3000/?");
|
|
124
|
+
expect(localUrl).toContain("room=mesh-alpha");
|
|
125
|
+
expect(localUrl).toContain("token=secret-token-xyz");
|
|
126
|
+
expect(localUrl).toContain("coordinator=https%3A%2F%2Fsharedots.onrender.com");
|
|
127
|
+
|
|
128
|
+
expect(hostedUrl).toContain("https://sharedots.onrender.com/?");
|
|
129
|
+
expect(hostedUrl).toContain("room=mesh-alpha");
|
|
130
|
+
expect(effectiveUrl).toBe(localUrl);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("supports hosted dashboard override", () => {
|
|
134
|
+
const { effectiveUrl } = constructDashboardUrl({
|
|
135
|
+
coordinator: "https://sharedots.onrender.com",
|
|
136
|
+
roomId: "room-hosted-1",
|
|
137
|
+
token: "sec-1",
|
|
138
|
+
hosted: true,
|
|
139
|
+
});
|
|
140
|
+
expect(effectiveUrl).toContain("https://sharedots.onrender.com/?");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("openDashboardBridge returns target URL without throwing when openBrowser is false", async () => {
|
|
144
|
+
const url = await openDashboardBridge({
|
|
145
|
+
coordinator: "https://sharedots.onrender.com",
|
|
146
|
+
roomId: "room-bridge-test",
|
|
147
|
+
token: "sec-bridge",
|
|
148
|
+
port: "9999",
|
|
149
|
+
openBrowser: false,
|
|
150
|
+
});
|
|
151
|
+
expect(url).toContain("room=room-bridge-test");
|
|
152
|
+
expect(url).toContain("token=sec-bridge");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("openDashboardBridge properly escapes ampersands on Windows when spawning browser", async () => {
|
|
156
|
+
const { spawnSync } = await import("node:child_process");
|
|
157
|
+
// Verify that Windows cmd.exe escape logic with ^& avoids command split
|
|
158
|
+
const testUrl = "http://localhost:3000/?room=mesh-1&token=sec-2&coordinator=https%3A%2F%2Fsharedots.onrender.com";
|
|
159
|
+
const escaped = testUrl.replace(/&/g, "^&");
|
|
160
|
+
expect(escaped).toContain("^&token=");
|
|
161
|
+
expect(escaped).toContain("^&coordinator=");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("constructDashboardUrl handles token isolation when explicit roomId is given", () => {
|
|
165
|
+
const { localUrl } = constructDashboardUrl({
|
|
166
|
+
roomId: "custom-isolated-room",
|
|
167
|
+
// no token provided, should not leak mismatched token
|
|
168
|
+
});
|
|
169
|
+
expect(localUrl).toContain("room=custom-isolated-room");
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe("runScript & runInteractiveJobRunner Lifecycle", () => {
|
|
174
|
+
it("runScript throws an Error when target script file does not exist", async () => {
|
|
175
|
+
await expect(
|
|
176
|
+
runScript({
|
|
177
|
+
scriptPath: "non_existent_file_xyz_12345.py",
|
|
178
|
+
}),
|
|
179
|
+
).rejects.toThrow("Script file not found");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("executes runScript with mock coordinator and streams progress", async () => {
|
|
183
|
+
const tmpScript = join(os.tmpdir(), `test_script_${Date.now()}.py`);
|
|
184
|
+
writeFileSync(tmpScript, `print("hello from test script")\n`, "utf8");
|
|
185
|
+
|
|
186
|
+
let progressCb: ((p: { percent: number; completedTasks: number; totalTasks: number }) => void) | null = null;
|
|
187
|
+
const mockJob = {
|
|
188
|
+
jobId: "job-mock-run-001",
|
|
189
|
+
on: vi.fn((event: string, cb: any) => {
|
|
190
|
+
if (event === "progress") progressCb = cb;
|
|
191
|
+
}),
|
|
192
|
+
result: vi.fn(async () => {
|
|
193
|
+
progressCb?.({ percent: 100, completedTasks: 2, totalTasks: 2 });
|
|
194
|
+
return {
|
|
195
|
+
jobId: "job-mock-run-001",
|
|
196
|
+
status: "completed",
|
|
197
|
+
resultArtifactId: "art-out-123",
|
|
198
|
+
};
|
|
199
|
+
}),
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const mockRoom = {
|
|
203
|
+
roomId: "room-test-run",
|
|
204
|
+
uploadArtifact: vi.fn(async () => ({
|
|
205
|
+
artifactId: "art-script-1",
|
|
206
|
+
sha256: "dummy-sha",
|
|
207
|
+
sizeBytes: 120,
|
|
208
|
+
filename: "test.py",
|
|
209
|
+
})),
|
|
210
|
+
submit: vi.fn(async () => mockJob),
|
|
211
|
+
downloadArtifact: vi.fn(async () => Buffer.from(JSON.stringify({ success: true, result: 3.1415 }))),
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const { Sharedots } = await import("sharedots");
|
|
215
|
+
const createRoomSpy = vi.spyOn(Sharedots.prototype, "createRoom").mockResolvedValue({
|
|
216
|
+
roomId: "room-test-run",
|
|
217
|
+
secret: "sec-test",
|
|
218
|
+
name: "run-room",
|
|
219
|
+
createdAt: Date.now(),
|
|
220
|
+
});
|
|
221
|
+
const connectSpy = vi.spyOn(Sharedots.prototype, "connect").mockResolvedValue(mockRoom as any);
|
|
222
|
+
|
|
223
|
+
try {
|
|
224
|
+
await runScript({
|
|
225
|
+
scriptPath: tmpScript,
|
|
226
|
+
coordinator: "https://sharedots.onrender.com",
|
|
227
|
+
roomId: "room-test-run",
|
|
228
|
+
token: "sec-test",
|
|
229
|
+
partitions: 2,
|
|
230
|
+
args: ["--test-flag", "val"],
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
expect(mockRoom.uploadArtifact).toHaveBeenCalled();
|
|
234
|
+
expect(mockRoom.submit).toHaveBeenCalled();
|
|
235
|
+
expect(mockJob.result).toHaveBeenCalled();
|
|
236
|
+
expect(mockRoom.downloadArtifact).toHaveBeenCalledWith("art-out-123");
|
|
237
|
+
} finally {
|
|
238
|
+
createRoomSpy.mockRestore();
|
|
239
|
+
connectSpy.mockRestore();
|
|
240
|
+
if (existsSync(tmpScript)) {
|
|
241
|
+
unlinkSync(tmpScript);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("executes runInteractiveJobRunner non-interactively with preset 1", async () => {
|
|
247
|
+
const mockJob = {
|
|
248
|
+
jobId: "job-mock-interactive-002",
|
|
249
|
+
on: vi.fn(),
|
|
250
|
+
result: vi.fn(async () => ({
|
|
251
|
+
jobId: "job-mock-interactive-002",
|
|
252
|
+
status: "completed",
|
|
253
|
+
resultArtifactId: "art-mc-res",
|
|
254
|
+
})),
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const mockRoom = {
|
|
258
|
+
roomId: "room-interactive-test",
|
|
259
|
+
uploadArtifact: vi.fn(async () => ({
|
|
260
|
+
artifactId: "art-preset-1",
|
|
261
|
+
sha256: "sha-preset",
|
|
262
|
+
sizeBytes: 500,
|
|
263
|
+
filename: "monte_carlo.py",
|
|
264
|
+
})),
|
|
265
|
+
submit: vi.fn(async () => mockJob),
|
|
266
|
+
downloadArtifact: vi.fn(async () => Buffer.from("simulation output")),
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const { Sharedots } = await import("sharedots");
|
|
270
|
+
const connectSpy = vi.spyOn(Sharedots.prototype, "connect").mockResolvedValue(mockRoom as any);
|
|
271
|
+
|
|
272
|
+
try {
|
|
273
|
+
await runInteractiveJobRunner({
|
|
274
|
+
coordinator: "https://sharedots.onrender.com",
|
|
275
|
+
roomId: "room-interactive-test",
|
|
276
|
+
token: "token-interactive",
|
|
277
|
+
preset: "1",
|
|
278
|
+
partitions: 2,
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
expect(mockRoom.submit).toHaveBeenCalled();
|
|
282
|
+
expect(mockJob.result).toHaveBeenCalled();
|
|
283
|
+
} finally {
|
|
284
|
+
connectSpy.mockRestore();
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
});
|