Skip to content
Open
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
9 changes: 8 additions & 1 deletion src/router/tokenSaver/parseTier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ export function parseTier(judgeOutput: string, knownTiers: string[]): string | u
if (found) return found;
}

for (const tier of knownTiers) {
const exact = knownTiers.find(t => t.toLowerCase() === cleaned.toLowerCase());
if (exact) return exact;

// Longest tiers first: `-` is a word boundary, so a shorter tier that is a
// word-prefix of a hyphenated tier (e.g. "fast" vs "fast-pro") would
// otherwise match inside the longer name.
const byLength = [...knownTiers].sort((a, b) => b.length - a.length);
for (const tier of byLength) {
const pattern = new RegExp(`\\b${escapeRegex(tier)}\\b`, "i");
if (pattern.test(cleaned)) {
return tier;
Expand Down
29 changes: 29 additions & 0 deletions tests/router/parseTier.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import assert from "node:assert/strict";
import test from "node:test";

import { parseTier } from "../../src/router/tokenSaver/parseTier.js";

test("prefers an exact tier over a word-prefix of a hyphenated tier", () => {
assert.equal(parseTier("a-a", ["a", "a-a"]), "a-a");
assert.equal(parseTier("fast-pro", ["fast", "fast-pro"]), "fast-pro");
});

test("keeps exact matching case-insensitive", () => {
assert.equal(parseTier("Fast-Pro", ["fast", "fast-pro"]), "fast-pro");
});

test("tagged output still resolves the longer tier", () => {
assert.equal(parseTier("<tier>a-a</tier>", ["a", "a-a"]), "a-a");
});

test("fallback text matching picks the longest tier mentioned", () => {
assert.equal(parseTier("please use a-a", ["a", "a-a"]), "a-a");
});

test("fallback text matching still resolves a plain tier mention", () => {
assert.equal(parseTier("the answer is fast", ["fast", "fast-pro"]), "fast");
});

test("returns undefined when no tier matches", () => {
assert.equal(parseTier("no such tier", ["fast", "fast-pro"]), undefined);
});