diff --git a/@omniroute/opencode-plugin/package.json b/@omniroute/opencode-plugin/package.json index fa373a83b6..a77cee5b93 100644 --- a/@omniroute/opencode-plugin/package.json +++ b/@omniroute/opencode-plugin/package.json @@ -23,7 +23,7 @@ "scripts": { "build": "tsup", "clean": "rm -rf dist", - "test": "node --import tsx/esm --test tests/scaffold.test.ts tests/auth.test.ts tests/options-schema.test.ts tests/multi-instance.test.ts tests/fetch-interceptor.test.ts tests/provider.test.ts tests/gemini-sanitize.test.ts tests/combos.test.ts tests/config-shim.test.ts tests/features.test.ts tests/feature-defaults.test.ts tests/usable-combo.test.ts tests/disk-snapshot-perms.test.ts tests/fork-features.test.ts tests/auto-combo-context.test.ts tests/provider-id-routing.test.ts tests/management-read-token.test.ts tests/auto-sync.test.ts tests/model-allowlist.test.ts tests/log-level.test.ts tests/effort-tier-variants.test.ts", + "test": "node --import tsx/esm --test tests/scaffold.test.ts tests/auth.test.ts tests/options-schema.test.ts tests/multi-instance.test.ts tests/fetch-interceptor.test.ts tests/provider.test.ts tests/gemini-sanitize.test.ts tests/combos.test.ts tests/config-shim.test.ts tests/features.test.ts tests/feature-defaults.test.ts tests/usable-combo.test.ts tests/disk-snapshot-perms.test.ts tests/fork-features.test.ts tests/auto-combo-context.test.ts tests/provider-id-routing.test.ts tests/management-read-token.test.ts tests/auto-sync.test.ts tests/model-allowlist.test.ts tests/log-level.test.ts tests/effort-tier-variants.test.ts tests/naming.test.ts", "prepublishOnly": "npm run clean && npm run build && npm test" }, "keywords": [ diff --git a/@omniroute/opencode-plugin/tests/naming.test.ts b/@omniroute/opencode-plugin/tests/naming.test.ts new file mode 100644 index 0000000000..4502bf6a19 --- /dev/null +++ b/@omniroute/opencode-plugin/tests/naming.test.ts @@ -0,0 +1,81 @@ +/** + * Tests for `formatFreeBudget` (@omniroute/opencode-plugin/src/naming.ts): + * formats a free-tier model's budget info into a short human-readable + * suffix, branching on `freeType`. + */ + +import { test } from "node:test"; +import assert from "node:assert/strict"; + +import { formatFreeBudget, type FreeModelFreeType } from "../src/naming.js"; + +test("formatFreeBudget: recurring-daily formats tokens/day", () => { + assert.equal( + formatFreeBudget({ freeType: "recurring-daily", monthlyTokens: 25_000_000 }), + "25M tokens/day" + ); +}); + +test("formatFreeBudget: recurring-monthly formats tokens/month", () => { + assert.equal( + formatFreeBudget({ freeType: "recurring-monthly", monthlyTokens: 1_000_000 }), + "1M tokens/month" + ); +}); + +test("formatFreeBudget: recurring-credit formats credits", () => { + assert.equal( + formatFreeBudget({ freeType: "recurring-credit", creditTokens: 10_000_000 }), + "10M credits" + ); +}); + +test("formatFreeBudget: one-time-initial formats credits with (one-time) suffix", () => { + assert.equal( + formatFreeBudget({ freeType: "one-time-initial", creditTokens: 1_000_000 }), + "1M credits (one-time)" + ); +}); + +test("formatFreeBudget: keyless has no token/credit args", () => { + assert.equal(formatFreeBudget({ freeType: "keyless" }), "(keyless)"); +}); + +test("formatFreeBudget: discontinued has no token/credit args", () => { + assert.equal(formatFreeBudget({ freeType: "discontinued" }), "(discontinued)"); +}); + +test("formatFreeBudget: missing token/credit counts default to 0", () => { + assert.equal( + formatFreeBudget({ freeType: "recurring-daily" }), + "0 tokens/day" + ); +}); + +test("formatFreeBudget: unrecognised freeType falls through to the default branch", () => { + // `freeType` is populated from catalog data at runtime, so a value the + // build doesn't know about is reachable even though TypeScript treats the + // `default:` arm as dead code for a well-typed caller. + assert.equal( + formatFreeBudget({ freeType: "some-future-type" as FreeModelFreeType }), + "" + ); +}); + +test("formatFreeBudget: sub-1K token count is not abbreviated", () => { + assert.equal( + formatFreeBudget({ freeType: "recurring-daily", monthlyTokens: 500 }), + "500 tokens/day" + ); +}); + +test("formatFreeBudget: characterises the 999_999 rounding wart (rounds past its own magnitude)", () => { + // `toFixed(1)` rounds 999999/1e3 up to "1000.0" before the `>= 1e6` + // threshold check has a chance to apply, so this reads "1000K" instead of + // the intended "1M". Not this PR's bug to fix — pinned here as the + // documented current behaviour so a future fix has a test to flip. + assert.equal( + formatFreeBudget({ freeType: "recurring-daily", monthlyTokens: 999_999 }), + "1000K tokens/day" + ); +}); diff --git a/changelog.d/maintenance/11660-formatfreebudget-unit-tests.md b/changelog.d/maintenance/11660-formatfreebudget-unit-tests.md new file mode 100644 index 0000000000..a700f82214 --- /dev/null +++ b/changelog.d/maintenance/11660-formatfreebudget-unit-tests.md @@ -0,0 +1 @@ +- **test(opencode-plugin):** add unit test coverage for `formatFreeBudget()` naming helper ([#11660](https://github.com/diegosouzapw/OmniRoute/pull/11660)) — thanks @f9td56dbgh-hub