Files
galaxy-game/ui/frontend/tests/science-validation.test.ts
T
Ilia Denisov 140ee8e0ee
Build · Site / build (push) Successful in 8s
Tests · Go / test (push) Successful in 2m27s
Tests · UI / test (push) Waiting to run
Tests · Integration / integration (pull_request) Successful in 1m45s
Build · Site / build (pull_request) Successful in 9s
Tests · Go / test (pull_request) Successful in 3m14s
Tests · UI / test (pull_request) Successful in 3m14s
docs(site): edit rules for clarity + cross-links; migrate off rules.txt
Editorial pass over site/ru/rules.md (on top of the verbatim port):
- moved the lore intro to the RU home page, rewritten in a modern voice;
- fixed typos, replaced the TODO/WTF cargo-tech note and the abandoned
  (---ссылка---) marker with the verified mechanic and a real cross-link,
  dropped the report TODO row;
- wove organic intra-page cross-links (#combat, #movement, #victory, ...);
- documented engine nuances verified against the code: ore auto-farming
  and the capital / "запасы промышленности" store (industry capped at
  population); cargo lost with ships destroyed in battle; and that a
  losing race's colonists at a neutral planet are NOT lost — they stay
  aboard (this corrects the audit note, verified in route.go).

Migration: delete game/rules.txt (its content now lives, authoritative,
in site/ru/rules.md) and repoint every reference to it (ui/frontend code
comments + tests, ui/docs, tools, ui/PLAN.md links). Record the
RU-authoritative rule in site/README.md and CLAUDE.md. The English
site/rules.md mirror follows in a separate stage.
2026-05-31 15:56:00 +02:00

191 lines
5.8 KiB
TypeScript

// Vitest coverage for `lib/util/science-validation.ts`. The
// validator is the TS-side mirror of
// `pkg/calc/validator.go.ValidateScienceValues` plus the
// `validateEntityName` rules and a UX-only duplicate-name check.
// The designer composes percentages (`[0, 100]` summing to `100`)
// and the validator returns canonical fractions (`[0, 1]` summing
// to `1.0`) on success — so the exhaustive coverage below also
// pins the percent → fraction conversion contract.
import { describe, expect, test } from "vitest";
import {
SUM_EPSILON_PERCENT,
fractionsToPercent,
validateScience,
type ScienceDraft,
} from "../src/lib/util/science-validation";
function draft(overrides: Partial<ScienceDraft>): ScienceDraft {
return {
name: "FirstStep",
drive: 25,
weapons: 25,
shields: 25,
cargo: 25,
...overrides,
};
}
describe("validateScience", () => {
test("accepts an even-split science and converts to fractions", () => {
const result = validateScience(draft({ name: "Even" }));
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.name).toBe("Even");
expect(result.value.drive).toBeCloseTo(0.25, 12);
expect(result.value.weapons).toBeCloseTo(0.25, 12);
expect(result.value.shields).toBeCloseTo(0.25, 12);
expect(result.value.cargo).toBeCloseTo(0.25, 12);
});
test("trims surrounding whitespace from the name", () => {
const result = validateScience(draft({ name: " Beta " }));
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.name).toBe("Beta");
});
test("rejects empty name", () => {
const result = validateScience(draft({ name: "" }));
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.reason).toBe("empty");
});
test("rejects name longer than 30 runes", () => {
const result = validateScience(draft({ name: "a".repeat(31) }));
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.reason).toBe("too_long");
});
test("rejects name with whitespace inside", () => {
const result = validateScience(draft({ name: "Big Name" }));
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.reason).toBe("whitespace");
});
test.each([
{ value: -0.1, reason: "drive_value" as const, field: "drive" as const },
{ value: 100.1, reason: "drive_value" as const, field: "drive" as const },
{
value: Number.POSITIVE_INFINITY,
reason: "drive_value" as const,
field: "drive" as const,
},
{
value: Number.NaN,
reason: "weapons_value" as const,
field: "weapons" as const,
},
{
value: -1,
reason: "shields_value" as const,
field: "shields" as const,
},
{ value: 101, reason: "cargo_value" as const, field: "cargo" as const },
])(
"rejects $field = $value with reason $reason",
({ value, reason, field }) => {
const overrides: Partial<ScienceDraft> = {
drive: 25,
weapons: 25,
shields: 25,
cargo: 25,
};
(overrides as Record<typeof field, number>)[field] = value;
const result = validateScience(draft(overrides));
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.reason).toBe(reason);
},
);
test("rejects sum off by more than the epsilon", () => {
const result = validateScience(
draft({ drive: 30, weapons: 30, shields: 30, cargo: 5 }),
);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.reason).toBe("sum_not_hundred");
});
test("accepts the canonical First Step fixture from site/ru/rules.md", () => {
// 10 Drive + 5 Weapons + 30 Shields + 0 Cargo, normalised:
// 10/45 ≈ 22.222… %, 5/45 ≈ 11.111… %, 30/45 ≈ 66.666… %,
// 0/45 = 0 %. Snapped to one decimal at input time:
// 22.2 / 11.1 / 66.7 / 0 → sum = 100. The float arithmetic is
// well within `SUM_EPSILON_PERCENT`.
const result = validateScience(
draft({ name: "FirstStep", drive: 22.2, weapons: 11.1, shields: 66.7, cargo: 0 }),
);
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.drive).toBeCloseTo(0.222, 6);
expect(result.value.weapons).toBeCloseTo(0.111, 6);
expect(result.value.shields).toBeCloseTo(0.667, 6);
expect(result.value.cargo).toBe(0);
});
test("accepts a 100/0/0/0 single-axis science", () => {
const result = validateScience(
draft({ name: "PureDrive", drive: 100, weapons: 0, shields: 0, cargo: 0 }),
);
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.drive).toBe(1);
});
test("accepts sums within the float tolerance", () => {
// Build a sum that drifts a hair off due to FP arithmetic but
// stays inside `SUM_EPSILON_PERCENT`.
const sumOk = 100 - SUM_EPSILON_PERCENT / 2;
const result = validateScience(
draft({ drive: sumOk, weapons: 0, shields: 0, cargo: 0 }),
);
expect(result.ok).toBe(true);
});
test("flags duplicate names against existingNames", () => {
const result = validateScience(draft({ name: "Beta" }), {
existingNames: ["Alpha", "Beta"],
});
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.reason).toBe("duplicate_name");
});
test("compares duplicate names after trimming", () => {
const result = validateScience(draft({ name: " Beta " }), {
existingNames: ["Beta"],
});
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.reason).toBe("duplicate_name");
});
test("does not flag duplicates when existingNames is empty", () => {
const result = validateScience(draft({ name: "Beta" }), {
existingNames: [],
});
expect(result.ok).toBe(true);
});
});
describe("fractionsToPercent", () => {
test("inverts the percent → fraction conversion", () => {
const back = fractionsToPercent({
drive: 0.25,
weapons: 0.111,
shields: 0.5,
cargo: 0.139,
});
expect(back.drive).toBeCloseTo(25, 6);
expect(back.weapons).toBeCloseTo(11.1, 6);
expect(back.shields).toBeCloseTo(50, 6);
expect(back.cargo).toBeCloseTo(13.9, 6);
});
});