Files

115 lines
6.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// A DOM-free smoke test of ui/src/diff-render.js (node tests/smoke_render.js):
// the global and its API, the pure helpers, and renderFiles / fileList /
// fileTree drawing into stub elements (their HTML checked as text).
"use strict";
const fs = require("fs");
const path = require("path");
const assert = require("assert");
function stubEl(doc) {
return {
ownerDocument: doc, innerHTML: "", listeners: {},
addEventListener(t, f) { (this.listeners[t] = this.listeners[t] || []).push(f); },
querySelectorAll() { return []; },
querySelector() { return null; },
};
}
const head = { children: [], appendChild(n) { this.children.push(n); } };
const doc = {
head, documentElement: head,
getElementById(id) { return head.children.find((n) => n.id === id) || null; },
createElement() { return { id: "", textContent: "", className: "", innerHTML: "" }; },
};
const win = { document: doc };
const src = fs.readFileSync(path.join(__dirname, "..", "ui", "src", "diff-render.js"), "utf8");
new Function("window", src)(win);
const D = win.AiaDiff;
// the contract's API
for (const k of ["css", "injectStyles", "fileList", "renderFiles", "stat", "esc"]) assert.ok(D[k] != null, "AiaDiff." + k);
assert.strictEqual(typeof D.css, "string");
assert.ok(D.css.includes("hsl(var(--"), "theme tokens in css");
D.injectStyles(doc); D.injectStyles(doc);
assert.strictEqual(head.children.length, 1, "styles injected once");
// helpers
assert.strictEqual(D.esc(`<a href="x">&'</a>`), "&lt;a href=&quot;x&quot;&gt;&amp;&#39;&lt;/a&gt;");
assert.strictEqual(D.fileKey({ repo: "project:x", path: "a/b.ts" }), "project:x|a/b.ts");
const f1 = {
path: "src/app.ts", status: "modified", additions: 2, deletions: 1, binary: false, truncated: false, repo: "project:demo",
hunks: [{ header: "@@ -1,3 +1,4 @@", oldStart: 1, oldLines: 3, newStart: 1, newLines: 4, lines: [
{ type: "ctx", text: "const a = 1;", oldNo: 1, newNo: 1 },
{ type: "del", text: "let b = 'x';", oldNo: 2, newNo: null },
{ type: "add", text: "let b = \"<y>\"; // note", oldNo: null, newNo: 2 },
{ type: "add", text: "call(b);", oldNo: null, newNo: 3 },
] }, { header: "@@ -20,2 +21,2 @@", oldStart: 20, oldLines: 2, newStart: 21, newLines: 2, lines: [
{ type: "ctx", text: "", oldNo: 20, newNo: 21 },
] }],
};
const sig = D.signature(f1);
assert.match(sig, /^2\/1\/2\/[0-9a-z]+$/);
assert.notStrictEqual(sig, D.signature({ ...f1, additions: 3 }), "a changed file changes its signature");
// the app's reviewStore.fileSignature, same input → same output
let h = 0;
for (const hk of f1.hunks) for (let i = 0; i < hk.header.length; i++) h = (h * 31 + hk.header.charCodeAt(i)) | 0;
assert.strictEqual(sig, `2/1/2/${(h >>> 0).toString(36)}`);
const hl = D.highlight("let b = \"<y>\"; // note", "x.ts");
assert.ok(hl.includes('<span class="aiad-k">let</span>'), hl);
assert.ok(hl.includes("&lt;y&gt;") && !hl.includes("<y>"), "highlight escapes");
assert.ok(hl.includes('<span class="aiad-c">// note</span>'), hl);
assert.strictEqual(D.highlight("<b>", "x.unknown"), "&lt;b&gt;");
assert.ok(D.highlight("# c", "a.py").includes("aiad-c"));
assert.strictEqual(D.t("fr", "Viewed"), "Vu");
assert.ok(D.isImage("a/b.PNG") && !D.isImage("a.ts"));
assert.strictEqual(D.statHtml(0, 0), "");
assert.ok(D.statHtml(12, 3).includes("+12") && D.statHtml(12, 3).includes("−3"));
const st = D.stat(1, 2);
assert.ok(st.innerHTML.includes("+1") && st.innerHTML.includes("−2"));
// renderFiles: hunks, gap bar, comments, viewed, French
const img = { path: "logo.png", status: "added", additions: 0, deletions: 0, binary: true, truncated: false, hunks: [], repo: "project:demo" };
const bin = { path: "x.bin", status: "modified", additions: 0, deletions: 0, binary: true, truncated: false, hunks: [] };
const el = stubEl(doc);
const handle = D.renderFiles(el, [f1, img, bin], {
lang: "fr",
viewed: {},
comments: [{ path: "src/app.ts", line: 2, side: "new", text: "why <this>?" }, { path: "src/app.ts", text: "whole" }],
onComment() {}, onDeleteComment() {}, onToggleViewed() {},
imageUrl: () => Promise.resolve(null),
});
assert.ok(handle && typeof handle.open === "function");
const out = el.innerHTML;
assert.ok(out.includes("aiad-a") && out.includes("aiad-d"), "add/del rows");
assert.ok(out.includes("16 lignes inchangées"), "gap bar in French: " + (out.match(/aiad-gap[^]*?<\/div>/) || [""])[0]);
assert.ok(out.includes("why &lt;this&gt;?"), "line comment drawn, escaped");
assert.ok(out.includes("aiad-marked"), "commented line marked");
assert.ok(out.includes("fichier entier"), "file comment label");
assert.ok(out.includes(">Vu<"), "Viewed checkbox");
assert.ok(out.includes("Fichier binaire"), "binary note");
assert.ok(out.includes('data-side="old"') === false && out.includes('data-side="new"'), "added image: the after tile only");
assert.strictEqual(el.listeners.click.length, 1);
D.renderFiles(el, [f1], {});
assert.strictEqual(el.listeners.click.length, 1, "listeners bound once");
assert.ok(!el.innerHTML.includes("aiad-viewed"), "no Viewed box without onToggleViewed");
assert.ok(!el.innerHTML.includes('data-act="cline"'), "no comment gutter without onComment");
// viewed files start folded; maxLines caps the body
const el2 = stubEl(doc);
D.renderFiles(el2, [f1], { viewed: { "src/app.ts": true }, onToggleViewed() {} });
assert.ok(!el2.innerHTML.includes("aiad-code"), "a viewed file starts folded");
const el3 = stubEl(doc);
D.renderFiles(el3, [f1], { maxLines: 2, wrap: false });
assert.ok(el3.innerHTML.includes("… 3 more lines") && el3.innerHTML.includes("aiad-nowrap"));
// fileList + fileTree
const el4 = stubEl(doc);
D.fileList(el4, [f1, { ...f1, path: "README.md", repo: null }], { viewed: { "project:demo|src/app.ts": true }, comments: [{ path: "README.md", text: "x" }] });
assert.ok(el4.innerHTML.includes("aiad-seen") && el4.innerHTML.includes("aiad-cc"));
const el5 = stubEl(doc);
D.fileTree(el5, [f1, { ...f1, path: "src/lib/x.ts" }, { ...f1, path: "README.md", status: "added" }], {});
const tree = el5.innerHTML;
assert.ok(tree.indexOf("src") < tree.indexOf("README.md"), "dirs first");
assert.ok(tree.includes("aiad-ic-add"));
console.log("diff-render smoke: ok");