| 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 | let thumbnailStatusFixture; |
| 20 | let rootAccessFixture; |
| 21 | let folderIconFixture; |
| 22 | let adminUsersFixture; |
| 23 | let adminConfigFixture; |
| 24 | let downloadFixture; |
| 25 | const openDoms = []; |
| 26 | |
| 27 | beforeEach(() => |
| 28 | { |
| 29 | rootsFixture = [{ label: 'Root', path: '/data' }]; |
| 30 | listFixture = { |
| 31 | '/data': [ |
| 32 | { |
| 33 | name: 'readme.txt', |
| 34 | path: '/data/readme.txt', |
| 35 | isDir: false, |
| 36 | size: 12, |
| 37 | mtime: Date.now(), |
| 38 | }, |
| 39 | ], |
| 40 | }; |
| 41 | spaceFixture = { |
| 42 | '/data': { usedBytes: 0, freeBytes: 1024 * 1024 }, |
| 43 | }; |
| 44 | meFixture = { username: 'tester', isAdmin: false, permissions: {} }; |
| 45 | thumbnailStatusFixture = {}; |
| 46 | rootAccessFixture = {}; |
| 47 | folderIconFixture = {}; |
| 48 | adminUsersFixture = { users: [] }; |
| 49 | adminConfigFixture = { port: 3000, paths: ['/data'], effectivePaths: ['/data'] }; |
| 50 | downloadFixture = {}; |
| 51 | }); |
| 52 | |
| 53 | beforeAll(async () => |
| 54 | { |
| 55 | ({ JSDOM, VirtualConsole } = require('jsdom')); |
| 56 | |
| 57 | const app = express(); |
| 58 | const publicDir = path.join(__dirname, '../public'); |
| 59 | |
| 60 | app.use(express.json()); |
| 61 | app.use(express.static(publicDir)); |
| 62 | |
| 63 | app.get('/api/auth/server-key', (req, res) => res.json({ publicKey: null })); |
| 64 | app.get('/api/auth/setup-needed', (req, res) => res.json({ needed: false })); |
| 65 | app.get('/api/auth/me', (req, res) => res.json(meFixture)); |
| 66 | app.get('/api/files/roots', (req, res) => res.json({ roots: rootsFixture })); |
| 67 | app.get('/api/files/list', (req, res) => |
| 68 | { |
| 69 | const dir = req.query.dir; |
| 70 | res.json({ items: listFixture[dir] || [] }); |
| 71 | }); |
| 72 | app.get('/api/files/space', (req, res) => |
| 73 | { |
| 74 | const dir = req.query.dir; |
| 75 | const stats = spaceFixture[dir] || { usedBytes: 0, freeBytes: null }; |
| 76 | res.json(stats); |
| 77 | }); |
| 78 | app.get('/api/files/thumbnail', (req, res) => |
| 79 | { |
| 80 | const file = String(req.query.file || ''); |
| 81 | const status = Number(thumbnailStatusFixture[file] || 200); |
| 82 | if (status === 404) |
| 83 | { |
| 84 | return res.status(404).json({ error: 'Thumbnail not ready.' }); |
| 85 | } |
| 86 | res.type('image/webp').send('thumb'); |
| 87 | }); |
| 88 | app.get('/api/files/download', (req, res) => |
| 89 | { |
| 90 | const file = String(req.query.file || ''); |
| 91 | const payload = downloadFixture[file]; |
| 92 | if (!payload) |
| 93 | { |
| 94 | return res.status(404).json({ error: 'File not found.' }); |
| 95 | } |
| 96 | res.type(payload.type || 'application/octet-stream').send(payload.body || 'x'); |
| 97 | }); |
| 98 | app.get('/api/files/root-access', (req, res) => |
| 99 | { |
| 100 | const rootPath = String(req.query.rootPath || ''); |
| 101 | res.json({ users: rootAccessFixture[rootPath] || [] }); |
| 102 | }); |
| 103 | app.get('/api/files/folder-icon', (req, res) => |
| 104 | { |
| 105 | const dir = String(req.query.dir || ''); |
| 106 | const icon = folderIconFixture[dir] || null; |
| 107 | res.json({ icon }); |
| 108 | }); |
| 109 | app.get('/api/files/folder-images', (req, res) => |
| 110 | { |
| 111 | res.json({ images: [] }); |
| 112 | }); |
| 113 | app.get('/api/admin/config', (req, res) => res.json(adminConfigFixture)); |
| 114 | app.get('/api/admin/users', (req, res) => res.json(adminUsersFixture)); |
| 115 | app.post('/api/auth/logout', (req, res) => res.json({ ok: true })); |
| 116 | app.get('*', (req, res) => res.sendFile(path.join(publicDir, 'index.html'))); |
| 117 | |
| 118 | server = http.createServer(app); |
| 119 | server.listen(0); |
| 120 | await once(server, 'listening'); |
| 121 | baseUrl = `http://127.0.0.1:${server.address().port}`; |
| 122 | }); |
| 123 | |
| 124 | afterAll(() => server.close()); |
| 125 | |
| 126 | afterEach(() => |
| 127 | { |
| 128 | while (openDoms.length) |
| 129 | { |
| 130 | const dom = openDoms.pop(); |
| 131 | try { dom.window.close(); } catch { /* ignore */ } |
| 132 | } |
| 133 | }); |
| 134 | |
| 135 | async function bootClient ({ mobile = false, savedSession } = {}) |
| 136 | { |
| 137 | const virtualConsole = new VirtualConsole(); |
| 138 | const runtimeErrors = []; |
| 139 | |
| 140 | virtualConsole.on('jsdomError', (err) => |
| 141 | { |
| 142 | runtimeErrors.push(err); |
| 143 | }); |
| 144 | |
| 145 | const dom = await JSDOM.fromURL(baseUrl + '/', { |
| 146 | resources: 'usable', |
| 147 | runScripts: 'dangerously', |
| 148 | pretendToBeVisual: true, |
| 149 | virtualConsole, |
| 150 | beforeParse (window) |
| 151 | { |
| 152 | window.crypto = webcrypto; |
| 153 | window.fetch = (input, init) => |
| 154 | { |
| 155 | const url = typeof input === 'string' ? input : input.url; |
| 156 | return fetch(new URL(url, baseUrl).toString(), init); |
| 157 | }; |
| 158 | window.HTMLElement.prototype.focus = function focus () { }; |
| 159 | window.HTMLCanvasElement.prototype.getContext = () => null; |
| 160 | window.matchMedia = () => ({ matches: mobile, addListener () { }, removeListener () { }, addEventListener () { }, removeEventListener () { } }); |
| 161 | window.scrollTo = () => { }; |
| 162 | let blobCounter = 0; |
| 163 | window.URL.createObjectURL = () => `blob:mock-${blobCounter++}`; |
| 164 | window.URL.revokeObjectURL = () => { }; |
| 165 | |
| 166 | const defaultSaved = { |
| 167 | token: 'saved-token', |
| 168 | dir: '/data', |
| 169 | rootPath: '/data', |
| 170 | lastOperationAt: Date.now(), |
| 171 | }; |
| 172 | const effectiveSaved = savedSession === undefined ? defaultSaved : savedSession; |
| 173 | if (effectiveSaved) |
| 174 | { |
| 175 | window.localStorage.setItem('filemanager.session.v1', JSON.stringify(effectiveSaved)); |
| 176 | } |
| 177 | window.addEventListener('error', (event) => runtimeErrors.push(event.error || new Error(event.message))); |
| 178 | window.addEventListener('unhandledrejection', (event) => runtimeErrors.push(event.reason || new Error('Unhandled rejection'))); |
| 179 | }, |
| 180 | }); |
| 181 | |
| 182 | await new Promise((resolve) => dom.window.addEventListener('load', resolve, { once: true })); |
| 183 | await new Promise(resolve => dom.window.setTimeout(resolve, 100)); |
| 184 | |
| 185 | openDoms.push(dom); |
| 186 | |
| 187 | return { dom, runtimeErrors }; |
| 188 | } |
| 189 | |
| 190 | test('requests / and boots the client without runtime errors', async () => |
| 191 | { |
| 192 | const { dom, runtimeErrors } = await bootClient(); |
| 193 | |
| 194 | expect(runtimeErrors).toEqual([]); |
| 195 | expect(dom.window.document.getElementById('app').classList.contains('hidden')).toBe(false); |
| 196 | expect(dom.window.document.getElementById('sidebar').classList.contains('hidden')).toBe(false); |
| 197 | |
| 198 | dom.window.close(); |
| 199 | }); |
| 200 | |
| 201 | test('keeps sidebar folded on initial mobile boot', async () => |
| 202 | { |
| 203 | const { dom, runtimeErrors } = await bootClient({ mobile: true }); |
| 204 | |
| 205 | expect(runtimeErrors).toEqual([]); |
| 206 | expect(dom.window.document.getElementById('app').classList.contains('hidden')).toBe(false); |
| 207 | expect(dom.window.document.getElementById('sidebar').classList.contains('hidden')).toBe(true); |
| 208 | |
| 209 | dom.window.close(); |
| 210 | }); |
| 211 | |
| 212 | test('suppresses default context menu on mobile file long-press', async () => |
| 213 | { |
| 214 | const { dom, runtimeErrors } = await bootClient({ mobile: true }); |
| 215 | |
| 216 | expect(runtimeErrors).toEqual([]); |
| 217 | |
| 218 | const tile = dom.window.document.querySelector('.file-item'); |
| 219 | expect(tile).toBeTruthy(); |
| 220 | |
| 221 | const evt = new dom.window.MouseEvent('contextmenu', { |
| 222 | bubbles: true, |
| 223 | cancelable: true, |
| 224 | view: dom.window, |
| 225 | }); |
| 226 | |
| 227 | const dispatchResult = tile.dispatchEvent(evt); |
| 228 | expect(dispatchResult).toBe(false); |
| 229 | expect(evt.defaultPrevented).toBe(true); |
| 230 | |
| 231 | dom.window.close(); |
| 232 | }); |
| 233 | |
| 234 | test('in Home, root tiles single-click focus and double-click open', async () => |
| 235 | { |
| 236 | rootsFixture = [ |
| 237 | { label: 'Root A', path: '/data-a' }, |
| 238 | { label: 'Root B', path: '/data-b' }, |
| 239 | ]; |
| 240 | listFixture = { |
| 241 | '/data-a': [ |
| 242 | { |
| 243 | name: 'docs', |
| 244 | path: '/data-a/docs', |
| 245 | isDir: true, |
| 246 | size: 0, |
| 247 | mtime: Date.now(), |
| 248 | }, |
| 249 | ], |
| 250 | }; |
| 251 | spaceFixture = { |
| 252 | '/data-a': { usedBytes: 2048, freeBytes: 4096 }, |
| 253 | '/data-b': { usedBytes: 1024, freeBytes: 8192 }, |
| 254 | }; |
| 255 | |
| 256 | const { dom, runtimeErrors } = await bootClient({ |
| 257 | savedSession: { |
| 258 | token: 'saved-token', |
| 259 | dir: null, |
| 260 | rootPath: null, |
| 261 | lastOperationAt: Date.now(), |
| 262 | }, |
| 263 | }); |
| 264 | expect(runtimeErrors).toEqual([]); |
| 265 | |
| 266 | const rootTile = dom.window.document.querySelector('.file-item[data-path="/data-a"]'); |
| 267 | expect(rootTile).toBeTruthy(); |
| 268 | |
| 269 | rootTile.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true })); |
| 270 | await new Promise(resolve => dom.window.setTimeout(resolve, 40)); |
| 271 | |
| 272 | expect(rootTile.classList.contains('selected')).toBe(true); |
| 273 | expect(dom.window.document.querySelector('.file-item[data-path="/data-b"]')).toBeTruthy(); |
| 274 | expect(dom.window.document.getElementById('sidebar-meta').textContent).toContain('Used'); |
| 275 | expect(dom.window.document.getElementById('sidebar-meta').textContent).toContain('Remaining'); |
| 276 | |
| 277 | rootTile.dispatchEvent(new dom.window.MouseEvent('dblclick', { bubbles: true })); |
| 278 | await new Promise(resolve => dom.window.setTimeout(resolve, 80)); |
| 279 | |
| 280 | expect(dom.window.document.querySelector('.file-item[data-path="/data-a/docs"]')).toBeTruthy(); |
| 281 | |
| 282 | dom.window.close(); |
| 283 | }); |
| 284 | |
| 285 | test('root sidebar shows alias and original name, and excludes Move to action', async () => |
| 286 | { |
| 287 | rootsFixture = [ |
| 288 | { label: 'Shared', path: '/data-a', originalName: 'data-a', alias: 'Shared' }, |
| 289 | { label: 'Root B', path: '/data-b', originalName: 'data-b', alias: null }, |
| 290 | ]; |
| 291 | meFixture = { |
| 292 | username: 'tester', |
| 293 | isAdmin: false, |
| 294 | permissions: { |
| 295 | 'data-a': { read: true, write: true }, |
| 296 | 'data-b': { read: true, write: false }, |
| 297 | }, |
| 298 | }; |
| 299 | spaceFixture = { |
| 300 | '/data-a': { usedBytes: 1024, freeBytes: 2048 }, |
| 301 | '/data-b': { usedBytes: 2048, freeBytes: 1024 }, |
| 302 | }; |
| 303 | rootAccessFixture = { |
| 304 | '/data-a': [ |
| 305 | { username: 'alice', mode: 'R' }, |
| 306 | { username: 'bob', mode: 'R+W' }, |
| 307 | ], |
| 308 | }; |
| 309 | |
| 310 | const { dom, runtimeErrors } = await bootClient({ |
| 311 | savedSession: { |
| 312 | token: 'saved-token', |
| 313 | dir: null, |
| 314 | rootPath: null, |
| 315 | lastOperationAt: Date.now(), |
| 316 | }, |
| 317 | }); |
| 318 | |
| 319 | expect(runtimeErrors).toEqual([]); |
| 320 | |
| 321 | const rootTile = dom.window.document.querySelector('.file-item[data-path="/data-a"]'); |
| 322 | expect(rootTile).toBeTruthy(); |
| 323 | rootTile.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true })); |
| 324 | await new Promise(resolve => dom.window.setTimeout(resolve, 40)); |
| 325 | |
| 326 | const sidebarText = dom.window.document.getElementById('sidebar-meta').textContent; |
| 327 | expect(sidebarText).toContain('Alias'); |
| 328 | expect(sidebarText).toContain('Shared'); |
| 329 | expect(sidebarText).toContain('Original name'); |
| 330 | expect(sidebarText).toContain('data-a'); |
| 331 | expect(sidebarText).toContain('Access'); |
| 332 | expect(sidebarText).toContain('alice R'); |
| 333 | expect(sidebarText).toContain('bob R+W'); |
| 334 | |
| 335 | const accessItems = Array.from(dom.window.document.querySelectorAll('#sidebar-meta .root-access-list li')).map(li => li.textContent.trim()); |
| 336 | expect(accessItems).toEqual(['bob R+W', 'alice R']); |
| 337 | |
| 338 | const actionButtons = Array.from(dom.window.document.querySelectorAll('#sidebar-actions button')).map(b => b.textContent); |
| 339 | expect(actionButtons).toContain('Rename'); |
| 340 | expect(actionButtons).not.toContain('Move to'); |
| 341 | expect(actionButtons).not.toContain('Delete'); |
| 342 | |
| 343 | dom.window.close(); |
| 344 | }); |
| 345 | |
| 346 | test('base-level root sidebar shows access list with no item selected', async () => |
| 347 | { |
| 348 | rootAccessFixture = { |
| 349 | '/data': [ |
| 350 | { username: 'tim', mode: 'R+W' }, |
| 351 | { username: 'guest', mode: 'R' }, |
| 352 | ], |
| 353 | }; |
| 354 | |
| 355 | const { dom, runtimeErrors } = await bootClient(); |
| 356 | expect(runtimeErrors).toEqual([]); |
| 357 | |
| 358 | await new Promise(resolve => dom.window.setTimeout(resolve, 80)); |
| 359 | const sidebarText = dom.window.document.getElementById('sidebar-meta').textContent; |
| 360 | expect(sidebarText).toContain('Access'); |
| 361 | expect(sidebarText).toContain('tim R+W'); |
| 362 | expect(sidebarText).toContain('guest R'); |
| 363 | |
| 364 | const accessItems = Array.from(dom.window.document.querySelectorAll('#sidebar-meta .root-access-list li')).map(li => li.textContent.trim()); |
| 365 | expect(accessItems).toEqual(['tim R+W', 'guest R']); |
| 366 | |
| 367 | dom.window.close(); |
| 368 | }); |
| 369 | |
| 370 | test('shows pending icon spinner when image thumbnail cache is not ready', async () => |
| 371 | { |
| 372 | listFixture = { |
| 373 | '/data': [ |
| 374 | { |
| 375 | name: 'photo.png', |
| 376 | path: '/data/photo.png', |
| 377 | isDir: false, |
| 378 | size: 1024, |
| 379 | mtime: Date.now(), |
| 380 | }, |
| 381 | ], |
| 382 | }; |
| 383 | thumbnailStatusFixture['/data/photo.png'] = 404; |
| 384 | |
| 385 | const { dom, runtimeErrors } = await bootClient(); |
| 386 | expect(runtimeErrors).toEqual([]); |
| 387 | |
| 388 | await new Promise(resolve => dom.window.setTimeout(resolve, 80)); |
| 389 | const icon = dom.window.document.querySelector('.icon[data-preview-path="/data/photo.png"]'); |
| 390 | expect(icon).toBeTruthy(); |
| 391 | expect(icon.classList.contains('thumb-pending')).toBe(true); |
| 392 | |
| 393 | dom.window.close(); |
| 394 | }); |
| 395 | |
| 396 | test('admin user modal locks write checkbox while read is unchecked', async () => |
| 397 | { |
| 398 | meFixture = { username: 'admin', isAdmin: true, permissions: {} }; |
| 399 | adminConfigFixture = { port: 3000, paths: ['/data'], effectivePaths: ['/data'] }; |
| 400 | adminUsersFixture = { |
| 401 | users: [ |
| 402 | { |
| 403 | id: 9, |
| 404 | username: 'guest', |
| 405 | isAdmin: false, |
| 406 | permissions: { data: { read: false, write: true } }, |
| 407 | }, |
| 408 | ], |
| 409 | }; |
| 410 | |
| 411 | const { dom, runtimeErrors } = await bootClient(); |
| 412 | expect(runtimeErrors).toEqual([]); |
| 413 | |
| 414 | dom.window.document.getElementById('btn-admin').dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true })); |
| 415 | await new Promise(resolve => dom.window.setTimeout(resolve, 80)); |
| 416 | |
| 417 | const editBtn = dom.window.document.querySelector('#admin-users-list [data-action="edit"]'); |
| 418 | expect(editBtn).toBeTruthy(); |
| 419 | editBtn.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true })); |
| 420 | await new Promise(resolve => dom.window.setTimeout(resolve, 40)); |
| 421 | |
| 422 | const readCb = dom.window.document.querySelector('input[data-label="data"][data-type="read"]'); |
| 423 | const writeCb = dom.window.document.querySelector('input[data-label="data"][data-type="write"]'); |
| 424 | expect(readCb).toBeTruthy(); |
| 425 | expect(writeCb).toBeTruthy(); |
| 426 | expect(readCb.checked).toBe(false); |
| 427 | expect(writeCb.checked).toBe(false); |
| 428 | expect(writeCb.disabled).toBe(true); |
| 429 | |
| 430 | readCb.checked = true; |
| 431 | readCb.dispatchEvent(new dom.window.Event('change', { bubbles: true })); |
| 432 | expect(writeCb.disabled).toBe(false); |
| 433 | |
| 434 | writeCb.checked = true; |
| 435 | readCb.checked = false; |
| 436 | readCb.dispatchEvent(new dom.window.Event('change', { bubbles: true })); |
| 437 | expect(writeCb.checked).toBe(false); |
| 438 | expect(writeCb.disabled).toBe(true); |
| 439 | |
| 440 | dom.window.close(); |
| 441 | }); |
| 442 | |
| 443 | test('uses direct svg thumbnail when svg file is under 1MB', async () => |
| 444 | { |
| 445 | listFixture = { |
| 446 | '/data': [ |
| 447 | { |
| 448 | name: 'icon.svg', |
| 449 | path: '/data/icon.svg', |
| 450 | isDir: false, |
| 451 | size: 400 * 1024, |
| 452 | mtime: Date.now(), |
| 453 | }, |
| 454 | ], |
| 455 | }; |
| 456 | thumbnailStatusFixture['/data/icon.svg'] = 404; |
| 457 | downloadFixture['/data/icon.svg'] = { |
| 458 | type: 'image/svg+xml', |
| 459 | body: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><rect width="10" height="10" fill="red"/></svg>', |
| 460 | }; |
| 461 | |
| 462 | const { dom, runtimeErrors } = await bootClient(); |
| 463 | expect(runtimeErrors).toEqual([]); |
| 464 | |
| 465 | await new Promise(resolve => dom.window.setTimeout(resolve, 80)); |
| 466 | const icon = dom.window.document.querySelector('.icon[data-preview-path="/data/icon.svg"]'); |
| 467 | expect(icon).toBeTruthy(); |
| 468 | expect(icon.classList.contains('thumb-pending')).toBe(false); |
| 469 | expect(icon.dataset.thumbReady).toBe('1'); |
| 470 | |
| 471 | dom.window.close(); |
| 472 | }); |
| 473 | }); |