|
1 | | -import { afterEach, describe, expect, test } from "bun:test"; |
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
2 | 2 | import { mkdir, mkdtemp, writeFile, rm } from "node:fs/promises"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { join } from "node:path"; |
5 | 5 | import { execFile } from "node:child_process"; |
6 | 6 | import { promisify } from "node:util"; |
7 | 7 |
|
8 | | -import { initEvalGitRepo, parseArgs } from "./eval-capability.ts"; |
| 8 | +import { initEvalGitRepo, mapPool, parseArgs } from "./eval-capability.ts"; |
9 | 9 |
|
10 | 10 | const execFileAsync = promisify(execFile); |
11 | 11 |
|
12 | 12 | describe("parseArgs", () => { |
| 13 | + const savedConcurrency = process.env.CORBITS_EVAL_CONCURRENCY; |
| 14 | + |
| 15 | + const restoreConcurrency = (): void => { |
| 16 | + if (savedConcurrency === undefined) { |
| 17 | + delete process.env.CORBITS_EVAL_CONCURRENCY; |
| 18 | + } else { |
| 19 | + process.env.CORBITS_EVAL_CONCURRENCY = savedConcurrency; |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + restoreConcurrency(); |
| 25 | + }); |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + delete process.env.CORBITS_EVAL_CONCURRENCY; |
| 29 | + }); |
| 30 | + |
13 | 31 | test("--help does not require provider or model", () => { |
14 | 32 | const opts = parseArgs(["--help"]); |
15 | 33 | expect(opts.help).toBe(true); |
@@ -61,6 +79,77 @@ describe("parseArgs", () => { |
61 | 79 | expect(pair.provider).toBe("foo"); |
62 | 80 | expect(pair.model).toBe("bar"); |
63 | 81 | }); |
| 82 | + |
| 83 | + test("defaults concurrency to 1", () => { |
| 84 | + delete process.env.CORBITS_EVAL_CONCURRENCY; |
| 85 | + const opts = parseArgs(["--provider", "foo", "--model", "bar"]); |
| 86 | + expect(opts.concurrency).toBe(1); |
| 87 | + }); |
| 88 | + |
| 89 | + test("--concurrency 4 is accepted", () => { |
| 90 | + delete process.env.CORBITS_EVAL_CONCURRENCY; |
| 91 | + const opts = parseArgs(["--provider", "foo", "--model", "bar", "--concurrency", "4"]); |
| 92 | + expect(opts.concurrency).toBe(4); |
| 93 | + }); |
| 94 | + |
| 95 | + test("invalid --concurrency values throw", () => { |
| 96 | + const pair = ["--provider", "foo", "--model", "bar"] as const; |
| 97 | + expect(() => parseArgs([...pair, "--concurrency", "0"])).toThrow(/positive integer/); |
| 98 | + expect(() => parseArgs([...pair, "--concurrency", "-1"])).toThrow(/positive integer/); |
| 99 | + expect(() => parseArgs([...pair, "--concurrency", "1.5"])).toThrow(/positive integer/); |
| 100 | + expect(() => parseArgs([...pair, "--concurrency", "foo"])).toThrow(/positive integer/); |
| 101 | + }); |
| 102 | + |
| 103 | + test("CORBITS_EVAL_CONCURRENCY sets the default", () => { |
| 104 | + process.env.CORBITS_EVAL_CONCURRENCY = "3"; |
| 105 | + const opts = parseArgs(["--provider", "foo", "--model", "bar"]); |
| 106 | + expect(opts.concurrency).toBe(3); |
| 107 | + }); |
| 108 | + |
| 109 | + test("--concurrency overrides CORBITS_EVAL_CONCURRENCY", () => { |
| 110 | + process.env.CORBITS_EVAL_CONCURRENCY = "8"; |
| 111 | + const opts = parseArgs(["--provider", "foo", "--model", "bar", "--concurrency", "2"]); |
| 112 | + expect(opts.concurrency).toBe(2); |
| 113 | + }); |
| 114 | + |
| 115 | + test("invalid CORBITS_EVAL_CONCURRENCY throws", () => { |
| 116 | + process.env.CORBITS_EVAL_CONCURRENCY = "0"; |
| 117 | + expect(() => parseArgs(["--provider", "foo", "--model", "bar"])).toThrow( |
| 118 | + /CORBITS_EVAL_CONCURRENCY must be a positive integer/, |
| 119 | + ); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe("mapPool", () => { |
| 124 | + test("N overlapping jobs with concurrency N finish in ~one job duration", async () => { |
| 125 | + const jobMs = 80; |
| 126 | + const n = 4; |
| 127 | + const start = Date.now(); |
| 128 | + const results = await mapPool([0, 1, 2, 3], n, async (item) => { |
| 129 | + await new Promise((r) => setTimeout(r, jobMs)); |
| 130 | + return item; |
| 131 | + }); |
| 132 | + const elapsed = Date.now() - start; |
| 133 | + expect(results).toEqual([0, 1, 2, 3]); |
| 134 | + expect(elapsed).toBeLessThan(jobMs * 2); |
| 135 | + expect(elapsed).toBeGreaterThanOrEqual(jobMs - 20); |
| 136 | + }); |
| 137 | + |
| 138 | + test("preserves input order when later items finish first", async () => { |
| 139 | + const results = await mapPool([1, 2, 3], 3, async (item) => { |
| 140 | + await new Promise((r) => setTimeout(r, (4 - item) * 30)); |
| 141 | + return item; |
| 142 | + }); |
| 143 | + expect(results).toEqual([1, 2, 3]); |
| 144 | + }); |
| 145 | + |
| 146 | + test("empty input returns an empty array", async () => { |
| 147 | + expect(await mapPool([], 4, async (item) => item)).toEqual([]); |
| 148 | + }); |
| 149 | + |
| 150 | + test("rejects non-positive concurrency", async () => { |
| 151 | + await expect(mapPool([1], 0, async (item) => item)).rejects.toThrow(/positive integer/); |
| 152 | + }); |
64 | 153 | }); |
65 | 154 |
|
66 | 155 | describe("initEvalGitRepo", () => { |
|
0 commit comments