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