vomebook commited on
Commit ·
9435d92
1
Parent(s): da9ec8b
Gate download controller behavior and coverage
Browse files
Dockerfile
CHANGED
|
@@ -49,6 +49,7 @@ RUN npx --no-install pyright --project pyrightconfig.json \
|
|
| 49 |
&& node scripts/check_search_filter_equality_baseline.mjs \
|
| 50 |
&& node scripts/check_search_folder_merge_baseline.mjs \
|
| 51 |
&& node --test tests/test_tap_contracts.mjs \
|
|
|
|
| 52 |
&& printf 'static gate tests passed\n' >/check/static-gate-test-ok
|
| 53 |
|
| 54 |
FROM node:26.11.1-bookworm-slim AS static-assets
|
|
|
|
| 49 |
&& node scripts/check_search_filter_equality_baseline.mjs \
|
| 50 |
&& node scripts/check_search_folder_merge_baseline.mjs \
|
| 51 |
&& node --test tests/test_tap_contracts.mjs \
|
| 52 |
+
&& node scripts/check_download_controller_coverage.mjs \
|
| 53 |
&& printf 'static gate tests passed\n' >/check/static-gate-test-ok
|
| 54 |
|
| 55 |
FROM node:26.11.1-bookworm-slim AS static-assets
|
scripts/check_download_controller_coverage.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {spawnSync} from "node:child_process";
|
| 2 |
+
import {hasPassingTapContracts} from "./check_tap_contracts.mjs";
|
| 3 |
+
|
| 4 |
+
// Node 26/V8 scope: the download controller and its synthetic contract tests.
|
| 5 |
+
// A missing row, missing named test, skipped case, or fractional regression fails.
|
| 6 |
+
const minimum = [100, 87.74, 100];
|
| 7 |
+
const result = spawnSync(process.execPath, [
|
| 8 |
+
"--test", "--test-reporter=tap", "--experimental-test-coverage",
|
| 9 |
+
"--test-coverage-include=**/static/search/download-controller.js",
|
| 10 |
+
...minimum.map((value, index) => `--test-coverage-${["lines", "branches", "functions"][index]}=${value}`),
|
| 11 |
+
"tests/test_download_controller_contract.js"
|
| 12 |
+
], {encoding: "utf8"});
|
| 13 |
+
if (result.stdout) process.stdout.write(result.stdout);
|
| 14 |
+
if (result.stderr) process.stderr.write(result.stderr);
|
| 15 |
+
if (result.error) throw result.error;
|
| 16 |
+
const text = result.stdout || "";
|
| 17 |
+
const rows = text.split(/\r?\n/).flatMap(line => {
|
| 18 |
+
const match = /^\s*#\s*download-controller\.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 |
+
"shared checks preserve independent cancellation, cached success and expiration",
|
| 23 |
+
"two active checks bound the queue and abandoned work never starts",
|
| 24 |
+
"occupied queue starts the next live check only after a slot is released",
|
| 25 |
+
"check failures report legacy messages and permit a retry",
|
| 26 |
+
"a stalled response body times out, releases its slot and retries without caching failure",
|
| 27 |
+
"an abandoned active check cannot evict a successful retry when its body resolves late",
|
| 28 |
+
"successful check cache retains at most 64 entries in insertion order",
|
| 29 |
+
"shared downloads retain other owners and restore button feedback",
|
| 30 |
+
"download failures restore controls and emit the appropriate feedback",
|
| 31 |
+
"a failed browser handoff does not strand later queued downloads",
|
| 32 |
+
"handoffs are paced and a cancelled queued handoff never launches"
|
| 33 |
+
].map(name => ({name}));
|
| 34 |
+
if (result.status !== 0 || rows.length !== 1 || rows[0].some((value, index) => value < minimum[index]) ||
|
| 35 |
+
!hasPassingTapContracts(text, required)) {
|
| 36 |
+
console.error("Download controller coverage requires passing named contracts and exactly one source row above each minimum");
|
| 37 |
+
process.exitCode = 1;
|
| 38 |
+
}
|
tests/test_download_controller_contract.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const assert = require("node:assert/strict");
|
| 2 |
+
const fs = require("node:fs");
|
| 3 |
+
const path = require("node:path");
|
| 4 |
+
const test = require("node:test");
|
| 5 |
+
const vm = require("node:vm");
|
| 6 |
+
|
| 7 |
+
// Behavioral baseline against the original shared download controller.
|
| 8 |
+
// Only synthetic requests and a controlled clock are used; no file is downloaded.
|
| 9 |
+
function fixture() {
|
| 10 |
+
let now = 1000, nextTimer = 0;
|
| 11 |
+
const timers = new Map(), requests = [], launches = [], toasts = [];
|
| 12 |
+
const sandbox = {
|
| 13 |
+
AbortController, DOMException,
|
| 14 |
+
Date: class extends Date { static now() { return now; } },
|
| 15 |
+
setTimeout(callback, delay) {
|
| 16 |
+
const id = ++nextTimer;
|
| 17 |
+
timers.set(id, {callback, delay});
|
| 18 |
+
return id;
|
| 19 |
+
},
|
| 20 |
+
clearTimeout(id) { timers.delete(id); },
|
| 21 |
+
fetch(url, options) {
|
| 22 |
+
return new Promise((resolve, reject) => requests.push({url, signal: options.signal, resolve, reject}));
|
| 23 |
+
},
|
| 24 |
+
};
|
| 25 |
+
const filename = path.join(__dirname, "../static/search/download-controller.js");
|
| 26 |
+
vm.runInNewContext(fs.readFileSync(filename, "utf8"), sandbox, {filename});
|
| 27 |
+
const controller = sandbox.VoiceOfMLDownloadController.createDownloadController({
|
| 28 |
+
buildCheckUrl: link => "check:" + link,
|
| 29 |
+
buildDownloadUrl: (name, link) => `${name}|${link}`,
|
| 30 |
+
triggerDownload: url => launches.push(url),
|
| 31 |
+
showToast: (...args) => toasts.push(args),
|
| 32 |
+
});
|
| 33 |
+
return {
|
| 34 |
+
controller, requests, launches, toasts, timers,
|
| 35 |
+
advance(ms) { now += ms; },
|
| 36 |
+
fireDelay(delay) {
|
| 37 |
+
const entry = [...timers].find(([, timer]) => timer.delay === delay);
|
| 38 |
+
assert.ok(entry, `missing ${delay}ms timer`);
|
| 39 |
+
timers.delete(entry[0]);
|
| 40 |
+
entry[1].callback();
|
| 41 |
+
},
|
| 42 |
+
};
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
const response = (data, ok = true, status = 200) => ({ok, status, json: async () => data});
|
| 46 |
+
const flush = () => new Promise(resolve => setImmediate(resolve));
|
| 47 |
+
|
| 48 |
+
test("shared checks preserve independent cancellation, cached success and expiration", async () => {
|
| 49 |
+
const {controller: c, requests, timers, advance} = fixture();
|
| 50 |
+
const owner = new AbortController();
|
| 51 |
+
const first = c.checkDownload("book", false, owner.signal);
|
| 52 |
+
const second = c.checkDownload("book");
|
| 53 |
+
assert.equal(requests.length, 1);
|
| 54 |
+
assert.equal(requests[0].url, "check:book");
|
| 55 |
+
assert.equal(c.activeDownloadChecks, 1);
|
| 56 |
+
owner.abort();
|
| 57 |
+
assert.equal(await first, false);
|
| 58 |
+
assert.equal(requests[0].signal.aborted, false);
|
| 59 |
+
requests[0].resolve(response({ok: true}));
|
| 60 |
+
assert.equal(await second, true);
|
| 61 |
+
assert.equal(c.activeDownloadChecks, 0);
|
| 62 |
+
assert.equal(timers.size, 0);
|
| 63 |
+
advance(29999);
|
| 64 |
+
assert.equal(await c.checkDownload("book"), true);
|
| 65 |
+
assert.equal(requests.length, 1);
|
| 66 |
+
advance(1);
|
| 67 |
+
const expired = c.checkDownload("book");
|
| 68 |
+
assert.equal(requests.length, 2);
|
| 69 |
+
requests[1].resolve(response({ok: true}));
|
| 70 |
+
assert.equal(await expired, true);
|
| 71 |
+
});
|
| 72 |
+
|
| 73 |
+
test("two active checks bound the queue and abandoned work never starts", async () => {
|
| 74 |
+
const {controller: c, requests} = fixture();
|
| 75 |
+
const thirdOwner = new AbortController();
|
| 76 |
+
const first = c.checkDownload("a"), second = c.checkDownload("b");
|
| 77 |
+
const third = c.checkDownload("c", false, thirdOwner.signal);
|
| 78 |
+
assert.equal(c.activeDownloadChecks, 2);
|
| 79 |
+
assert.equal(c.downloadCheckQueue.length, 1);
|
| 80 |
+
assert.equal(requests.length, 2);
|
| 81 |
+
assert.equal(await c.checkDownload("speculative", true), false);
|
| 82 |
+
thirdOwner.abort();
|
| 83 |
+
assert.equal(await third, false);
|
| 84 |
+
assert.equal(c.downloadChecks.has("c"), false);
|
| 85 |
+
requests[0].resolve(response({ok: true}));
|
| 86 |
+
assert.equal(await first, true);
|
| 87 |
+
assert.equal(requests.length, 2);
|
| 88 |
+
assert.equal(c.downloadCheckQueue.length, 0);
|
| 89 |
+
requests[1].resolve(response({ok: true}));
|
| 90 |
+
assert.equal(await second, true);
|
| 91 |
+
assert.equal(c.activeDownloadChecks, 0);
|
| 92 |
+
const speculative = c.checkDownload("idle-speculative", true);
|
| 93 |
+
assert.equal(requests.length, 3);
|
| 94 |
+
requests[2].resolve(response({ok: true}));
|
| 95 |
+
assert.equal(await speculative, true);
|
| 96 |
+
});
|
| 97 |
+
|
| 98 |
+
test("occupied queue starts the next live check only after a slot is released", async () => {
|
| 99 |
+
const {controller: c, requests} = fixture();
|
| 100 |
+
const first = c.checkDownload("a"), second = c.checkDownload("b"), third = c.checkDownload("c");
|
| 101 |
+
assert.equal(requests.length, 2);
|
| 102 |
+
assert.equal(c.downloadCheckQueue.length, 1);
|
| 103 |
+
requests[0].resolve(response({ok: true}));
|
| 104 |
+
assert.equal(await first, true);
|
| 105 |
+
assert.equal(requests.length, 3);
|
| 106 |
+
assert.equal(requests[2].url, "check:c");
|
| 107 |
+
assert.equal(c.activeDownloadChecks, 2);
|
| 108 |
+
requests[1].resolve(response({ok: true}));
|
| 109 |
+
requests[2].resolve(response({ok: true}));
|
| 110 |
+
assert.equal(await second, true);
|
| 111 |
+
assert.equal(await third, true);
|
| 112 |
+
assert.equal(c.activeDownloadChecks, 0);
|
| 113 |
+
});
|
| 114 |
+
|
| 115 |
+
test("check failures report legacy messages and permit a retry", async () => {
|
| 116 |
+
const {controller: c, requests} = fixture();
|
| 117 |
+
const failed = c.checkDownload("retry");
|
| 118 |
+
requests[0].resolve(response({error: "missing"}, false, 503));
|
| 119 |
+
assert.equal(await failed, "missing");
|
| 120 |
+
assert.equal(c.downloadChecks.has("retry"), false);
|
| 121 |
+
const retry = c.checkDownload("retry");
|
| 122 |
+
requests[1].resolve(response({ok: true}));
|
| 123 |
+
assert.equal(await retry, true);
|
| 124 |
+
const badJson = c.checkDownload("gateway");
|
| 125 |
+
requests[2].resolve({ok: false, status: 502, json: async () => { throw new SyntaxError("html"); }});
|
| 126 |
+
assert.equal(await badJson, "下载检查失败(HTTP 502),请重试");
|
| 127 |
+
const httpFailure = c.checkDownload("contradictory");
|
| 128 |
+
requests[3].resolve(response({ok: true}, false, 503));
|
| 129 |
+
assert.equal(await httpFailure, "下载检查失败(HTTP 503),请重试");
|
| 130 |
+
const applicationFailure = c.checkDownload("not-ok");
|
| 131 |
+
requests[4].resolve(response({ok: false}, true));
|
| 132 |
+
assert.equal(await applicationFailure, "下载检查失败,请重试");
|
| 133 |
+
});
|
| 134 |
+
|
| 135 |
+
test("a stalled response body times out, releases its slot and retries without caching failure", async () => {
|
| 136 |
+
const {controller: c, requests, timers, fireDelay} = fixture();
|
| 137 |
+
const first = c.checkDownload("stalled");
|
| 138 |
+
const request = requests[0];
|
| 139 |
+
request.resolve({ok: true, status: 200, json: () => new Promise((resolve, reject) => {
|
| 140 |
+
request.signal.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")));
|
| 141 |
+
})});
|
| 142 |
+
await flush();
|
| 143 |
+
fireDelay(8000);
|
| 144 |
+
assert.equal(await first, "下载检查超时,请重试");
|
| 145 |
+
assert.equal(request.signal.aborted, true);
|
| 146 |
+
assert.equal(c.activeDownloadChecks, 0);
|
| 147 |
+
assert.equal(c.downloadChecks.has("stalled"), false);
|
| 148 |
+
assert.equal(timers.size, 0);
|
| 149 |
+
const retry = c.checkDownload("stalled");
|
| 150 |
+
requests[1].resolve(response({ok: true}));
|
| 151 |
+
assert.equal(await retry, true);
|
| 152 |
+
});
|
| 153 |
+
|
| 154 |
+
test("an abandoned active check cannot evict a successful retry when its body resolves late", async () => {
|
| 155 |
+
const {controller: c, requests} = fixture();
|
| 156 |
+
const owner = new AbortController();
|
| 157 |
+
const old = c.checkDownload("retry", false, owner.signal);
|
| 158 |
+
owner.abort();
|
| 159 |
+
assert.equal(await old, false);
|
| 160 |
+
assert.equal(requests[0].signal.aborted, true);
|
| 161 |
+
const retry = c.checkDownload("retry");
|
| 162 |
+
assert.equal(requests.length, 2);
|
| 163 |
+
requests[1].resolve(response({ok: true}));
|
| 164 |
+
assert.equal(await retry, true);
|
| 165 |
+
requests[0].resolve(response({ok: true}));
|
| 166 |
+
await flush();
|
| 167 |
+
assert.equal(await c.checkDownload("retry"), true);
|
| 168 |
+
assert.equal(requests.length, 2);
|
| 169 |
+
assert.equal(c.activeDownloadChecks, 0);
|
| 170 |
+
});
|
| 171 |
+
|
| 172 |
+
test("successful check cache retains at most 64 entries in insertion order", async () => {
|
| 173 |
+
const {controller: c, requests} = fixture();
|
| 174 |
+
for (let i = 0; i < 65; i++) {
|
| 175 |
+
const checked = c.checkDownload("item" + i);
|
| 176 |
+
requests[i].resolve(response({ok: true}));
|
| 177 |
+
assert.equal(await checked, true);
|
| 178 |
+
}
|
| 179 |
+
assert.equal(c.downloadChecks.size, 64);
|
| 180 |
+
assert.equal(c.downloadChecks.has("item0"), false);
|
| 181 |
+
assert.equal(c.downloadChecks.has("item64"), true);
|
| 182 |
+
});
|
| 183 |
+
|
| 184 |
+
test("shared downloads retain other owners and restore button feedback", async () => {
|
| 185 |
+
const {controller: c, requests, launches, toasts} = fixture();
|
| 186 |
+
const attrs = new Map();
|
| 187 |
+
const button = {textContent: "下载", setAttribute(k, v) { attrs.set(k, v); }, removeAttribute(k) { attrs.delete(k); }};
|
| 188 |
+
const batch = {cancelled: false, abortController: new AbortController()};
|
| 189 |
+
const first = c.downloadFile("book.txt", "shared", {batch, button});
|
| 190 |
+
const second = c.downloadFile("book.txt", "shared", {quiet: true});
|
| 191 |
+
assert.equal(button.textContent, "准备中…");
|
| 192 |
+
assert.equal(attrs.get("aria-busy"), "true");
|
| 193 |
+
await flush();
|
| 194 |
+
assert.equal(requests.length, 1);
|
| 195 |
+
batch.abortController.abort();
|
| 196 |
+
assert.equal(await first, false);
|
| 197 |
+
assert.equal(requests[0].signal.aborted, false);
|
| 198 |
+
requests[0].resolve(response({ok: true}));
|
| 199 |
+
assert.equal(await second, true);
|
| 200 |
+
assert.deepEqual(launches, ["book.txt|shared"]);
|
| 201 |
+
assert.equal(button.textContent, "下载");
|
| 202 |
+
assert.equal(attrs.has("aria-busy"), false);
|
| 203 |
+
assert.equal(c.pendingDownloads.size, 0);
|
| 204 |
+
assert.equal(toasts[0][0], "正在准备下载…");
|
| 205 |
+
});
|
| 206 |
+
|
| 207 |
+
test("download failures restore controls and emit the appropriate feedback", async () => {
|
| 208 |
+
const {controller: c, requests, launches, toasts} = fixture();
|
| 209 |
+
const attrs = new Map();
|
| 210 |
+
const button = {textContent: "下载", setAttribute(k, v) { attrs.set(k, v); }, removeAttribute(k) { attrs.delete(k); }};
|
| 211 |
+
const failed = c.downloadFile("missing.txt", "missing", {button});
|
| 212 |
+
await flush();
|
| 213 |
+
requests[0].resolve(response({error: "不存在"}, false, 404));
|
| 214 |
+
assert.equal(await failed, false);
|
| 215 |
+
assert.deepEqual(toasts.map(args => args[0]), ["正在准备下载…", "不存在"]);
|
| 216 |
+
assert.deepEqual(launches, []);
|
| 217 |
+
assert.equal(button.textContent, "下载");
|
| 218 |
+
assert.equal(attrs.has("aria-busy"), false);
|
| 219 |
+
assert.equal(c.pendingDownloads.size, 0);
|
| 220 |
+
});
|
| 221 |
+
|
| 222 |
+
test("a failed browser handoff does not strand later queued downloads", async () => {
|
| 223 |
+
const {controller: c, launches, toasts} = fixture();
|
| 224 |
+
const interrupted = c.downloadFile({toString() { throw new Error("handoff failed"); }}, "bad", {skipCheck: true});
|
| 225 |
+
assert.equal(await interrupted, false);
|
| 226 |
+
assert.equal(toasts.at(-1)[0], "下载失败,请稍后重试");
|
| 227 |
+
assert.equal(c.pendingDownloads.size, 0);
|
| 228 |
+
assert.equal(await c.downloadFile("next.txt", "next", {quiet: true, skipCheck: true}), true);
|
| 229 |
+
assert.deepEqual(launches, ["next.txt|next"]);
|
| 230 |
+
});
|
| 231 |
+
|
| 232 |
+
test("handoffs are paced and a cancelled queued handoff never launches", async () => {
|
| 233 |
+
const {controller: c, launches, timers, advance, fireDelay} = fixture();
|
| 234 |
+
assert.equal(await c.downloadFile("one.txt", "one", {quiet: true, skipCheck: true}), true);
|
| 235 |
+
const batch = {cancelled: false, abortController: new AbortController()};
|
| 236 |
+
const delayed = c.downloadFile("two.txt", "two", {batch, quiet: true, skipCheck: true});
|
| 237 |
+
await flush();
|
| 238 |
+
assert.equal(timers.size, 1);
|
| 239 |
+
batch.abortController.abort();
|
| 240 |
+
assert.equal(await delayed, false);
|
| 241 |
+
advance(300);
|
| 242 |
+
fireDelay(300);
|
| 243 |
+
await flush();
|
| 244 |
+
assert.deepEqual(launches, ["one.txt|one"]);
|
| 245 |
+
assert.equal(c.pendingDownloads.size, 0);
|
| 246 |
+
assert.equal(timers.size, 0);
|
| 247 |
+
});
|
tests/test_static_checks.py
CHANGED
|
@@ -268,6 +268,43 @@ class StaticGateTests(unittest.TestCase):
|
|
| 268 |
(self.root / paths[2]).write_text('import "../static/reader/core/reader-document-loader.mjs";\n')
|
| 269 |
self.assert_check(self.run_check(*check), False, "coverage")
|
| 270 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
def test_shared_tap_validator_tests_are_required(self):
|
| 272 |
paths = ["scripts/check_tap_contracts.mjs", "tests/test_tap_contracts.mjs"]
|
| 273 |
for path in paths:
|
|
|
|
| 268 |
(self.root / paths[2]).write_text('import "../static/reader/core/reader-document-loader.mjs";\n')
|
| 269 |
self.assert_check(self.run_check(*check), False, "coverage")
|
| 270 |
|
| 271 |
+
def test_download_controller_coverage_gate_fails_closed(self):
|
| 272 |
+
paths = ["static/search/download-controller.js", "scripts/check_download_controller_coverage.mjs",
|
| 273 |
+
"scripts/check_tap_contracts.mjs", "tests/test_download_controller_contract.js"]
|
| 274 |
+
for relative in paths:
|
| 275 |
+
target = self.root / relative
|
| 276 |
+
target.parent.mkdir(parents=True, exist_ok=True)
|
| 277 |
+
shutil.copyfile(ROOT / relative, target)
|
| 278 |
+
check = ["node", "scripts/check_download_controller_coverage.mjs"]
|
| 279 |
+
self.assert_check(self.run_check(*check), True, "download-controller.js")
|
| 280 |
+
contract = self.root / paths[3]
|
| 281 |
+
original = contract.read_text()
|
| 282 |
+
names = re.findall(r'^test\("([^"\n]+)"', original, re.M)
|
| 283 |
+
self.assertEqual(len(names), 11)
|
| 284 |
+
self.assertEqual(len(set(names)), len(names))
|
| 285 |
+
for name in names:
|
| 286 |
+
anchor = 'test("' + name + '"'
|
| 287 |
+
self.assertEqual(original.count(anchor), 1)
|
| 288 |
+
for replacement in ('test("renamed contract"', 'test.skip("' + name + '"', 'test.todo("' + name + '"'):
|
| 289 |
+
contract.write_text(original.replace(anchor, replacement))
|
| 290 |
+
self.assert_check(self.run_check(*check), False, "passing named contracts")
|
| 291 |
+
for extra in ('test("' + names[0] + '", () => {});',
|
| 292 |
+
'test.skip("extra skip", () => {});', 'test.todo("extra todo");',
|
| 293 |
+
'console.log("skipped 0");', 'console.log("todo 0");'):
|
| 294 |
+
contract.write_text(original + "\n" + extra + "\n")
|
| 295 |
+
self.assert_check(self.run_check(*check), False, "passing named contracts")
|
| 296 |
+
contract.write_text('throw new Error("failed download contract");\n')
|
| 297 |
+
self.assert_check(self.run_check(*check), False, "failed download contract")
|
| 298 |
+
contract.write_text('const test = require("node:test"); test("empty", () => {});\n')
|
| 299 |
+
self.assert_check(self.run_check(*check), False, "exactly one source row")
|
| 300 |
+
contract.write_text(original)
|
| 301 |
+
gate = self.root / paths[1]
|
| 302 |
+
source = gate.read_text()
|
| 303 |
+
anchor = "const minimum = [100, 87.74, 100];"
|
| 304 |
+
self.assertEqual(source.count(anchor), 1)
|
| 305 |
+
gate.write_text(source.replace(anchor, "const minimum = [100, 88, 100];"))
|
| 306 |
+
self.assert_check(self.run_check(*check), False, "above each minimum")
|
| 307 |
+
|
| 308 |
def test_shared_tap_validator_tests_are_required(self):
|
| 309 |
paths = ["scripts/check_tap_contracts.mjs", "tests/test_tap_contracts.mjs"]
|
| 310 |
for path in paths:
|
tests/test_static_contract.py
CHANGED
|
@@ -452,6 +452,7 @@ class StaticFrontendContractTests(unittest.TestCase):
|
|
| 452 |
self.assertIn("node scripts/check_search_response_validation_coverage.mjs", dockerfile)
|
| 453 |
self.assertIn("node scripts/check_search_result_identity_coverage.mjs", dockerfile)
|
| 454 |
self.assertIn("node --test tests/test_tap_contracts.mjs", dockerfile)
|
|
|
|
| 455 |
self.assertIn("node scripts/check_search_filter_equality_baseline.mjs", dockerfile)
|
| 456 |
self.assertIn("node scripts/check_search_folder_merge_baseline.mjs", dockerfile)
|
| 457 |
self.assertIn("node --test tests/test_search_response_validation_baseline.mjs", dockerfile)
|
|
|
|
| 452 |
self.assertIn("node scripts/check_search_response_validation_coverage.mjs", dockerfile)
|
| 453 |
self.assertIn("node scripts/check_search_result_identity_coverage.mjs", dockerfile)
|
| 454 |
self.assertIn("node --test tests/test_tap_contracts.mjs", dockerfile)
|
| 455 |
+
self.assertIn("node scripts/check_download_controller_coverage.mjs", dockerfile)
|
| 456 |
self.assertIn("node scripts/check_search_filter_equality_baseline.mjs", dockerfile)
|
| 457 |
self.assertIn("node scripts/check_search_folder_merge_baseline.mjs", dockerfile)
|
| 458 |
self.assertIn("node --test tests/test_search_response_validation_baseline.mjs", dockerfile)
|