okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0215-webspec: web page: the About card's "MTP drafts up to N tokens" only when the MTP drafts (#5080)d8124515a

main
2.2 KiB46 linesraw
1"""#5080: the web page's About card said "MTP drafts up to 5 tokens" for an engine that ran the DFlash2 drafter (or none): the row named the MTP
2whatever /metrics said. The row now says that only when the engine's ``drafting`` is the MTP, or not reported (an older engine, which had only the MTP)."""
3
4import json
5import re
6import shutil
7import subprocess
8import tempfile
9import unittest
10from pathlib import Path
11
12APP = Path(__file__).resolve().parent / "web" / "app.js"
13
14
15def speculation_expression() -> str:
16    line = next(ln for ln in APP.read_text(encoding="utf-8").splitlines() if ln.strip().startswith('["Speculation",'))
17    return re.match(r'\s*\["Speculation",\s*(.*)\],\s*$', line).group(1)
18
19
20class SpeculationRow(unittest.TestCase):
21    def test_the_row_reads_the_engines_drafter(self):
22        self.assertRegex(speculation_expression(), r'eng\.drafting == null \|\| eng\.drafting === "mtp"')
23
24    @unittest.skipUnless(shutil.which("node"), "node is not on PATH")
25    def test_what_the_row_says_for_each_drafter(self):
26        cases = {"dflash2": {"spec": 6, "mtp_max": 6, "lookup": 3, "drafting": "dflash2"},
27                 "off": {"spec": 4, "lookup": 3, "drafting": "off"},
28                 "mtp": {"spec": 4, "lookup": 0, "drafting": "mtp"},
29                 "old": {"spec": 4, "lookup": 3},
30                 "none": {"spec": 0, "drafting": "mtp"}}
31        script = "const out = {};\n" + "".join(
32            f"{{ const eng = {json.dumps(eng)}; out[{json.dumps(k)}] = {speculation_expression()}; }}\n" for k, eng in cases.items()) + \
33            "console.log(JSON.stringify(out));\n"
34        with tempfile.TemporaryDirectory() as d:
35            path = Path(d) / "row.js"
36            path.write_text(script, encoding="utf-8")
37            got = json.loads(subprocess.run([shutil.which("node"), str(path)], capture_output=True, text=True, check=True, timeout=60).stdout)
38        self.assertIsNone(got["dflash2"], "a DFlash2 engine is not said to draft with the MTP")
39        self.assertIsNone(got["off"])
40        self.assertEqual(got["mtp"], "MTP drafts up to 3 tokens")
41        self.assertEqual(got["old"], "MTP drafts up to 3 tokens, prompt lookup on")
42        self.assertIsNone(got["none"])
43
44
45if __name__ == "__main__":
46    unittest.main()