|
| 1 | +# Copilot instructions for ReSpec |
| 2 | + |
| 3 | +ReSpec is a browser-based tool that generates W3C specifications. Authors write HTML plus a `<script>` tag, and ReSpec fills in boilerplate, cross-references, bibliography, and validation. Roughly half of W3C standards are produced with it, so a regression here reaches a lot of documents. |
| 4 | + |
| 5 | +Contributions written with AI are welcome and have a policy: see [AI_POLICY.md](../AI_POLICY.md). Read it before opening a pull request. |
| 6 | + |
| 7 | +## Build and test |
| 8 | + |
| 9 | +This is the whole sequence. The build is part of it, and so are `BROWSERS` and `PUPPETEER_CACHE_DIR`: |
| 10 | + |
| 11 | +```bash |
| 12 | +export PUPPETEER_CACHE_DIR="$PWD/.cache/puppeteer" |
| 13 | +pnpm i --frozen-lockfile |
| 14 | +pnpm lint # tsc -p src/jsconfig.json && eslint . |
| 15 | +pnpm build:w3c && pnpm build:aom && pnpm build:dini |
| 16 | +BROWSERS=ChromeHeadless pnpm test # unit then integration, via karma |
| 17 | +pnpm test:build # the builder tool |
| 18 | +pnpm test:headless # renders examples through puppeteer |
| 19 | +``` |
| 20 | + |
| 21 | +**Always set `BROWSERS`, or pass `--browsers`.** No karma config sets a default, so `pnpm test` on its own launches nothing, waits for a browser to connect by hand, and hangs until something kills it. Nothing in the output says so; it simply stops after printing `START:`. |
| 22 | + |
| 23 | +**Set `PUPPETEER_CACHE_DIR` to `$PWD/.cache/puppeteer` for anything that launches a browser**, which includes `pnpm test:headless` via `tools/respecDocWriter.js`. The agent environment provisions the browser there rather than in the default home cache, so puppeteer will not find it unless pointed at the same place. |
| 24 | + |
| 25 | +If you are a human with a browser already in puppeteer's default cache, you do not need that line, and you should skip it: the workspace copy is around 340 MB and does not get shared between checkouts. |
| 26 | + |
| 27 | +The integration suite (`tests/spec/`) reads the bundles in `builds/`, not `src/`, so a source change has no effect on it until the bundle is rebuilt. That is why the build sits above the test line rather than being mentioned afterwards. The unit suite (`tests/unit/`) loads `src/` directly, so it needs no rebuild and is the faster loop while iterating. |
| 28 | + |
| 29 | +There is one bundle per profile. Building only `w3c` leaves the DiNI and AOM suites testing the previous code, which looks like a passing or failing test that has nothing to do with the change. If a change that provably does nothing alters a test result, suspect a stale bundle before suspecting the test. |
| 30 | + |
| 31 | +Never commit anything under `builds/`. CI rebuilds it, and a PR touching it fails a dedicated check. |
| 32 | + |
| 33 | +To run a single integration suite, rebuild first, then pass the describe block. The unit config needs no rebuild: |
| 34 | + |
| 35 | +```bash |
| 36 | +pnpm build:w3c |
| 37 | +npx karma start tests/spec/karma.conf.cjs --single-run --browsers ChromeHeadless --grep="Core - Inlines" |
| 38 | +npx karma start tests/unit/karma.conf.cjs --single-run --browsers ChromeHeadless |
| 39 | +``` |
| 40 | + |
| 41 | +`--grep` is a literal string match. Alternation does not work, and only the last `--grep` counts, so run separate invocations instead. |
| 42 | + |
| 43 | +### Testing against a local respec-web-services or specref |
| 44 | + |
| 45 | +The suite resolves cross-references, W3C group data, caniuse, baseline and bibliography against `respec.org` and `api.specref.org`. Two environment variables send those requests somewhere else instead, so you can check a service change against this suite before it deploys: |
| 46 | + |
| 47 | +```bash |
| 48 | +RESPEC_SERVICES_BASE=http://localhost:8000 \ |
| 49 | +SPECREF_BASE=http://localhost:8001 \ |
| 50 | +BROWSERS=ChromeHeadless npx karma start tests/spec/karma.conf.cjs --single-run |
| 51 | +``` |
| 52 | + |
| 53 | +Set neither and everything goes to production exactly as before. A service worker (`tests/spec/respec-test-sw.js`) does the redirecting, so it also covers requests no per-document `fetch` wrapper reaches, such as the highlighter's `importScripts`. |
| 54 | + |
| 55 | +**Set both, or bibliography can still reach production.** `core/biblio.js` asks `api.specref.org` first and can fall back to `respec.org/bibrefs`, and these variables replace whole origins, so either one on its own leaves one of those two on the network: with only `SPECREF_BASE`, the fallback goes to production, and with only `RESPEC_SERVICES_BASE`, the first attempt does. Either way a local Specref that is stopped or broken can still pass the bibliography specs, quietly answered by production. Karma warns when only one is set. |
| 56 | + |
| 57 | +Two consequences to expect. While either variable is set the suite stops seeding the Cache API and gives each spec document a cache that misses every lookup, so every request really reaches your service rather than being answered from a fixture. And a service that is not running answers 502 through the worker, which surfaces as ReSpec's ordinary "response was not ok" handling rather than as an unhandled rejection. |
| 58 | + |
| 59 | +## Code style |
| 60 | + |
| 61 | +Prefer functional style over imperative loops: `forEach`, `map`, `filter`, `find`, `reduce`, `some`, `every`. Use an early `return` inside `forEach` rather than `continue`. `NodeList`, `Map` and `Set` have `forEach` natively, so prefer `nodeList.forEach()` over `[...nodeList].forEach()` and avoid the extra array. |
| 62 | + |
| 63 | +Every `querySelector`, `closest`, `getElementById` and `getAttribute` result is possibly null; check before use. Do not paper over it with `?? ""`, because the empty string then flows on into an `html` template and renders as an empty text node, so the bug shows up later as missing output rather than as a null failing where it happened. |
| 64 | + |
| 65 | +Write en-US English everywhere, including comments and identifiers: behavior, color, license, center, analyze, initialize, serialize. Leave existing en-GB spelling alone when it is load-bearing, such as an established identifier or a verbatim quotation. |
| 66 | + |
| 67 | +Run `pnpm format --write` on changed files. CI fails on unformatted code. |
| 68 | + |
| 69 | +## Pull requests, commits, and comments |
| 70 | + |
| 71 | +The house style is in [AI_POLICY.md](../AI_POLICY.md) under "Write it the way the project writes it". In short: |
| 72 | + |
| 73 | +- Pull request titles follow conventional commits, `type(scope): summary`, enforced by the Check PR Title workflow. |
| 74 | +- Pull request descriptions are plain prose. No `## Summary`, no `## Changes`, no `## Test plan`, no emoji, no bold section labels. A closing reference goes on the first line. |
| 75 | +- `Closes #N` only when every ask in the issue is delivered, otherwise `Refs #N`. Confirm the number is an issue and not a pull request before citing it. |
| 76 | +- Commit messages are one imperative subject line, lowercase after any prefix, no trailing period, and no body unless the diff cannot be understood without one. A `Co-Authored-By` trailer naming the model that wrote it is welcome. |
| 77 | +- Comments say why, not what. Delete a comment that restates the line below it. No bare `fixes #1234` in source. |
| 78 | + |
| 79 | +## Tests |
| 80 | + |
| 81 | +A test that cannot fail is worse than no test, because it costs review time and implies coverage that does not exist. A regression test must fail on `main` for the reason the issue describes, and pass with the fix. Do not assert a literal the implementation just set, and do not exercise a path the issue never mentioned while the reported path stays uncovered. |
| 82 | + |
| 83 | +Do not claim a bug is fixed without showing it broken first. Run the same repro against the pre-fix state and against your fix, and put that in the PR description. Prefer running it on the base commit; when the base will not build with your new test, revert only the fix and say so. The exact format the check validates is in [ai-pr-proof-requirement.md](../ai-pr-proof-requirement.md). |
| 84 | + |
| 85 | +One concern per pull request. A locale addition or a drive-by refactor belongs in its own PR even when it is one line and obviously correct. |
| 86 | + |
| 87 | +## Adding a module |
| 88 | + |
| 89 | +A module goes in `src/core/` only if every profile wants it. If it is specific to one, put it in that profile's folder instead, as `src/w3c/` already does for eight modules. Either way it exports `name` and a `run(conf)`. `run` may be synchronous or async, whichever the work needs: most core modules are synchronous, and only the ones that fetch or await something are not. The module must be registered in every profile that needs it: `profiles/w3c.js`, `profiles/aom.js`, `profiles/dini.js`. Tests go in `tests/spec/core/`. If the module uses `getIntlData`, add a Czech (`cs`) entry. |
0 commit comments