Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/tools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ export function sleep(ms) {
* Retry a function with exponential backoff
*/
export async function retry(fn, maxAttempts = 3, baseDelay = 1000) {
if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {
throw new Error('retry maxAttempts must be a positive integer');
}
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fn();
Expand Down
24 changes: 23 additions & 1 deletion test/tools.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { fileURLToPath } from "node:url";
import { spawn } from "node:child_process";
import test from "node:test";

import { TOOLS, resolveTool, toolList } from "../src/tools.mjs";
import { TOOLS, resolveTool, retry, toolList } from "../src/tools.mjs";

const BIN = fileURLToPath(new URL("../bin/moshcode.mjs", import.meta.url));

Expand Down Expand Up @@ -204,3 +204,25 @@ test("moshcode install reports an Object.prototype name as unknown", async () =>
assert.match(result.stderr, /usage: moshcode install <engine\|tool>/);
assert.doesNotMatch(result.stderr, /TypeError/);
});

test("retry rejects non-positive attempt limits", async () => {
let calls = 0;

await assert.rejects(
retry(() => { calls++; }, 0, 1),
/maxAttempts must be a positive integer/,
);
assert.equal(calls, 0);
});

test("retry retries until a later attempt succeeds", async () => {
let calls = 0;
const result = await retry(() => {
calls++;
if (calls < 2) throw new Error("not yet");
return "ok";
}, 2, 1);

assert.equal(result, "ok");
assert.equal(calls, 2);
});
Loading