Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | 34x 51x 51x 34x 34x 34x 19x 19x 15x 15x 34x 34x 19x 19x 19x 19x 27x 27x 3x 6x 3x 19x 27x 24x 38x 19x 2x 2x 3x 2x 2x 19x 19x 27x 27x 27x 27x 25x 25x 35x 35x 25x 25x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 15x 3x 2x 15x 15x 47x 12x 12x 12x 12x 27x 38x 45x 38x 38x 38x 38x 38x 36x 36x 38x 27x 27x 27x 27x 17x 27x 19x 25x 17x 15x 4x 19x 38x 19x 19x 82x 19x | /**
* Core drift audit: compare fetched Zeroheight pages against local content
* and the previous manifest. Pure apart from the injected `readLocal`.
*/
import { hashContent } from './hash.js';
import { normName } from './pages.js';
import { similarity } from './similarity.js';
/**
* @typedef {Object} ZhTab
* @property {string} name
* @property {string | null} uid
* @property {number | null} order
* @property {boolean} hidden
* @property {string} content
*
* @typedef {Object} ZhPage
* @property {string} uid
* @property {number} id
* @property {string} name
* @property {string[]} categoryPath
* @property {boolean} hidden
* @property {'mcp' | 'api'} source where `content`/tab content came from
* @property {string} raw full page payload used for the page hash
* @property {string} content untabbed page body ('' for tabbed pages)
* @property {ZhTab[]} tabs display order
*
* @typedef {Object} Finding
* @property {string} uid
* @property {string} name
* @property {string} category
* @property {boolean} hidden
* @property {string} decision
* @property {string | null} tab
* @property {string | null} localFile
* @property {string[]} status
* @property {number} [similarity]
*
* @typedef {import('./match.js').PageMatch | { matchedBy: 'manual', localFile: string | null, tabs: Map<string, string | null> }} AnyMatch
*/
/** Strip frontmatter, return trimmed body. @param {string} text */
const bodyOf = (text) => text.replace(/^---\n[\s\S]*?\n---\n?/, '').trim();
/**
* Status flags for one comparable unit (an untabbed page or one tab).
* @param {{ zhContent: string, zhHash: string, localFile: string | null, localText: string | null,
* prev: { zhHash: string | null, localHash: string | null } | null, threshold: number }} u
*/
export function unitStatus({ zhContent, zhHash, localFile, localText, prev, threshold }) {
/** @type {string[]} */
const status = [];
/** @type {number | undefined} */
let score;
if (!localFile) return { status: ['unmatched'], score };
Iif (localText === null) return { status: ['missing-local'], score };
const localHash = hashContent(localText);
if (prev?.zhHash) {
if (prev.zhHash !== zhHash) status.push('changed');
if (prev.localHash && prev.localHash !== localHash) status.push('locally-edited');
} else {
score = Math.round(similarity(zhContent, localText) * 100) / 100;
if (score < threshold) status.push('likely-changed');
}
if (!bodyOf(localText) && zhContent.trim()) status.push('empty-export');
return { status, score };
}
/**
* @param {{
* zhPages: ZhPage[],
* previous: import('./manifest.js').Manifest,
* releaseId: number | null,
* match: (page: ZhPage) => import('./match.js').PageMatch,
* readLocal: (file: string) => string | null,
* localFiles: string[],
* threshold: number,
* today: string,
* partial?: boolean,
* }} opts
*/
export function runAudit({
zhPages,
previous,
releaseId,
match,
readLocal,
localFiles,
threshold,
today,
partial = false,
}) {
/** @type {Record<string, import('./manifest.js').ManifestEntry>} */
const pages = partial ? { ...previous.pages } : {};
/** @type {Finding[]} */
const findings = [];
/** @type {Map<string, AnyMatch>} */
const matches = new Map();
for (const zh of zhPages) {
const prev = previous.pages[zh.uid];
matches.set(
zh.uid,
prev?.matchedBy === 'manual'
? {
matchedBy: 'manual',
localFile: prev.localFile,
tabs: new Map(
zh.tabs.map((t) => {
// Pinned tabs follow their UID, so a Zeroheight rename keeps the mapping.
const pinned = prev.tabs.find(
(p) => (t.uid && p.uid === t.uid) || normName(p.name) === normName(t.name),
);
return [normName(t.name), pinned?.localFile ?? null];
}),
),
}
: match(zh),
);
}
// Hidden drafts often share a name with a visible page (Empty State, Toast):
// the visible page owns the file.
const visibleFiles = new Set(
zhPages
.filter((zh) => !zh.hidden)
.flatMap((zh) => {
const m = /** @type {AnyMatch} */ (matches.get(zh.uid));
return [m.localFile, ...zh.tabs.filter((t) => !t.hidden).map((t) => m.tabs.get(normName(t.name)))];
})
.filter(Boolean),
);
if (partial) {
// Pages left out of a partial run still own their files.
const fetched = new Set(zhPages.map((zh) => zh.uid));
for (const [uid, entry] of Object.entries(previous.pages)) {
if (fetched.has(uid) || entry.hidden) continue;
for (const file of [entry.localFile, ...entry.tabs.filter((t) => !t.hidden).map((t) => t.localFile)]) {
Eif (file) visibleFiles.add(file);
}
}
}
/** @param {string | null | undefined} file */
const ownFile = (file) => (file && !visibleFiles.has(file) ? file : null);
for (const zh of zhPages) {
const prev = previous.pages[zh.uid] ?? null;
const matched = /** @type {AnyMatch} */ (matches.get(zh.uid));
/** @type {AnyMatch} */
let m = matched;
if (matched.matchedBy !== 'manual') {
const localFile = zh.hidden ? ownFile(matched.localFile) : matched.localFile;
const tabs = new Map(
zh.tabs.map((t) => {
const file = matched.tabs.get(normName(t.name)) ?? null;
return [normName(t.name), zh.hidden || t.hidden ? ownFile(file) : file];
}),
);
const found = localFile !== null || [...tabs.values()].some(Boolean);
m = { matchedBy: found ? matched.matchedBy : null, localFile, tabs };
}
const decision = prev?.decision ?? (zh.hidden ? 'skip' : 'port');
const category = zh.categoryPath.join(' / ');
const base = { uid: zh.uid, name: zh.name, category, hidden: zh.hidden, decision };
const landingText = m.localFile ? readLocal(m.localFile) : null;
const landingHash = landingText === null ? null : hashContent(landingText);
const zhHash = hashContent(zh.raw);
// Some tabbed Zeroheight pages were ported as a single file (Components):
// compare that file against all visible tabs combined.
const flattened =
zh.tabs.length > 0 && m.localFile !== null && zh.tabs.every((t) => !m.tabs.get(normName(t.name)));
/** @type {Finding[]} */
const pageFindings = [];
/** @type {import('./manifest.js').ManifestTab[]} */
const tabs = [];
if (zh.tabs.length === 0 || flattened) {
const zhContent = flattened
? zh.tabs
.filter((t) => !t.hidden)
.map((t) => t.content)
.join('\n\n')
: zh.content;
const { status, score } = unitStatus({
zhContent,
zhHash,
localFile: m.localFile,
localText: landingText,
prev: prev && { zhHash: prev.zhHash, localHash: prev.localHash },
threshold,
});
pageFindings.push({ ...base, tab: null, localFile: m.localFile, status, similarity: score });
} else {
// Tab hashes don't see tabs being reordered or removed, so compare the tab list itself.
/** @param {{ uid: string | null, name: string }} t */
const tabKey = (t) => t.uid ?? normName(t.name);
const restructured = prev && zh.tabs.map(tabKey).join() !== prev.tabs.map(tabKey).join();
// Tabbed landing file has no Zeroheight counterpart; only watch it for local edits.
const edited = m.localFile && prev?.localHash && landingHash && prev.localHash !== landingHash;
const status = [...(restructured ? ['changed'] : []), ...(edited ? ['locally-edited'] : [])];
if (status.length) pageFindings.push({ ...base, tab: null, localFile: m.localFile, status });
}
for (const tab of zh.tabs) {
const prevTab = prev?.tabs.find(
(t) => (tab.uid && t.uid === tab.uid) || normName(t.name) === normName(tab.name),
);
const localFile = m.tabs.get(normName(tab.name)) ?? null;
const localText = localFile ? readLocal(localFile) : null;
const tabHash = hashContent(tab.content);
const tabDecision = tab.hidden ? (prevTab?.decision ?? 'skip') : undefined;
// Flattened pages already compared their visible tabs as one unit above.
if (!flattened || tab.hidden) {
const { status, score } = unitStatus({
zhContent: tab.content,
zhHash: tabHash,
localFile,
localText,
prev: prevTab ?? null,
threshold,
});
pageFindings.push({
...base,
hidden: zh.hidden || tab.hidden,
decision: tabDecision ?? decision,
tab: tab.name,
localFile,
status,
similarity: score,
});
}
tabs.push({
name: tab.name,
uid: tab.uid,
order: tab.order,
hidden: tab.hidden,
decision: tabDecision,
localFile,
zhHash: tabHash,
localHash: localText === null ? null : hashContent(localText),
});
}
// Nothing ported: one line per page instead of an "unmatched" line per tab.
const nothingPorted = !m.localFile && tabs.every((t) => !t.localFile);
const pageStatus = prev ? 'unmatched' : 'new';
findings.push(
...(nothingPorted ? [{ ...base, tab: null, localFile: null, status: [pageStatus] }] : pageFindings),
);
const unchanged =
prev &&
prev.zhHash === zhHash &&
prev.localHash === landingHash &&
tabs.length === prev.tabs.length &&
tabs.every((t, i) => t.zhHash === prev.tabs[i].zhHash && t.localHash === prev.tabs[i].localHash);
pages[zh.uid] = {
id: zh.id,
name: zh.name,
category,
hidden: zh.hidden,
decision,
matchedBy: m.matchedBy,
localFile: m.localFile,
zhHash,
localHash: landingHash,
hashSource: zh.source,
lastSynced: unchanged ? prev.lastSynced : today,
tabs,
notes: prev?.notes,
};
}
if (!partial) {
const seen = new Set(zhPages.map((p) => p.uid));
for (const [uid, prev] of Object.entries(previous.pages)) {
if (seen.has(uid)) continue;
findings.push({
uid,
name: prev.name,
category: prev.category,
hidden: prev.hidden,
decision: prev.decision,
tab: null,
localFile: prev.localFile,
status: ['deleted'],
});
}
}
const referenced = new Set(
Object.values(pages).flatMap((p) => [p.localFile, ...p.tabs.map((t) => t.localFile)].filter(Boolean)),
);
const siteOnly = previous.siteOnly;
const orphans = partial
? []
: localFiles.filter((f) => !referenced.has(f) && !siteOnly.some((prefix) => f.startsWith(prefix))).sort();
return { manifest: { releaseId, siteOnly, pages }, findings, orphans };
}
|