|
| 1 | +import { mkdir, writeFile } from "node:fs/promises"; |
| 2 | +import { join, resolve } from "node:path"; |
| 3 | + |
| 4 | +import { fc, it } from "@fast-check/vitest"; |
| 5 | +import { describe, expect } from "vitest"; |
| 6 | + |
| 7 | +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; |
| 8 | + |
| 9 | +import { |
| 10 | + allocateByDuration, |
| 11 | + parseTestDurations, |
| 12 | + readTestDurations, |
| 13 | +} from "../../ciTestSequencer.js"; |
| 14 | + |
| 15 | +describe("CI test history", () => { |
| 16 | + it("uses only successful in-root file durations", () => { |
| 17 | + const root = resolve("/workspace/rea"); |
| 18 | + const durations = parseTestDurations( |
| 19 | + JSON.stringify({ |
| 20 | + success: true, |
| 21 | + numFailedTests: 0, |
| 22 | + testResults: [ |
| 23 | + { |
| 24 | + name: join(root, "tests/slow.test.ts"), |
| 25 | + startTime: 10, |
| 26 | + endTime: 110, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: join(root, "tests/slow.test.ts"), |
| 30 | + startTime: 20, |
| 31 | + endTime: 80, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: resolve(root, "../outside.test.ts"), |
| 35 | + startTime: 10, |
| 36 | + endTime: 1000, |
| 37 | + }, |
| 38 | + null, |
| 39 | + ], |
| 40 | + }), |
| 41 | + root, |
| 42 | + ); |
| 43 | + |
| 44 | + expect(durations).toEqual(new Map([["tests/slow.test.ts", 100]])); |
| 45 | + expect( |
| 46 | + parseTestDurations('{"success": false, "numFailedTests": 1}', root), |
| 47 | + ).toBeUndefined(); |
| 48 | + expect(parseTestDurations("not json", root)).toBeUndefined(); |
| 49 | + expect(parseTestDurations("null", root)).toBeUndefined(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("falls back for missing history while preserving real filesystem errors", async () => { |
| 53 | + const temporaryRoot = await createTestTempDirectory("rea-ci-sequencer-"); |
| 54 | + const missing = await readTestDurations( |
| 55 | + join(temporaryRoot, "missing.json"), |
| 56 | + temporaryRoot, |
| 57 | + ); |
| 58 | + expect(missing).toBeUndefined(); |
| 59 | + |
| 60 | + const directory = join(temporaryRoot, "directory"); |
| 61 | + await mkdir(directory); |
| 62 | + const reportPath = join(temporaryRoot, "report.json"); |
| 63 | + await writeFile(reportPath, "{"); |
| 64 | + expect(await readTestDurations(reportPath, temporaryRoot)).toBeUndefined(); |
| 65 | + |
| 66 | + await expect( |
| 67 | + readTestDurations(directory, temporaryRoot), |
| 68 | + ).rejects.toMatchObject({ |
| 69 | + code: "EISDIR", |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe("duration shard allocation", () => { |
| 75 | + it.prop([ |
| 76 | + fc.uniqueArray(fc.integer({ min: 0, max: 10_000 }), { |
| 77 | + minLength: 1, |
| 78 | + maxLength: 80, |
| 79 | + }), |
| 80 | + fc.integer({ min: 3, max: 8 }), |
| 81 | + ])( |
| 82 | + "is complete, disjoint, reference-preserving, and input-order independent", |
| 83 | + (ids, shardCount) => { |
| 84 | + const items = ids.map((id, index) => ({ |
| 85 | + id, |
| 86 | + path: `tests/${id}.test.ts`, |
| 87 | + project: `project-${id % 3}`, |
| 88 | + pool: id % 2 === 0 ? "forks" : "threads", |
| 89 | + duration: index > 0 && id % 5 === 0 ? undefined : (id % 17) + 1, |
| 90 | + })); |
| 91 | + const specifications = items.map((item) => ({ |
| 92 | + item, |
| 93 | + key: `${item.path}\0${item.project}\0${item.pool}`, |
| 94 | + duration: item.duration, |
| 95 | + })); |
| 96 | + const shards = allocateByDuration(specifications, shardCount); |
| 97 | + const reversedShards = allocateByDuration( |
| 98 | + [...specifications].reverse(), |
| 99 | + shardCount, |
| 100 | + ); |
| 101 | + |
| 102 | + expect(shards).toBeDefined(); |
| 103 | + expect(reversedShards).toEqual(shards); |
| 104 | + const assigned = shards!.flat(); |
| 105 | + expect(assigned).toHaveLength(items.length); |
| 106 | + expect(new Set(assigned).size).toBe(items.length); |
| 107 | + expect(assigned.every((item) => items.includes(item))).toBe(true); |
| 108 | + }, |
| 109 | + ); |
| 110 | + |
| 111 | + it("keeps tied path, project, pool, and new-file entries deterministic", () => { |
| 112 | + const items = [ |
| 113 | + { |
| 114 | + id: "a", |
| 115 | + path: "tests/shared.test.ts", |
| 116 | + project: "alpha", |
| 117 | + pool: "forks", |
| 118 | + duration: 10, |
| 119 | + }, |
| 120 | + { |
| 121 | + id: "b", |
| 122 | + path: "tests/shared.test.ts", |
| 123 | + project: "beta", |
| 124 | + pool: "forks", |
| 125 | + duration: 10, |
| 126 | + }, |
| 127 | + { |
| 128 | + id: "c", |
| 129 | + path: "tests/shared.test.ts", |
| 130 | + project: "beta", |
| 131 | + pool: "threads", |
| 132 | + duration: 10, |
| 133 | + }, |
| 134 | + { |
| 135 | + id: "new", |
| 136 | + path: "tests/new.test.ts", |
| 137 | + project: "alpha", |
| 138 | + pool: "forks", |
| 139 | + duration: undefined, |
| 140 | + }, |
| 141 | + ]; |
| 142 | + const specifications = items.map((item) => ({ |
| 143 | + item, |
| 144 | + key: `${item.path}\0${item.project}\0${item.pool}`, |
| 145 | + duration: item.duration, |
| 146 | + })); |
| 147 | + const shards = allocateByDuration(specifications, 3); |
| 148 | + const reversedShards = allocateByDuration([...specifications].reverse(), 3); |
| 149 | + |
| 150 | + expect(shards).toBeDefined(); |
| 151 | + expect(reversedShards).toEqual(shards); |
| 152 | + expect(shards!.flat()).toHaveLength(items.length); |
| 153 | + expect(new Set(shards!.flat()).size).toBe(items.length); |
| 154 | + }); |
| 155 | +}); |
0 commit comments