detached 973e682

lazy.test.mjs RawHistoryFolder

1// Tests of the loader of the files of a diff (static/js/lazy.js). Run with
2//   node --test 'internal/adapters/web/jstest/*.test.mjs'
3import assert from 'node:assert/strict';
4import { test } from 'node:test';
5import { readFileSync } from 'node:fs';
6
7const SRC = readFileSync(new URL('../static/js/lazy.js', import.meta.url), 'utf8');
8
9globalThis.window = globalThis;
10const saved = { document: globalThis.document, fetch: globalThis.fetch, IO: globalThis.IntersectionObserver };
11
12// A placeholder: an element with a dataset; its link finds it back.
13function placeholder(src, manual) {
14  const details = { id: 'f1', querySelector: () => null };
15  const ph = { dataset: manual ? { src, manual: '' } : { src }, setAttribute() {}, removeAttribute() {}, closest: (s) => (s === 'details' ? details : null) };
16  const link = { closest: (s) => (s === '.dlazy' ? ph : s === '.dlazy[data-manual] > a' && manual ? link : null) };
17  return { ph, link, details };
18}
19
20async function setup(phs, io = true, opts = {}) {
21  const observed = [];
22  const urls = [];
23  if (io) globalThis.IntersectionObserver = class { observe(p) { observed.push(p); } unobserve() {} };
24  else delete globalThis.IntersectionObserver;
25  const reply = opts.reply || { status: 200, ok: true, body: '' };
26  globalThis.fetch = async (u) => { urls.push(u); return { status: reply.status, ok: reply.ok, text: async () => reply.body }; };
27  globalThis.document = {
28    readyState: 'complete',
29    querySelector: (s) => (s === '#diffs' ? { dataset: {} } : null),
30    querySelectorAll: (s) => (s.includes(':not([data-manual])') ? phs.filter((p) => !('manual' in p.dataset)) : phs),
31    getElementById: opts.getElementById || (() => null),
32    createElement: () => ({ content: { querySelectorAll: () => opts.files || [] } }),
33    addEventListener() {},
34  };
35  delete globalThis.ermiteLazy;
36  (0, eval)(SRC);
37  return { observed, urls };
38}
39const settle = () => new Promise((r) => setTimeout(r, 0));
40const restore = () => {
41  delete globalThis.ermiteLazy;
42  for (const [k, v] of [['document', saved.document], ['fetch', saved.fetch], ['IntersectionObserver', saved.IO]]) {
43    if (v === undefined) delete globalThis[k]; else globalThis[k] = v;
44  }
45};
46
47test('manual placeholders are not observed', async () => {
48  const a = placeholder('/r/commit/abc/files?one=1&from=0', true);
49  const b = placeholder('/r/merge-requests/1/files?from=25', false);
50  try {
51    const { observed, urls } = await setup([a.ph, b.ph]);
52    assert.deepEqual(observed, [b.ph]);
53    assert.deepEqual(urls, []);
54  } finally { await settle(); restore(); }
55});
56
57test('without IntersectionObserver only the automatic placeholders load', async () => {
58  const a = placeholder('/m?one=1&from=0', true);
59  const b = placeholder('/w?from=25', false);
60  try {
61    const { urls } = await setup([a.ph, b.ph], false);
62    assert.deepEqual(urls, ['/w?from=25']);
63  } finally { await settle(); restore(); }
64});
65
66test('a click on "load diff" loads that one file and does not navigate', async () => {
67  const a = placeholder('/r/commit/abc/files?one=1&from=3', true);
68  try {
69    const { urls } = await setup([a.ph]);
70    let prevented = false;
71    const used = window.ermiteLazy.onClick({ target: a.link, button: 0, preventDefault() { prevented = true; } });
72    assert.equal(used, true);
73    assert.equal(prevented, true);
74    assert.deepEqual(urls, ['/r/commit/abc/files?one=1&from=3']);
75  } finally { await settle(); restore(); }
76});
77
78test('Enter and Space on the link load it; other keys and modified clicks do not', async () => {
79  const a = placeholder('/f?one=1&from=1', true);
80  try {
81    const { urls } = await setup([a.ph]);
82    const key = (k, extra = {}) => window.ermiteLazy.onKey({ target: a.link, key: k, preventDefault() {}, ...extra });
83    assert.equal(key('a'), false);
84    assert.equal(key('Enter', { ctrlKey: true }), false);
85    assert.equal(window.ermiteLazy.onClick({ target: a.link, button: 0, ctrlKey: true, preventDefault() {} }), false);
86    assert.equal(urls.length, 0);
87    assert.equal(key('Enter'), true);
88    assert.equal(key(' '), true);
89    assert.equal(urls.length, 1, 'a file is fetched once');
90  } finally { await settle(); restore(); }
91});
92
93test('a manual placeholder without an address is a plain link', async () => {
94  const a = placeholder('', true);
95  a.ph.dataset.src = '';
96  try {
97    await setup([a.ph]);
98    assert.equal(window.ermiteLazy.onClick({ target: a.link, button: 0, preventDefault() { assert.fail(); } }), false);
99  } finally { await settle(); restore(); }
100});
101
102// A file of the page (old) or of a fragment (new), with the queries swap makes.
103function fileEl(id, { lazy = false, manual = false, open = false } = {}) {
104  const f = { id, open, queried: [] };
105  f.querySelector = (sel) => {
106    if (sel === '.dlazy[data-manual]') return manual ? {} : null;
107    if (sel === '.dlazy') return lazy || manual ? {} : null;
108    if (sel === '.ln a') return f.line || null;
109    return null;
110  };
111  f.querySelectorAll = () => [];
112  return f;
113}
114
115test('a loaded manual file opens and takes the focus without scrolling', async () => {
116  const a = placeholder('/f?one=1&from=0', true);
117  const old = fileEl('f1', { manual: true });
118  const fresh = fileEl('f1');
119  const focused = [];
120  fresh.line = { focus: (o) => focused.push(o) };
121  let current = old;
122  old.replaceWith = (n) => { current = n; };
123  try {
124    await setup([a.ph], true, { reply: { status: 200, ok: true, body: '<x>' }, files: [fresh], getElementById: () => current });
125    window.ermiteLazy.onClick({ target: a.link, button: 0, preventDefault() {} });
126    await settle(); await settle();
127    assert.equal(current, fresh);
128    assert.equal(fresh.open, true);
129    assert.deepEqual(focused, [{ preventScroll: true }]);
130  } finally { await settle(); restore(); }
131});
132
133test('a window that still holds a collapsed file keeps it closed', async () => {
134  const w = placeholder('/w?from=25', false);
135  const old = fileEl('f26', { lazy: true, open: true });
136  const fresh = fileEl('f26', { manual: true, open: true });
137  let replaced = null;
138  old.replaceWith = (n) => { replaced = n; };
139  try {
140    await setup([w.ph], true, { reply: { status: 200, ok: true, body: '<x>' }, files: [fresh], getElementById: () => old });
141    await window.ermiteLazy.load(w.ph);
142    assert.equal(replaced, fresh);
143    assert.equal(fresh.open, false);
144  } finally { await settle(); restore(); }
145});
146
147test('a 409 makes the page stale: no later load fetches', async () => {
148  const a = placeholder('/f?one=1&from=0', true);
149  const b = placeholder('/f?one=1&from=1', true);
150  try {
151    const { urls } = await setup([a.ph, b.ph], true, { reply: { status: 409, ok: false, body: '<p>moved</p>' } });
152    await window.ermiteLazy.load(a.ph);
153    assert.equal(a.ph.innerHTML, '<p>moved</p>');
154    assert.equal(urls.length, 1);
155    await window.ermiteLazy.load(b.ph);
156    assert.equal(urls.length, 1, 'stale: nothing is fetched');
157  } finally { await settle(); restore(); }
158});
159
160test('a failed load clears the loading mark and can be retried', async () => {
161  const a = placeholder('/f?one=1&from=0', true);
162  const marks = new Set();
163  a.ph.setAttribute = (n) => marks.add(n);
164  a.ph.removeAttribute = (n) => marks.delete(n);
165  try {
166    const { urls } = await setup([a.ph], true, { reply: { status: 500, ok: false, body: 'boom' } });
167    const p = window.ermiteLazy.load(a.ph);
168    assert.ok(marks.has('data-loading'));
169    await p;
170    assert.equal(marks.has('data-loading'), false);
171    await window.ermiteLazy.load(a.ph);
172    assert.equal(urls.length, 2, 'a retry fetches again');
173  } finally { await settle(); restore(); }
174});
175
176test('Space is prevented on the link; a held key is ignored', async () => {
177  const a = placeholder('/f?one=1&from=1', true);
178  try {
179    const { urls } = await setup([a.ph]);
180    let prevented = 0;
181    const ev = (extra) => ({ target: a.link, key: ' ', preventDefault() { prevented++; }, ...extra });
182    assert.equal(window.ermiteLazy.onKey(ev({ repeat: true })), false);
183    assert.equal(prevented, 0);
184    assert.equal(urls.length, 0);
185    assert.equal(window.ermiteLazy.onKey(ev({})), true);
186    assert.equal(prevented, 1, 'Space does not scroll the page');
187    assert.equal(urls.length, 1);
188  } finally { await settle(); restore(); }
189});

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 4

Tags 45

↑↓ moveenter openesc close