|
1 | 1 | import assert from "node:assert/strict"; |
2 | 2 | import { describe, it } from "node:test"; |
3 | 3 | import type { AgentResult } from "../types.js"; |
4 | | -import { analyzeConvergence, recommend } from "./convergence.js"; |
| 4 | +import { analyzeConvergence, copelandRecommend, recommend } from "./convergence.js"; |
5 | 5 |
|
6 | 6 | const DIFF_A = `diff --git a/a.ts b/a.ts |
7 | 7 | --- a/a.ts |
@@ -245,3 +245,146 @@ describe("recommend", () => { |
245 | 245 | assert.ok(score1.diffSizePoints < 10); |
246 | 246 | }); |
247 | 247 | }); |
| 248 | + |
| 249 | +describe("copelandRecommend", () => { |
| 250 | + it("returns null for no completed agents", () => { |
| 251 | + const agents = [makeAgent({ id: 1, status: "error", diff: "" })]; |
| 252 | + const result = copelandRecommend(agents, [], []); |
| 253 | + assert.equal(result.recommended, null); |
| 254 | + assert.deepEqual(result.scores, []); |
| 255 | + }); |
| 256 | + |
| 257 | + it("recommends the agent that dominates all criteria", () => { |
| 258 | + // Agent 1: passes tests, in larger convergence group, fewer files |
| 259 | + // Agent 2: fails tests, alone, more files |
| 260 | + const agents = [ |
| 261 | + makeAgent({ id: 1, diff: DIFF_A, filesChanged: ["a.ts"] }), |
| 262 | + makeAgent({ id: 2, diff: DIFF_B, filesChanged: ["b.ts", "c.ts"] }), |
| 263 | + ]; |
| 264 | + const tests = [ |
| 265 | + { agentId: 1, passed: true }, |
| 266 | + { agentId: 2, passed: false }, |
| 267 | + ]; |
| 268 | + const convergence = analyzeConvergence(agents); |
| 269 | + const result = copelandRecommend(agents, tests, convergence); |
| 270 | + |
| 271 | + assert.equal(result.recommended, 1); |
| 272 | + const score1 = result.scores.find((s) => s.agentId === 1); |
| 273 | + assert.ok(score1); |
| 274 | + assert.equal(score1.copelandTotal, 1); // wins the one pairwise matchup |
| 275 | + assert.ok(score1.testsWins > 0); |
| 276 | + }); |
| 277 | + |
| 278 | + it("all agents identical gives zero Copeland scores", () => { |
| 279 | + const agents = [ |
| 280 | + makeAgent({ id: 1, diff: DIFF_A, filesChanged: ["a.ts"] }), |
| 281 | + makeAgent({ id: 2, diff: DIFF_A, filesChanged: ["a.ts"] }), |
| 282 | + makeAgent({ id: 3, diff: DIFF_A, filesChanged: ["a.ts"] }), |
| 283 | + ]; |
| 284 | + const tests = [ |
| 285 | + { agentId: 1, passed: true }, |
| 286 | + { agentId: 2, passed: true }, |
| 287 | + { agentId: 3, passed: true }, |
| 288 | + ]; |
| 289 | + const convergence = analyzeConvergence(agents); |
| 290 | + const result = copelandRecommend(agents, tests, convergence); |
| 291 | + |
| 292 | + // All agents tie on every criterion — all Copeland scores should be 0 |
| 293 | + for (const score of result.scores) { |
| 294 | + assert.equal(score.copelandTotal, 0, `Agent #${score.agentId} should have Copeland score 0`); |
| 295 | + assert.equal(score.testsWins, 0); |
| 296 | + assert.equal(score.convergenceWins, 0); |
| 297 | + assert.equal(score.filesChangedWins, 0); |
| 298 | + } |
| 299 | + // Still recommends someone (first agent) |
| 300 | + assert.ok(result.recommended !== null); |
| 301 | + }); |
| 302 | + |
| 303 | + it("handles agents with different strengths on different criteria (non-transitive)", () => { |
| 304 | + // Agent 1: passes tests, many files, small group |
| 305 | + // Agent 2: fails tests, few files, large group |
| 306 | + // Agent 3: fails tests, many files, large group |
| 307 | + const agents = [ |
| 308 | + makeAgent({ id: 1, diff: DIFF_A, filesChanged: ["a.ts", "b.ts", "c.ts"] }), |
| 309 | + makeAgent({ id: 2, diff: DIFF_B, filesChanged: ["x.ts"] }), |
| 310 | + makeAgent({ id: 3, diff: DIFF_B, filesChanged: ["x.ts", "y.ts", "z.ts"] }), |
| 311 | + ]; |
| 312 | + const tests = [ |
| 313 | + { agentId: 1, passed: true }, |
| 314 | + { agentId: 2, passed: false }, |
| 315 | + { agentId: 3, passed: false }, |
| 316 | + ]; |
| 317 | + const convergence = analyzeConvergence(agents); |
| 318 | + const result = copelandRecommend(agents, tests, convergence); |
| 319 | + |
| 320 | + // Agent 1 vs Agent 2: tests(+1), convergence(-1), files(-1) → Agent 2 wins |
| 321 | + // Agent 1 vs Agent 3: tests(+1), convergence(-1), files(tie) → tie |
| 322 | + // Agent 2 vs Agent 3: tests(tie), convergence(tie), files(+1 for 2) → Agent 2 wins |
| 323 | + // So Agent 2 should have the best Copeland score |
| 324 | + assert.equal(result.recommended, 2); |
| 325 | + }); |
| 326 | + |
| 327 | + it("prefers agent with test pass when other criteria are tied", () => { |
| 328 | + const agents = [ |
| 329 | + makeAgent({ id: 1, diff: DIFF_A, filesChanged: ["a.ts"] }), |
| 330 | + makeAgent({ id: 2, diff: DIFF_A, filesChanged: ["a.ts"] }), |
| 331 | + ]; |
| 332 | + const tests = [ |
| 333 | + { agentId: 1, passed: true }, |
| 334 | + { agentId: 2, passed: false }, |
| 335 | + ]; |
| 336 | + const convergence = analyzeConvergence(agents); |
| 337 | + const result = copelandRecommend(agents, tests, convergence); |
| 338 | + |
| 339 | + assert.equal(result.recommended, 1); |
| 340 | + const score1 = result.scores.find((s) => s.agentId === 1); |
| 341 | + assert.ok(score1); |
| 342 | + assert.equal(score1.testsWins, 1); |
| 343 | + assert.equal(score1.copelandTotal, 1); |
| 344 | + }); |
| 345 | + |
| 346 | + it("prefers fewer files changed when other criteria are equal", () => { |
| 347 | + const agents = [ |
| 348 | + makeAgent({ id: 1, diff: DIFF_A, filesChanged: ["a.ts", "b.ts", "c.ts"] }), |
| 349 | + makeAgent({ id: 2, diff: DIFF_A, filesChanged: ["a.ts"] }), |
| 350 | + ]; |
| 351 | + const convergence = analyzeConvergence(agents); |
| 352 | + const result = copelandRecommend(agents, [], convergence); |
| 353 | + |
| 354 | + assert.equal(result.recommended, 2); |
| 355 | + }); |
| 356 | + |
| 357 | + it("returns per-agent criterion breakdowns", () => { |
| 358 | + const agents = [ |
| 359 | + makeAgent({ id: 1, diff: DIFF_A, filesChanged: ["a.ts"] }), |
| 360 | + makeAgent({ id: 2, diff: DIFF_B, filesChanged: ["b.ts", "c.ts"] }), |
| 361 | + ]; |
| 362 | + const tests = [ |
| 363 | + { agentId: 1, passed: true }, |
| 364 | + { agentId: 2, passed: false }, |
| 365 | + ]; |
| 366 | + const convergence = analyzeConvergence(agents); |
| 367 | + const result = copelandRecommend(agents, tests, convergence); |
| 368 | + |
| 369 | + assert.equal(result.scores.length, 2); |
| 370 | + const score1 = result.scores.find((s) => s.agentId === 1); |
| 371 | + const score2 = result.scores.find((s) => s.agentId === 2); |
| 372 | + assert.ok(score1); |
| 373 | + assert.ok(score2); |
| 374 | + |
| 375 | + // Score1 wins tests and files, score2 wins neither |
| 376 | + assert.equal(score1.testsWins, 1); |
| 377 | + assert.equal(score2.testsWins, -1); |
| 378 | + assert.equal(score1.filesChangedWins, 1); |
| 379 | + assert.equal(score2.filesChangedWins, -1); |
| 380 | + }); |
| 381 | + |
| 382 | + it("handles single agent", () => { |
| 383 | + const agents = [makeAgent({ id: 1, diff: DIFF_A })]; |
| 384 | + const result = copelandRecommend(agents, [], []); |
| 385 | + |
| 386 | + assert.equal(result.recommended, 1); |
| 387 | + assert.equal(result.scores.length, 1); |
| 388 | + assert.equal(result.scores[0]!.copelandTotal, 0); |
| 389 | + }); |
| 390 | +}); |
0 commit comments