1// Tests of the pure core of the merge request page (static/js/mr.js). Run with
2// node --test 'internal/adapters/web/jstest/*.test.mjs'
3import assert from 'node:assert/strict';
4import { test } from 'node:test';
5
6globalThis.window = globalThis;
7globalThis.document = { readyState: 'complete', querySelector: () => null, querySelectorAll: () => [] };
8await import('../static/js/lazy.js');
9await import('../static/js/mr.js');
10const C = window.ermiteMR.core;
11
12test('windowOf: the start of the window of a file', () => {
13 assert.equal(C.windowOf('f1', 25), 0);
14 assert.equal(C.windowOf('f25', 25), 0);
15 assert.equal(C.windowOf('f26', 25), 25);
16 assert.equal(C.windowOf('f84', 25), 75);
17 assert.equal(C.windowOf('c3', 25), -1, 'not a file');
18 assert.equal(C.windowOf('f0', 25), -1);
19});
20
21test('menuKey: modes by number, D toggles the deletion, nothing else', () => {
22 assert.deepEqual(C.menuKey('1', 2), { mode: 0 });
23 assert.deepEqual(C.menuKey('2', 2), { mode: 1 });
24 assert.equal(C.menuKey('3', 2), null, 'no third mode');
25 assert.equal(C.menuKey('0', 2), null);
26 assert.deepEqual(C.menuKey('D', 2), { toggle: true });
27 assert.equal(C.menuKey('d', 2), null, 'd is the split layout');
28 assert.equal(C.menuKey('Enter', 2), null, 'the browser submits: the focus is on the button');
29 assert.equal(C.menuKey('M', 2), null);
30});
31
32test('step: stays in the list', () => {
33 assert.equal(C.step(0, 1, 3), 1);
34 assert.equal(C.step(2, 1, 3), 2);
35 assert.equal(C.step(0, -1, 3), 0);
36 assert.equal(C.step(-1, 1, 3), 0);
37 assert.equal(C.step(-1, -1, 3), 2);
38 assert.equal(C.step(0, 1, 0), -1);
39});
40
41test('rangeURL requires the same file, side and MR and sorts endpoints', () => {
42 const base = 'https://forge.test/demo/merge-requests/1';
43 const a = base+'?on=new:24:dir%2Fa.go#f1';
44 const b = base+'?from=25&on=new:10:dir%2Fa.go#f1';
45 const u = new URL(C.rangeURL(a,b,base));
46 assert.equal(u.searchParams.get('on'),'new:10-24:dir/a.go');
47 assert.equal(u.searchParams.get('from'),'25');
48 assert.equal(u.hash,'#f1');
49 assert.equal(C.rangeURL(a,a,base),null);
50 assert.equal(C.rangeURL(a,b.replace('new:','old:'),base),null);
51 assert.equal(C.rangeURL(a,b.replace('a.go','b.go'),base),null);
52 assert.equal(C.rangeURL(a,b.replace('/1?','/2?'),base),null);
53});
54
55test('quote opens the thread reply and prefills and focuses its textarea', () => {
56 let focused=false;
57 const field={value:'',focus(){focused=true;}};
58 const details={open:false,querySelector(){return field;}};
59 const thread={querySelector(){return details;}};
60 const button={dataset:{quote:'> source\n\n^2 '},closest(){return {closest(){return thread;}};}};
61 assert.equal(C.quote(button),true);
62 assert.equal(details.open,true);
63 assert.equal(field.value,'> source\n\n^2 ');
64 assert.equal(focused,true);
65});
66
67for (const dataset of [{diff:'split'}, {view:'split'}]) test(`reveal selects the split note (${JSON.stringify(dataset)}), including after lazy loading`, async () => {
68 const saved = globalThis.document;
69 const savedRAF = globalThis.requestAnimationFrame;
70 let scrolled = '';
71 const details = {open:false};
72 const node = id => ({id, querySelector(){return null;}, closest(){return details;}, scrollIntoView(){scrolled=id;}});
73 const nodes = new Map([['c12',node('c12')],['c12-s',node('c12-s')]]);
74 const ph = {dataset:{notes:'c12'}};
75 globalThis.document = {
76 querySelector: sel => sel === '#diffs' ? {dataset} : null,
77 querySelectorAll: () => [ph],
78 getElementById: id => nodes.get(id),
79 };
80 globalThis.requestAnimationFrame = fn => fn();
81 try {
82 assert.equal(window.ermiteMR.reveal('c12'),false);
83 assert.equal(scrolled,'c12-s');
84 assert.equal(details.open,true);
85 nodes.clear(); scrolled=''; details.open=false;
86 assert.equal(window.ermiteMR.reveal('c12'),true);
87 nodes.set('c12',node('c12')); nodes.set('c12-s',node('c12-s'));
88 await Promise.resolve();
89 assert.equal(scrolled,'c12-s');
90 assert.equal(details.open,true);
91 } finally {
92 globalThis.document=saved;
93 globalThis.requestAnimationFrame=savedRAF;
94 }
95});