import { describe, expect, it } from "vitest"; import { cheapestOption, computeResearchCapacity, costScore, effectiveDurationTicks, eligibleTechs, isEligibleForBallot, prereqsMet, qualifyingFacilities, researchProgress, tallyBallot, ticksToMilliseconds, type EligibilityTech, type ResearchFacilityInput, } from "@/lib/tech/research"; const facility = ( overrides: Partial & { id: number }, ): ResearchFacilityInput => ({ operational: true, researcherHeadcount: 1, researchSpeedModifier: 0, researchSlots: 1, ...overrides, }); const tech = (overrides: Partial & { id: number }): EligibilityTech => ({ approvalStatus: "approved", prereqAnd: [], prereqOr: [], resourceCosts: [], ...overrides, }); describe("research capacity (facilities + researchers)", () => { it("only counts operational AND staffed facilities", () => { const facilities = [ facility({ id: 1, operational: false, researchSpeedModifier: 0.5 }), facility({ id: 2, researcherHeadcount: 0, researchSpeedModifier: 0.5 }), facility({ id: 3, researchSpeedModifier: 0.1, researchSlots: 2, researcherHeadcount: 4 }), ]; const qualifying = qualifyingFacilities(facilities); expect(qualifying.map((f) => f.id)).toEqual([3]); const capacity = computeResearchCapacity(facilities, 0.0001); expect(capacity.facilityCount).toBe(1); expect(capacity.totalResearchers).toBe(4); expect(capacity.slots).toBe(2); expect(capacity.facilitySpeedModifier).toBeCloseTo(0.1); expect(capacity.researcherSpeedBonus).toBeCloseTo(0.0004); expect(capacity.totalModifier).toBeCloseTo(0.1004); }); it("sums slots and modifiers across qualifying facilities", () => { const capacity = computeResearchCapacity( [ facility({ id: 1, researchSpeedModifier: 0.05, researchSlots: 1, researcherHeadcount: 2 }), facility({ id: 2, researchSpeedModifier: 0.1, researchSlots: 3, researcherHeadcount: 5 }), ], 0.001, ); expect(capacity.slots).toBe(4); expect(capacity.totalResearchers).toBe(7); expect(capacity.totalModifier).toBeCloseTo(0.05 + 0.1 + 0.007); }); it("zero researchers everywhere means zero slots", () => { const capacity = computeResearchCapacity( [facility({ id: 1, researcherHeadcount: 0 }), facility({ id: 2, operational: false })], 0.0001, ); expect(capacity.slots).toBe(0); expect(capacity.facilityCount).toBe(0); }); }); describe("effectiveDurationTicks", () => { it("scales the maximum by (1 - modifier) and floors at the minimum", () => { // base 100 ticks, 20% faster -> 80 expect( effectiveDurationTicks({ minimumResearchDuration: 10, maximumResearchDuration: 100 }, 0.2), ).toBe(80); // floor is the minimum, even for a huge modifier expect( effectiveDurationTicks({ minimumResearchDuration: 10, maximumResearchDuration: 100 }, 0.95), ).toBe(10); expect(effectiveDurationTicks({ minimumResearchDuration: 10, maximumResearchDuration: 100 }, 3)).toBe(10); }); it("uses the minimum as base when no maximum is set", () => { expect(effectiveDurationTicks({ minimumResearchDuration: 50, maximumResearchDuration: null }, 0.5)).toBe(50); expect(effectiveDurationTicks({ minimumResearchDuration: 50 }, 0)).toBe(50); }); it("rounds to whole ticks and tolerates junk input", () => { expect(effectiveDurationTicks({ minimumResearchDuration: 3, maximumResearchDuration: 10 }, 0.26)).toBe(7); expect(effectiveDurationTicks({}, 0.5)).toBe(1); expect(effectiveDurationTicks({ minimumResearchDuration: 10, maximumResearchDuration: 100 }, NaN)).toBe(100); }); it("ticksToMilliseconds honors the tick interval with a fallback", () => { expect(ticksToMilliseconds(288, 10)).toBe(48 * 60 * 60 * 1000); expect(ticksToMilliseconds(6, 0)).toBe(60 * 60 * 1000); }); }); describe("eligibility", () => { const researched = new Set([1]); const busy = new Set([3]); it("requires approved + unresearched + not busy + prereqs met", () => { expect(isEligibleForBallot(tech({ id: 2 }), { researchedIds: researched, busyIds: busy })).toBe(true); expect( isEligibleForBallot(tech({ id: 2, approvalStatus: "in_progress" }), { researchedIds: researched, busyIds: busy, }), ).toBe(false); expect(isEligibleForBallot(tech({ id: 1 }), { researchedIds: researched, busyIds: busy })).toBe(false); expect( isEligibleForBallot(tech({ id: 1, researchedAt: null }), { researchedIds: new Set(), busyIds: busy, }), ).toBe(true); expect(isEligibleForBallot(tech({ id: 3 }), { researchedIds: researched, busyIds: busy })).toBe(false); expect( isEligibleForBallot(tech({ id: 4, prereqAnd: [1, 9] }), { researchedIds: researched, busyIds: busy }), ).toBe(false); }); it("AND needs all, OR needs one when present", () => { expect(prereqsMet({ prereqAnd: [1, 2], prereqOr: [] }, new Set([1, 2]))).toBe(true); expect(prereqsMet({ prereqAnd: [1, 2], prereqOr: [] }, new Set([1]))).toBe(false); expect(prereqsMet({ prereqAnd: [], prereqOr: [1, 2] }, new Set([2]))).toBe(true); expect(prereqsMet({ prereqAnd: [], prereqOr: [1, 2] }, new Set([8]))).toBe(false); expect(prereqsMet({ prereqAnd: [1], prereqOr: [7] }, new Set([1]))).toBe(false); }); it("eligibleTechs filters the list", () => { const list = [ tech({ id: 1 }), tech({ id: 2, approvalStatus: "rejected" }), tech({ id: 3 }), tech({ id: 4, prereqAnd: [99] }), ]; expect( eligibleTechs(list, { researchedIds: new Set(), busyIds: new Set() }).map((t) => t.id), ).toEqual([1, 3]); }); }); describe("ballot tally + tie cascade", () => { const options = [ { technologyId: 10, tech: tech({ id: 10, resourceCosts: [{ resource: 1, amount: 5 }], minimumResearchDuration: 20, createdAt: "2026-01-01T00:00:00Z", }), }, { technologyId: 20, tech: tech({ id: 20, resourceCosts: [{ resource: 1, amount: 5 }], minimumResearchDuration: 20, createdAt: "2026-02-01T00:00:00Z", }), }, { technologyId: 30, tech: tech({ id: 30, resourceCosts: [{ resource: 1, amount: 2 }], minimumResearchDuration: 40, createdAt: "2026-03-01T00:00:00Z", }), }, ]; it("counts raw and intel-weighted votes", () => { const result = tallyBallot({ options, votes: [ { userId: 1, technologyId: 10 }, { userId: 2, technologyId: 20 }, { userId: 3, technologyId: 20 }, ], intelUserIds: new Set([1]), }); expect(result.counts[10]).toEqual({ raw: 1, weighted: 2 }); expect(result.counts[20]).toEqual({ raw: 2, weighted: 2 }); expect(result.totalVotes).toBe(3); // weighted tie 2-2 -> cheaper/shorter: 10 and 20 cost/duration equal, oldest wins expect(result.winnerId).toBe(10); expect(result.method).toBe("tie-cascade"); }); it("intel weight can flip the winner against raw count", () => { const result = tallyBallot({ options, votes: [ { userId: 1, technologyId: 10 }, { userId: 2, technologyId: 30 }, { userId: 3, technologyId: 30 }, ], intelUserIds: new Set([1]), }); // weighted: 10 -> 2, 30 -> 2; tie -> cheaper (30 costs 2 < 5) wins expect(result.winnerId).toBe(30); }); it("plain majority wins without cascade", () => { const result = tallyBallot({ options, votes: [ { userId: 1, technologyId: 10 }, { userId: 2, technologyId: 10 }, { userId: 3, technologyId: 30 }, ], intelUserIds: new Set(), }); expect(result.winnerId).toBe(10); expect(result.method).toBe("votes"); }); it("zero votes yields no winner", () => { const result = tallyBallot({ options, votes: [], intelUserIds: new Set() }); expect(result.winnerId).toBeNull(); expect(result.method).toBe("no-votes"); }); it("ignores votes for options not on the ballot", () => { const result = tallyBallot({ options, votes: [ { userId: 1, technologyId: 999 }, { userId: 2, technologyId: 30 }, ], intelUserIds: new Set(), }); expect(result.totalVotes).toBe(1); expect(result.winnerId).toBe(30); }); it("cheapestOption picks cheaper, then shorter, then oldest", () => { expect(cheapestOption(options)?.technologyId).toBe(30); // cost 2 beats 5 const sameCost = [ { technologyId: 11, tech: tech({ id: 11, resourceCosts: [{ resource: 1, amount: 5 }], minimumResearchDuration: 30, createdAt: "2026-05-01T00:00:00Z", }), }, { technologyId: 12, tech: tech({ id: 12, resourceCosts: [{ resource: 1, amount: 5 }], minimumResearchDuration: 10, createdAt: "2026-06-01T00:00:00Z", }), }, ]; expect(cheapestOption(sameCost)?.technologyId).toBe(12); // shorter base duration expect(costScore({ resourceCosts: [{ amount: 1 }, { amount: 2 }] })).toBe(3); expect(cheapestOption([])).toBeNull(); }); }); describe("researchProgress", () => { it("clamps progress between 0 and 1", () => { const start = Date.parse("2026-01-01T00:00:00Z"); const eta = start + 1000 * 60 * 60; expect(researchProgress(start, eta, start)).toBe(0); expect(researchProgress(start, eta, start + 1000 * 60 * 30)).toBeCloseTo(0.5); expect(researchProgress(start, eta, eta + 10_000)).toBe(1); expect(researchProgress(start, start, start)).toBe(0); expect(researchProgress("garbage", eta, start)).toBe(0); }); });