vomebook commited on
Commit
e298863
·
1 Parent(s): 6b1923e

Group search response cache contracts

Browse files
scripts/check_search_response_cache_coverage.mjs CHANGED
@@ -7,15 +7,17 @@ const result = spawnSync(process.execPath, [
7
  "--test", "--test-reporter=tap", "--experimental-test-coverage",
8
  "--test-coverage-include=**/static/search/response-cache.js",
9
  "--test-coverage-lines=100", "--test-coverage-branches=100", "--test-coverage-functions=100",
10
- "tests/test_search_response_cache_contract.js"
11
  ], {encoding: "utf8"});
12
  if (result.stdout) process.stdout.write(result.stdout);
13
  if (result.stderr) process.stderr.write(result.stderr);
14
  if (result.error) throw result.error;
15
  const text = result.stdout || "";
16
  const lines = text.split(/\r?\n/);
17
- const rows = lines.filter(line =>
18
- /^\s*#\s*response-cache\.js\s*\|\s*100(?:\.0+)?\s*\|\s*100(?:\.0+)?\s*\|\s*100(?:\.0+)?\s*\|/.test(line));
 
 
19
  const required = [
20
  "cache preserves LRU access order and exact expiration boundary",
21
  "has promotes live entries and undefined values throw without insertion",
@@ -24,7 +26,15 @@ const required = [
24
  "oversize replacement and serialization errors retire the previous key",
25
  "the independent one-MiB entry cap rejects larger entries even with a larger total budget"
26
  ].map(name => ({name}));
27
- if (result.status !== 0 || rows.length !== 1 || !hasPassingTapContracts(text, required)) {
 
 
 
 
 
 
 
 
28
  console.error("Search response cache coverage requires passing named tests and exactly one fully covered source row");
29
  process.exitCode = 1;
30
  }
 
7
  "--test", "--test-reporter=tap", "--experimental-test-coverage",
8
  "--test-coverage-include=**/static/search/response-cache.js",
9
  "--test-coverage-lines=100", "--test-coverage-branches=100", "--test-coverage-functions=100",
10
+ "tests/search/response-cache/test_search_response_cache_contract.js"
11
  ], {encoding: "utf8"});
12
  if (result.stdout) process.stdout.write(result.stdout);
13
  if (result.stderr) process.stderr.write(result.stderr);
14
  if (result.error) throw result.error;
15
  const text = result.stdout || "";
16
  const lines = text.split(/\r?\n/);
17
+ const rows = lines.flatMap(line => {
18
+ const match = /^\s*#\s*response-cache\.js\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|/.exec(line);
19
+ return match ? [match.slice(1).map(Number)] : [];
20
+ });
21
  const required = [
22
  "cache preserves LRU access order and exact expiration boundary",
23
  "has promotes live entries and undefined values throw without insertion",
 
26
  "oversize replacement and serialization errors retire the previous key",
27
  "the independent one-MiB entry cap rejects larger entries even with a larger total budget"
28
  ].map(name => ({name}));
29
+ const summaries = ["1..6", "# tests 6", "# pass 6", "# fail 0", "# cancelled 0", "# skipped 0", "# todo 0"];
30
+ const complete = summaries.every(summary => lines.filter(line => line === summary).length === 1) &&
31
+ lines.filter(line => /^(?:# )?1\.\.\d+$/.test(line)).length === 1 &&
32
+ ["# tests ", "# pass ", "# fail ", "# cancelled ", "# skipped ", "# todo "].every(prefix =>
33
+ lines.filter(line => line.startsWith(prefix)).length === 1) &&
34
+ lines.filter(line => /^# duration_ms \d+(?:\.\d+)?$/.test(line)).length === 1 &&
35
+ ["# start of coverage report", "# end of coverage report"].every(marker => lines.filter(line => line === marker).length === 1);
36
+ if (result.status !== 0 || rows.length !== 1 || rows[0].some(value => value !== 100) ||
37
+ !hasPassingTapContracts(text, required) || !complete) {
38
  console.error("Search response cache coverage requires passing named tests and exactly one fully covered source row");
39
  process.exitCode = 1;
40
  }
tests/search/response-cache/README.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Search response-cache contracts
2
+ ===============================
3
+
4
+ Run from the repository root with Node 26:
5
+
6
+ node scripts/check_search_response_cache_coverage.mjs
7
+
8
+ Six original named Node contracts cover the extracted response cache's LRU,
9
+ TTL, byte budget, replacement and serialization boundaries. The V8 gate is
10
+ restricted to static/search/response-cache.js; it does not measure the search
11
+ host, backend, network, real browser or whole site. The direct browser cache
12
+ regressions remain in their existing Python modules.
13
+ The gate also requires a complete, unique zero-skip/TODO TAP summary and
14
+ coverage-report boundaries; missing or forged summaries fail closed.
tests/{test_search_response_cache_contract.js → search/response-cache/test_search_response_cache_contract.js} RENAMED
@@ -7,7 +7,7 @@ const vm = require("node:vm");
7
  // Expectations were pinned against each site's original cache before extraction.
8
  function fixture(limit = 3, ttl = 100, budget = 100) {
9
  const context = vm.createContext({clock: 0, limit, ttl, budget});
10
- const filename = path.join(__dirname, "../static/search/response-cache.js");
11
  vm.runInContext(fs.readFileSync(filename, "utf8"), context, {filename});
12
  vm.runInContext(`Date.now = () => clock;
13
  globalThis.cache = new BoundedResponseCache(limit, ttl, budget);`, context);
 
7
  // Expectations were pinned against each site's original cache before extraction.
8
  function fixture(limit = 3, ttl = 100, budget = 100) {
9
  const context = vm.createContext({clock: 0, limit, ttl, budget});
10
+ const filename = path.join(__dirname, "../../../static/search/response-cache.js");
11
  vm.runInContext(fs.readFileSync(filename, "utf8"), context, {filename});
12
  vm.runInContext(`Date.now = () => clock;
13
  globalThis.cache = new BoundedResponseCache(limit, ttl, budget);`, context);
tests/test_static_checks.py CHANGED
@@ -389,7 +389,7 @@ class StaticGateTests(unittest.TestCase):
389
 
390
  def test_search_response_cache_coverage_gate_fails_closed(self):
391
  paths = ["static/search/response-cache.js", "scripts/check_search_response_cache_coverage.mjs",
392
- "tests/test_search_response_cache_contract.js"]
393
  for path in [*paths, "scripts/check_tap_contracts.mjs"]:
394
  target = self.root / path
395
  target.parent.mkdir(parents=True, exist_ok=True)
@@ -416,9 +416,21 @@ class StaticGateTests(unittest.TestCase):
416
  self.assert_check(self.run_check(*check), False, "passing named tests")
417
  for extra in ('console.log("skipped 0"); test.skip("extra skip", () => {});',
418
  'console.log("todo 0"); test.todo("extra todo");',
419
- 'console.log("skipped 0");', 'console.log("todo 0");'):
 
 
 
 
420
  test.write_text(original + "\n" + extra + "\n")
421
  self.assert_check(self.run_check(*check), False, "passing named tests")
 
 
 
 
 
 
 
 
422
  test.write_text('require("node:test")("empty", () => {});\n')
423
  self.assert_check(self.run_check(*check), False, "exactly one fully covered source row")
424
  test.write_text('throw new Error("failed response cache test");\n')
 
389
 
390
  def test_search_response_cache_coverage_gate_fails_closed(self):
391
  paths = ["static/search/response-cache.js", "scripts/check_search_response_cache_coverage.mjs",
392
+ "tests/search/response-cache/test_search_response_cache_contract.js"]
393
  for path in [*paths, "scripts/check_tap_contracts.mjs"]:
394
  target = self.root / path
395
  target.parent.mkdir(parents=True, exist_ok=True)
 
416
  self.assert_check(self.run_check(*check), False, "passing named tests")
417
  for extra in ('console.log("skipped 0"); test.skip("extra skip", () => {});',
418
  'console.log("todo 0"); test.todo("extra todo");',
419
+ 'console.log("skipped 0");', 'console.log("todo 0");',
420
+ 'console.log("tests 6");', 'console.log("pass 6");', 'console.log("fail 0");',
421
+ 'console.log("cancelled 0");', 'console.log("duration_ms 1");',
422
+ 'console.log("start of coverage report");', 'console.log("end of coverage report");',
423
+ 'console.log("1..6");'):
424
  test.write_text(original + "\n" + extra + "\n")
425
  self.assert_check(self.run_check(*check), False, "passing named tests")
426
+ for extra in ('console.log("response-cache.js | 99.00 | 99.00 | 99.00 |");',
427
+ 'console.log("response-cache.js | 100.00 | 100.00 | 100.00 |");'):
428
+ with self.subTest(forged_row=extra):
429
+ test.write_text(original + "\n" + extra + "\n")
430
+ result = self.run_check(*check)
431
+ self.assert_check(result, False, "exactly one fully covered source row")
432
+ self.assertIn("# response-cache.js | 100.00", result.stdout)
433
+ self.assertEqual(result.stdout.count("response-cache.js |"), 2)
434
  test.write_text('require("node:test")("empty", () => {});\n')
435
  self.assert_check(self.run_check(*check), False, "exactly one fully covered source row")
436
  test.write_text('throw new Error("failed response cache test");\n')