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