Skip to content

Commit eb93992

Browse files
committed
fix(moshscript): skip sleep during dry runs
1 parent d8206d1 commit eb93992

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/commands.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,16 @@ const COMMANDS = [
137137
summary: "pause for N milliseconds (blocking)",
138138
// Synchronous/blocking so it pauses inline in the simple no-`await` style:
139139
// `while (alive) { work(); sleep(1000); }` actually waits each iteration.
140-
run(_ctx, ...args) {
140+
run(ctx, ...args) {
141141
const raw = args[0] ?? 0;
142142
const ms = Number(raw);
143143
if (!Number.isFinite(ms) || ms < 0) {
144144
throw new Error(`moshscript: sleep(ms) requires a finite non-negative number, got ${JSON.stringify(raw)}`);
145145
}
146+
if (ctx.dryRun) {
147+
ctx.out(` ⏱ sleep(${ms}) → would pause for ${ms}ms`);
148+
return;
149+
}
146150
if (ms > 0) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
147151
},
148152
},

test/commands.test.mjs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,35 @@ test("stop() flips the ctx alive flag off", async () => {
6666
});
6767

6868
test("sleep accepts zero milliseconds (no-op, synchronous)", () => {
69-
assert.equal(verb("sleep")({}, 0), undefined);
69+
assert.equal(verb("sleep")({ dryRun: false }, 0), undefined);
7070
});
7171

7272
test("sleep throws synchronously on a negative duration", () => {
7373
assert.throws(
74-
() => verb("sleep")({}, -1),
74+
() => verb("sleep")({ dryRun: false }, -1),
7575
/sleep\(ms\) requires a finite non-negative number/
7676
);
7777
});
7878

79+
test("sleep in dry-run narrates without blocking", () => {
80+
const ctx = createCtx();
81+
const originalWait = Atomics.wait;
82+
let waited = false;
83+
Atomics.wait = () => {
84+
waited = true;
85+
return "timed-out";
86+
};
87+
88+
try {
89+
assert.equal(verb("sleep")(ctx, 60_000), undefined);
90+
} finally {
91+
Atomics.wait = originalWait;
92+
}
93+
94+
assert.equal(waited, false);
95+
assert.match(ctx.lines.join("\n"), /would pause for 60000ms/);
96+
});
97+
7998
test("the vocabulary exposes summaries for `moshcode commands`", () => {
8099
for (const cmd of moshVocabulary().all()) {
81100
assert.equal(typeof cmd.name, "string");

0 commit comments

Comments
 (0)