| 1 | 'use strict'; |
| 2 | |
| 3 | const express = require('express'); |
| 4 | const http = require('http'); |
| 5 | const path = require('path'); |
| 6 | const { once } = require('events'); |
| 7 | const { webcrypto } = require('crypto'); |
| 8 | |
| 9 | describe('page load smoke test', () => |
| 10 | { |
| 11 | let server; |
| 12 | let baseUrl; |
| 13 | let JSDOM; |
| 14 | let VirtualConsole; |
| 15 | let rootsFixture; |
| 16 | let listFixture; |
| 17 | let spaceFixture; |
| 18 | let meFixture; |
| 19 | |
| 20 | beforeEach(() => |
| 21 | { |
| 22 | rootsFixture = [{ label: 'Root', path: '/data' }]; |
| 23 | listFixture = { |
| 24 | '/data': [ |
| 25 | { |
| 26 | name: 'readme.txt', |
| 27 | path: '/data/readme.txt', |
| 28 | isDir: false, |
| 29 | size: 12, |
| 30 | mtime: Date.now(), |
| 31 | }, |
| 32 | ], |
| 33 | }; |
| 34 | spaceFixture = { |
| 35 | '/data': { usedBytes: 0, freeBytes: 1024 * 1024 }, |
| 36 | }; |
| 37 | meFixture = { username: 'tester', isAdmin: false, permissions: {} }; |
| 38 | }); |
| 39 | |
| 40 | beforeAll(async () => |
| 41 | { |
| 42 | ({ JSDOM, VirtualConsole } = require('jsdom')); |
| 43 | |
| 44 | const app = express(); |
| 45 | const publicDir = path.join(__dirname, '../public'); |
| 46 | |
| 47 | app.use(express.json()); |
| 48 | app.use(express.static(publicDir)); |
| 49 | |
| 50 | app.get('/api/auth/server-key', (req, res) => res.json({ publicKey: null })); |
| 51 | app.get('/api/auth/setup-needed', (req, res) => res.json({ needed: false })); |
| 52 | app.get('/api/auth/me', (req, res) => res.json(meFixture)); |
| 53 | app.get('/api/files/roots', (req, res) => res.json({ roots: rootsFixture })); |
| 54 | app.get('/api/files/list', (req, res) => |
| 55 | { |
| 56 | const dir = req.query.dir; |
| 57 | res.json({ items: listFixture[dir] || [] }); |
| 58 | }); |
| 59 | app.get('/api/files/space', (req, res) => |
| 60 | { |
| 61 | const dir = req.query.dir; |
| 62 | const stats = spaceFixture[dir] || { usedBytes: 0, freeBytes: null }; |
| 63 | res.json(stats); |
| 64 | }); |
| 65 | app.post('/api/auth/logout', (req, res) => res.json({ ok: true })); |
| 66 | app.get('*', (req, res) => res.sendFile(path.join(publicDir, 'index.html'))); |
| 67 | |
| 68 | server = http.createServer(app); |
| 69 | server.listen(0); |
| 70 | await once(server, 'listening'); |
| 71 | baseUrl = `http://127.0.0.1:${server.address().port}`; |
| 72 | }); |
| 73 | |
| 74 | afterAll(() => server.close()); |
| 75 | |
| 76 | async function bootClient ({ mobile = false, savedSession } = {}) |
| 77 | { |
| 78 | const virtualConsole = new VirtualConsole(); |
| 79 | const runtimeErrors = []; |
| 80 | |
| 81 | virtualConsole.on('jsdomError', (err) => |
| 82 | { |
| 83 | runtimeErrors.push(err); |
| 84 | }); |
| 85 | |
| 86 | const dom = await JSDOM.fromURL(baseUrl + '/', { |
| 87 | resources: 'usable', |
| 88 | runScripts: 'dangerously', |
| 89 | pretendToBeVisual: true, |
| 90 | virtualConsole, |
| 91 | beforeParse (window) |
| 92 | { |
| 93 | window.crypto = webcrypto; |
| 94 | window.fetch = (input, init) => |
| 95 | { |
| 96 | const url = typeof input === 'string' ? input : input.url; |
| 97 | return fetch(new URL(url, baseUrl).toString(), init); |
| 98 | }; |
| 99 | window.HTMLElement.prototype.focus = function focus () { }; |
| 100 | window.HTMLCanvasElement.prototype.getContext = () => null; |
| 101 | window.matchMedia = () => ({ matches: mobile, addListener () { }, removeListener () { }, addEventListener () { }, removeEventListener () { } }); |
| 102 | window.scrollTo = () => { }; |
| 103 | |
| 104 | const defaultSaved = { |
| 105 | token: 'saved-token', |
| 106 | dir: '/data', |
| 107 | rootPath: '/data', |
| 108 | lastOperationAt: Date.now(), |
| 109 | }; |
| 110 | const effectiveSaved = savedSession === undefined ? defaultSaved : savedSession; |
| 111 | if (effectiveSaved) |
| 112 | { |
| 113 | window.localStorage.setItem('filemanager.session.v1', JSON.stringify(effectiveSaved)); |
| 114 | } |
| 115 | window.addEventListener('error', (event) => runtimeErrors.push(event.error || new Error(event.message))); |
| 116 | window.addEventListener('unhandledrejection', (event) => runtimeErrors.push(event.reason || new Error('Unhandled rejection'))); |
| 117 | }, |
| 118 | }); |
| 119 | |
| 120 | await new Promise((resolve) => dom.window.addEventListener('load', resolve, { once: true })); |
| 121 | await new Promise(resolve => dom.window.setTimeout(resolve, 100)); |
| 122 | |
| 123 | return { dom, runtimeErrors }; |
| 124 | } |
| 125 | |
| 126 | test('requests / and boots the client without runtime errors', async () => |
| 127 | { |
| 128 | const { dom, runtimeErrors } = await bootClient(); |
| 129 | |
| 130 | expect(runtimeErrors).toEqual([]); |
| 131 | expect(dom.window.document.getElementById('app').classList.contains('hidden')).toBe(false); |
| 132 | expect(dom.window.document.getElementById('sidebar').classList.contains('hidden')).toBe(false); |
| 133 | |
| 134 | dom.window.close(); |
| 135 | }); |
| 136 | |
| 137 | test('keeps sidebar folded on initial mobile boot', async () => |
| 138 | { |
| 139 | const { dom, runtimeErrors } = await bootClient({ mobile: true }); |
| 140 | |
| 141 | expect(runtimeErrors).toEqual([]); |
| 142 | expect(dom.window.document.getElementById('app').classList.contains('hidden')).toBe(false); |
| 143 | expect(dom.window.document.getElementById('sidebar').classList.contains('hidden')).toBe(true); |
| 144 | |
| 145 | dom.window.close(); |
| 146 | }); |
| 147 | |
| 148 | test('suppresses default context menu on mobile file long-press', async () => |
| 149 | { |
| 150 | const { dom, runtimeErrors } = await bootClient({ mobile: true }); |
| 151 | |
| 152 | expect(runtimeErrors).toEqual([]); |
| 153 | |
| 154 | const tile = dom.window.document.querySelector('.file-item'); |
| 155 | expect(tile).toBeTruthy(); |
| 156 | |
| 157 | const evt = new dom.window.MouseEvent('contextmenu', { |
| 158 | bubbles: true, |
| 159 | cancelable: true, |
| 160 | view: dom.window, |
| 161 | }); |
| 162 | |
| 163 | const dispatchResult = tile.dispatchEvent(evt); |
| 164 | expect(dispatchResult).toBe(false); |
| 165 | expect(evt.defaultPrevented).toBe(true); |
| 166 | |
| 167 | dom.window.close(); |
| 168 | }); |
| 169 | |
| 170 | test('in Home, root tiles single-click focus and double-click open', async () => |
| 171 | { |
| 172 | rootsFixture = [ |
| 173 | { label: 'Root A', path: '/data-a' }, |
| 174 | { label: 'Root B', path: '/data-b' }, |
| 175 | ]; |
| 176 | listFixture = { |
| 177 | '/data-a': [ |
| 178 | { |
| 179 | name: 'docs', |
| 180 | path: '/data-a/docs', |
| 181 | isDir: true, |
| 182 | size: 0, |
| 183 | mtime: Date.now(), |
| 184 | }, |
| 185 | ], |
| 186 | }; |
| 187 | spaceFixture = { |
| 188 | '/data-a': { usedBytes: 2048, freeBytes: 4096 }, |
| 189 | '/data-b': { usedBytes: 1024, freeBytes: 8192 }, |
| 190 | }; |
| 191 | |
| 192 | const { dom, runtimeErrors } = await bootClient({ |
| 193 | savedSession: { |
| 194 | token: 'saved-token', |
| 195 | dir: null, |
| 196 | rootPath: null, |
| 197 | lastOperationAt: Date.now(), |
| 198 | }, |
| 199 | }); |
| 200 | expect(runtimeErrors).toEqual([]); |
| 201 | |
| 202 | const rootTile = dom.window.document.querySelector('.file-item[data-path="/data-a"]'); |
| 203 | expect(rootTile).toBeTruthy(); |
| 204 | |
| 205 | rootTile.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true })); |
| 206 | await new Promise(resolve => dom.window.setTimeout(resolve, 40)); |
| 207 | |
| 208 | expect(rootTile.classList.contains('selected')).toBe(true); |
| 209 | expect(dom.window.document.querySelector('.file-item[data-path="/data-b"]')).toBeTruthy(); |
| 210 | expect(dom.window.document.getElementById('sidebar-meta').textContent).toContain('Used'); |
| 211 | expect(dom.window.document.getElementById('sidebar-meta').textContent).toContain('Remaining'); |
| 212 | |
| 213 | rootTile.dispatchEvent(new dom.window.MouseEvent('dblclick', { bubbles: true })); |
| 214 | await new Promise(resolve => dom.window.setTimeout(resolve, 80)); |
| 215 | |
| 216 | expect(dom.window.document.querySelector('.file-item[data-path="/data-a/docs"]')).toBeTruthy(); |
| 217 | |
| 218 | dom.window.close(); |
| 219 | }); |
| 220 | |
| 221 | test('root sidebar shows alias and original name, and excludes Move to action', async () => |
| 222 | { |
| 223 | rootsFixture = [ |
| 224 | { label: 'Shared', path: '/data-a', originalName: 'data-a', alias: 'Shared' }, |
| 225 | { label: 'Root B', path: '/data-b', originalName: 'data-b', alias: null }, |
| 226 | ]; |
| 227 | meFixture = { |
| 228 | username: 'tester', |
| 229 | isAdmin: false, |
| 230 | permissions: { |
| 231 | 'data-a': { read: true, write: true }, |
| 232 | 'data-b': { read: true, write: false }, |
| 233 | }, |
| 234 | }; |
| 235 | spaceFixture = { |
| 236 | '/data-a': { usedBytes: 1024, freeBytes: 2048 }, |
| 237 | '/data-b': { usedBytes: 2048, freeBytes: 1024 }, |
| 238 | }; |
| 239 | |
| 240 | const { dom, runtimeErrors } = await bootClient({ |
| 241 | savedSession: { |
| 242 | token: 'saved-token', |
| 243 | dir: null, |
| 244 | rootPath: null, |
| 245 | lastOperationAt: Date.now(), |
| 246 | }, |
| 247 | }); |
| 248 | |
| 249 | expect(runtimeErrors).toEqual([]); |
| 250 | |
| 251 | const rootTile = dom.window.document.querySelector('.file-item[data-path="/data-a"]'); |
| 252 | expect(rootTile).toBeTruthy(); |
| 253 | rootTile.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true })); |
| 254 | await new Promise(resolve => dom.window.setTimeout(resolve, 40)); |
| 255 | |
| 256 | const sidebarText = dom.window.document.getElementById('sidebar-meta').textContent; |
| 257 | expect(sidebarText).toContain('Alias'); |
| 258 | expect(sidebarText).toContain('Shared'); |
| 259 | expect(sidebarText).toContain('Original name'); |
| 260 | expect(sidebarText).toContain('data-a'); |
| 261 | |
| 262 | const actionButtons = Array.from(dom.window.document.querySelectorAll('#sidebar-actions button')).map(b => b.textContent); |
| 263 | expect(actionButtons).toContain('Rename'); |
| 264 | expect(actionButtons).not.toContain('Move to'); |
| 265 | expect(actionButtons).not.toContain('Delete'); |
| 266 | |
| 267 | dom.window.close(); |
| 268 | }); |
| 269 | }); |