diff --git a/scripts/check-citations.mjs b/scripts/check-citations.mjs new file mode 100755 index 0000000..3ebe47d --- /dev/null +++ b/scripts/check-citations.mjs @@ -0,0 +1,396 @@ +#!/usr/bin/env node +/** + * check-citations.mjs + * + * Plain Node script (no dependency, no test framework) that proves every + * research claim and figure in README.md and in public/src/data carries a + * resolvable inline citation, and that every inline citation resolves on + * both mirrors. + * + * The citation scheme (settled by the methodology lead): a bracketed named id, + * the same ids on both mirrors — deci-ryan-2000, mark-2005, desktime-2014, + * mit-sloan-2017, rogelberg-2019, slack-workplace-lab-2017. README.md carries + * a `## References` section listing every id; public/src/data/references.js + * carries the equivalent references structure. Source reachability is NOT + * checked: a locator (DOI, ISBN or locator note) present in the entry is + * sufficient. + * + * What counts as a claim or figure here: + * - README.md: a content line carrying a digit or research vocabulary + * (survey, study, reported, participant, respondent, trial). Structured + * lines are carved out and never claims: table rows, fenced code, the + * references section itself, the version footer, principle-number titles, + * ceremony/duration list items and bold lead-in rule or limit statements + * (limits and durations are BEAT defaults, not research findings). + * - public/src/data: a prose string value carrying a digit, a percent sign + * or research vocabulary. Presentation and structured keys are carved out: + * icon, gradient, color, id, citation, stat, value, label, step, name, + * title, constraint, limit, rule, range, key, dataKey, heading, over, + * valueMore, pattern, category, duration, number. Durations, limits, + * option scale labels and score values are structured fields, not claims. + * + * What resolves a claim: an inline `[id]` marker that resolves to a reference + * entry on BOTH sides, or a BEAT-default marker in the same string + * ("a BEAT default", "BEAT's own ... not a research finding", "not a measured + * threshold"). + * + * The run fails (exit 1) when: + * - a claim or figure carries neither an inline citation nor a BEAT-default + * marker; + * - an inline citation id resolves to no entry in the README references + * section or to no entry in the data references structure; + * - the two reference sets disagree (an id on only one side); + * - a reference entry has no locator (README: DOI, ISBN or locator note; + * data: an empty `locator` field); + * - a reference entry is never cited (a FAILURE, not a warning: an entry + * no claim uses is a defect under this scheme). + * + * Usage: + * node scripts/check-citations.mjs [--readme ] [--manifesto ] + * [--assessment ] [--references ] + * + * Exit codes: 0 = every claim resolves and every citation and reference entry + * resolves on both sides, + * 1 = at least one of the checks above fails, + * 2 = usage or I/O error. + */ + +import { readFileSync } from 'node:fs' +import { dirname, join, relative, resolve } from 'node:path' +import { fileURLToPath, pathToFileURL } from 'node:url' + +const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url)) +const REPO_ROOT = resolve(SCRIPT_DIR, '..') + +const DEFAULTS = { + readme: join(REPO_ROOT, 'README.md'), + manifesto: join(REPO_ROOT, 'public', 'src', 'data', 'manifesto.js'), + assessment: join(REPO_ROOT, 'public', 'src', 'data', 'assessment.js'), + references: join(REPO_ROOT, 'public', 'src', 'data', 'references.js') +} + +const usage = [ + 'Usage: node scripts/check-citations.mjs [--readme ] [--manifesto ]', + ' [--assessment ] [--references ]', + '', + 'Checks that every research claim and figure in README.md and public/src/data', + 'carries an inline [id] citation or a BEAT-default marker, and that every id', + 'resolves to a reference entry in the README ## References section and in', + 'public/src/data/references.js. A reference entry that is never cited fails.', + '', + 'Exit codes: 0 = all claims and citations resolve, 1 = a check fails, 2 = usage or I/O error.' +].join('\n') + +/* ------------------------------------------------------------------ options */ + +function readOptions (argv) { + const takesValue = new Map([ + ['--readme', 'readme'], + ['--manifesto', 'manifesto'], + ['--assessment', 'assessment'], + ['--references', 'references'] + ]) + const options = { ...DEFAULTS } + + for (let index = 0; index < argv.length; index++) { + const argument = argv[index] + if (argument === '--help' || argument === '-h') { + options.help = true + continue + } + if (!argument.startsWith('-')) throw new Error(`unexpected argument: ${argument}`) + const equals = argument.indexOf('=') + const name = equals === -1 ? argument : argument.slice(0, equals) + if (!takesValue.has(name)) throw new Error(`unknown option: ${argument}`) + let value + if (equals === -1) { + value = argv[index + 1] + index += 1 + if (value === undefined || value.startsWith('-')) throw new Error(`${name} needs a value`) + } else { + value = argument.slice(equals + 1) + if (value === '') throw new Error(`${name} needs a value`) + } + options[takesValue.get(name)] = resolve(value) + } + return options +} + +/* --------------------------------------------------------------- primitives */ + +const CITATION_RE = /\[([a-z][a-z0-9]*(?:-[a-z0-9]+)*)]/g +const MARKER_RE = /BEAT default|BEAT'?s own|not a research finding|not research findings|not a measured threshold/i +const RESEARCH_RE = /\b(survey\w*|stud(?:y|ies|ied)|reported|participant\w*|respondent\w*|trial)\b/i +/** A research term under `not ...`/`no ...`/`never ...` is a disclaimer, not a claim. */ +const NEGATION_RE = /\b(not|no|never)\s+(?:\w+\s+){0,2}?(survey\w*|stud(?:y|ies|ied)|reported|participant\w*|respondent\w*|trial)/i +const HAS_DIGIT_RE = /\d/ +const HAS_PERCENT_RE = /%/ + +/** Bracketed inline citation ids, skipping markdown links `[text](url)`. */ +function citationsIn (text) { + const ids = [] + CITATION_RE.lastIndex = 0 + let match = CITATION_RE.exec(text) + while (match !== null) { + if (text[match.index + match[0].length] !== '(') ids.push(match[1]) + match = CITATION_RE.exec(text) + } + return ids +} + +/* -------------------------------------------------------------- README side */ + +/** + * Scan the README. Returns the reference entries of the `## References` + * section, every inline citation id used outside that section, and every + * checked claim line with its resolution. + */ +function scanReadme (markdown, label) { + const lines = markdown.split('\n') + const result = { label, references: [], citations: [], checked: [], skippedStructured: 0, totalLines: lines.length } + let inCodeFence = false + let inReferences = false + + for (let index = 0; index < lines.length; index++) { + const raw = lines[index] + const line = raw.trim() + const at = `${label}:${index + 1}` + + if (line.startsWith('```')) { + inCodeFence = !inCodeFence + continue + } + if (inCodeFence) { + result.skippedStructured += 1 + continue + } + if (/^##\s+References\s*$/.test(line)) { + inReferences = true + continue + } + if (inReferences) { + const entry = /^\s*-\s+\*\*\[([a-z][a-z0-9-]*)\]\*\*/.exec(raw) + if (entry) result.references.push({ id: entry[1], at, text: line }) + continue + } + if (line === '' || line.startsWith('