Skip to content
Closed
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
1 change: 1 addition & 0 deletions packages/gittensory-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// focus-manifest parse/compile core, duplicate-winner adjudication, and their engine-parity fixtures).
// More modules land in follow-up issues.
export {
pickTopRankedOpportunities,
rankOpportunityScore,
rankOpportunities,
type OpportunityRankInput,
Expand Down
14 changes: 14 additions & 0 deletions packages/gittensory-engine/src/opportunity-ranker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ export function rankOpportunities<T>(
.sort((a, b) => b.rankScore - a.rankScore || a.index - b.index)
.map(({ candidate, rankScore }) => ({ ...candidate, rankScore }));
}

/**
* Rank candidates and return the top `limit` entries. Non-finite or negative limits return an empty list.
* Pure — delegates to {@link rankOpportunities} for ordering and tie-breaking.
*/
export function pickTopRankedOpportunities<T>(
candidates: Array<T & OpportunityRankInput>,
limit: number,
): Array<Omit<T, "rankScore"> & OpportunityRankInput & { rankScore: number }> {
if (!Number.isFinite(limit)) return [];
const safeLimit = Math.max(0, Math.trunc(limit));
if (safeLimit === 0 || candidates.length === 0) return [];
return rankOpportunities(candidates).slice(0, safeLimit);
}
19 changes: 18 additions & 1 deletion packages/gittensory-engine/test/opportunity-ranker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
// (dist/index.js) so the export contract itself is exercised. Pure module — no network, never flakes.
import { test } from "node:test";
import assert from "node:assert/strict";
import { rankOpportunityScore, rankOpportunities } from "../dist/index.js";
import { rankOpportunityScore, rankOpportunities, pickTopRankedOpportunities } from "../dist/index.js";

test("barrel: the public entrypoint re-exports the ranker API", () => {
assert.equal(typeof rankOpportunityScore, "function");
assert.equal(typeof rankOpportunities, "function");
assert.equal(typeof pickTopRankedOpportunities, "function");
});

const full = { potential: 1, feasibility: 1, laneFit: 1, freshness: 1, dupRisk: 0 };
Expand Down Expand Up @@ -103,3 +104,19 @@ test("rankOpportunities: a stale rankScore on the input is overwritten with the
test("rankOpportunities: an empty list ranks to an empty list", () => {
assert.deepEqual(rankOpportunities([]), []);
});

test("pickTopRankedOpportunities: returns the top N ranked candidates", () => {
const candidates = [
{ id: "low", potential: 0.2, feasibility: 1, laneFit: 1, freshness: 1, dupRisk: 0 },
{ id: "high", potential: 0.9, feasibility: 1, laneFit: 1, freshness: 1, dupRisk: 0 },
{ id: "mid", potential: 0.5, feasibility: 1, laneFit: 1, freshness: 1, dupRisk: 0 },
];
const topTwo = pickTopRankedOpportunities(candidates, 2);
assert.deepEqual(topTwo.map((entry) => entry.id), ["high", "mid"]);
});

test("pickTopRankedOpportunities: rejects non-finite limits", () => {
const candidates = [{ id: "only", ...full }];
assert.deepEqual(pickTopRankedOpportunities(candidates, Number.NaN), []);
assert.deepEqual(pickTopRankedOpportunities(candidates, Number.POSITIVE_INFINITY), []);
});
51 changes: 50 additions & 1 deletion test/unit/opportunity-ranker.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it } from "vitest";
import { rankOpportunities, rankOpportunityScore, type OpportunityRankInput } from "../../packages/gittensory-engine/src/opportunity-ranker";
import { rankOpportunityScore, rankOpportunities, pickTopRankedOpportunities } from "../../packages/gittensory-engine/src/opportunity-ranker";

// A neutral, all-passing candidate (every factor 1, no contention → score 1); tests override one field at a time.
function input(over: Partial<OpportunityRankInput> = {}): OpportunityRankInput {

Check failure on line 5 in test/unit/opportunity-ranker.test.ts

View workflow job for this annotation

GitHub Actions / validate-code

Cannot find name 'OpportunityRankInput'.

Check failure on line 5 in test/unit/opportunity-ranker.test.ts

View workflow job for this annotation

GitHub Actions / validate-code

Cannot find name 'OpportunityRankInput'.
return { potential: 1, feasibility: 1, laneFit: 1, freshness: 1, dupRisk: 0, ...over };
}

Expand Down Expand Up @@ -67,7 +67,7 @@
// Property table: the ranker is pure and forbids randomness, so instead of seeded random draws this pins a fixed
// set of varied inputs with independently hand-computed expected scores — deterministic and reproducible — so the
// product and clamp/fail-closed rules are exercised across the whole [0,1] range (and past its edges) at once.
const CASES: Array<{ in: OpportunityRankInput; score: number }> = [

Check failure on line 70 in test/unit/opportunity-ranker.test.ts

View workflow job for this annotation

GitHub Actions / validate-code

Cannot find name 'OpportunityRankInput'.
{ in: input(), score: 1 },
{ in: input({ dupRisk: 0.5 }), score: 0.5 },
{ in: input({ potential: 0.5 }), score: 0.5 },
Expand Down Expand Up @@ -137,3 +137,52 @@
expect(rankOpportunities([])).toEqual([]);
});
});

describe("pickTopRankedOpportunities", () => {
const candidates = [
{ id: "mid", ...input({ potential: 0.5 }) },
{ id: "top", ...input() },
{ id: "low", ...input({ freshness: 0.25 }) },
];

it("returns the highest-scoring candidates up to the limit", () => {
const topTwo = pickTopRankedOpportunities(candidates, 2);
expect(topTwo.map((entry) => entry.id)).toEqual(["top", "mid"]);
expect(topTwo.map((entry) => entry.rankScore)).toEqual([1, 0.5]);
});

it("returns every candidate when the limit exceeds the list size", () => {
expect(pickTopRankedOpportunities(candidates, 10).map((entry) => entry.id)).toEqual([
"top",
"mid",
"low",
]);
});

it("returns an empty array for a zero, negative, or non-finite limit", () => {
expect(pickTopRankedOpportunities(candidates, 0)).toEqual([]);
expect(pickTopRankedOpportunities(candidates, -1)).toEqual([]);
expect(pickTopRankedOpportunities(candidates, Number.NaN)).toEqual([]);
expect(pickTopRankedOpportunities(candidates, Number.POSITIVE_INFINITY)).toEqual([]);
});

it("returns an empty array for no candidates", () => {
expect(pickTopRankedOpportunities([], 3)).toEqual([]);
});

it("preserves rankOpportunities tie-breaking within the slice", () => {
const tie = input({ potential: 0.5 });
const tied = [
{ id: "first", ...tie },
{ id: "second", ...tie },
{ id: "winner", ...input() },
];
expect(pickTopRankedOpportunities(tied, 2).map((entry) => entry.id)).toEqual(["winner", "first"]);
});

it("is exported from the package barrel", async () => {
const barrel = await import("../../packages/gittensory-engine/src/index");
expect(typeof barrel.pickTopRankedOpportunities).toBe("function");
expect(barrel.pickTopRankedOpportunities(candidates, 1).map((entry) => entry.id)).toEqual(["top"]);
});
});
Loading