| 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 | |
| 22 | beforeEach(() => |
| 23 | { |
| 24 | rootsFixture = [{ label: 'Root', path: '/data' }]; |
| 25 | listFixture = { |
| 26 | '/data': [ |
| 27 | { |
| 28 | name: 'readme.txt', |
| 29 | path: '/data/readme.txt', |
| 30 | isDir: false, |
| 31 | size: 12, |
| 32 | mtime: Date.now(), |
| 33 | }, |
| 34 | ], |
| 35 | }; |
| 36 | spaceFixture = { |
| 37 | '/data': { usedBytes: 0, freeBytes: 1024 * 1024 }, |
| 38 | }; |
| 39 | meFixture = { username: 'tester', isAdmin: false, permissions: {} }; |
| 40 | thumbnailStatusFixture = {}; |
| 41 | rootAccessFixture = {}; |
| 42 | }); |
| 43 | |
| 44 | beforeAll(async () => |
| 45 | { |
| 46 | ({ JSDOM, VirtualConsole } = require('jsdom')); |
| 47 | |
| 48 | const app = express(); |
| 49 | const publicDir = path.join(__dirname, '../public'); |
| 50 | |
| 51 | app.use(express.json()); |
| 52 | app.use(express.static(publicDir)); |
| 53 | |
| 54 | app.get('/api/auth/server-key', (req, res) => res.json({ publicKey: null })); |
| 55 | app.get('/api/auth/setup-needed', (req, res) => res.json({ needed: false })); |
| 56 | app.get('/api/auth/me', (req, res) => res.json(meFixture)); |
| 57 | app.get('/api/files/roots', (req, res) => res.json({ roots: rootsFixture })); |
| 58 | app.get('/api/files/list', (req, res) => |
| 59 | { |
| 60 | const dir = req.query.dir; |
| 61 | res.json({ items: listFixture[dir] || [] }); |
| 62 | }); |
| 63 | app.get('/api/files/space', (req, res) => |
| 64 | { |
| 65 | const dir = req.query.dir; |
| 66 | const stats = spaceFixture[dir] || { usedBytes: 0, freeBytes: null }; |
| 67 | res.json(stats); |
| 68 | }); |
| 69 | app.get('/api/files/thumbnail', (req, res) => |
| 70 | { |
| 71 | const file = String(req.query.file || ''); |
| 72 | const status = Number(thumbnailStatusFixture[file] || 200); |
| 73 | if (status === 404) |
| 74 | { |
| 75 | return res.status(404).json({ error: 'Thumbnail not ready.' }); |
| 76 | } |
| 77 | res.type('image/webp').send('thumb'); |
| 78 | }); |
| 79 | app.get('/api/files/root-access', (req, res) => |
| 80 | { |
| 81 | const rootPath = String(req.query.rootPath || ''); |
| 82 | res.json({ users: rootAccessFixture[rootPath] || [] }); |
| 83 | }); |
| 84 | app.post('/api/auth/logout', (req, res) => res.json({ ok: true })); |
| 85 | app.get('*', (req, res) => res.sendFile(path.join(publicDir, 'index.html'))); |
| 86 | |
| 87 | server = http.createServer(app); |
| 88 | server.listen(0); |
| 89 | await once(server, 'listening'); |
| 90 | baseUrl = `http://127.0.0.1:${server.address().port}`; |
| 91 | }); |
| 92 | |
| 93 | afterAll(() => server.close()); |
| 94 | |
| 95 | async function bootClient ({ mobile = false, savedSession } = {}) |
| 96 | { |
| 97 | const virtualConsole = new VirtualConsole(); |
| 98 | const runtimeErrors = []; |
| 99 | |
| 100 | virtualConsole.on('jsdomError', (err) => |
| 101 | { |
| 102 | runtimeErrors.push(err); |
| 103 | }); |
| 104 | |
| 105 | const dom = await JSDOM.fromURL(baseUrl + '/', { |
| 106 | resources: 'usable', |
| 107 | runScripts: 'dangerously', |
| 108 | pretendToBeVisual: true, |
| 109 | virtualConsole, |
| 110 | beforeParse (window) |
| 111 | { |
| 112 | window.crypto = webcrypto; |
| 113 | window.fetch = (input, init) => |
| 114 | { |
| 115 | const url = typeof input === 'string' ? input : input.url; |
| 116 | return fetch(new URL(url, baseUrl).toString(), init); |
| 117 | }; |
| 118 | window.HTMLElement.prototype.focus = function focus () { }; |
| 119 | window.HTMLCanvasElement.prototype.getContext = () => null; |
| 120 | window.matchMedia = () => ({ matches: mobile, addListener () { }, removeListener () { }, addEventListener () { }, removeEventListener () { } }); |
| 121 | window.scrollTo = () => { }; |
| 122 | |
| 123 | const defaultSaved = { |
| 124 | token: 'saved-token', |
| 125 | dir: '/data', |
| 126 | rootPath: '/data', |
| 127 | lastOperationAt: Date.now(), |
| 128 | }; |
| 129 | const effectiveSaved = savedSession === undefined ? defaultSaved : savedSession; |
| 130 | if (effectiveSaved) |
| 131 | { |
| 132 | window.localStorage.setItem('filemanager.session.v1', JSON.stringify(effectiveSaved)); |
| 133 | } |
| 134 | window.addEventListener('error', (event) => runtimeErrors.push(event.error || new Error(event.message))); |
| 135 | window.addEventListener('unhandledrejection', (event) => runtimeErrors.push(event.reason || new Error('Unhandled rejection'))); |
| 136 | }, |
| 137 | }); |
| 138 | |
| 139 | await new Promise((resolve) => dom.window.addEventListener('load', resolve, { once: true })); |
| 140 | await new Promise(resolve => dom.window.setTimeout(resolve, 100)); |
| 141 | |
| 142 | return { dom, runtimeErrors }; |
| 143 | } |
| 144 | |
| 145 | test('requests / and boots the client without runtime errors', async () => |
| 146 | { |
| 147 | const { dom, runtimeErrors } = await bootClient(); |
| 148 | |
| 149 | expect(runtimeErrors).toEqual([]); |
| 150 | expect(dom.window.document.getElementById('app').classList.contains('hidden')).toBe(false); |
| 151 | expect(dom.window.document.getElementById('sidebar').classList.contains('hidden')).toBe(false); |
| 152 | |
| 153 | dom.window.close(); |
| 154 | }); |
| 155 | |
| 156 | test('keeps sidebar folded on initial mobile boot', async () => |
| 157 | { |
| 158 | const { dom, runtimeErrors } = await bootClient({ mobile: true }); |
| 159 | |
| 160 | expect(runtimeErrors).toEqual([]); |
| 161 | expect(dom.window.document.getElementById('app').classList.contains('hidden')).toBe(false); |
| 162 | expect(dom.window.document.getElementById('sidebar').classList.contains('hidden')).toBe(true); |
| 163 | |
| 164 | dom.window.close(); |
| 165 | }); |
| 166 | |
| 167 | test('suppresses default context menu on mobile file long-press', async () => |
| 168 | { |
| 169 | const { dom, runtimeErrors } = await bootClient({ mobile: true }); |
| 170 | |
| 171 | expect(runtimeErrors).toEqual([]); |
| 172 | |
| 173 | const tile = dom.window.document.querySelector('.file-item'); |
| 174 | expect(tile).toBeTruthy(); |
| 175 | |
| 176 | const evt = new dom.window.MouseEvent('contextmenu', { |
| 177 | bubbles: true, |
| 178 | cancelable: true, |
| 179 | view: dom.window, |
| 180 | }); |
| 181 | |
| 182 | const dispatchResult = tile.dispatchEvent(evt); |
| 183 | expect(dispatchResult).toBe(false); |
| 184 | expect(evt.defaultPrevented).toBe(true); |
| 185 | |
| 186 | dom.window.close(); |
| 187 | }); |
| 188 | |
| 189 | test('in Home, root tiles single-click focus and double-click open', async () => |
| 190 | { |
| 191 | rootsFixture = [ |
| 192 | { label: 'Root A', path: '/data-a' }, |
| 193 | { label: 'Root B', path: '/data-b' }, |
| 194 | ]; |
| 195 | listFixture = { |
| 196 | '/data-a': [ |
| 197 | { |
| 198 | name: 'docs', |
| 199 | path: '/data-a/docs', |
| 200 | isDir: true, |
| 201 | size: 0, |
| 202 | mtime: Date.now(), |
| 203 | }, |
| 204 | ], |
| 205 | }; |
| 206 | spaceFixture = { |
| 207 | '/data-a': { usedBytes: 2048, freeBytes: 4096 }, |
| 208 | '/data-b': { usedBytes: 1024, freeBytes: 8192 }, |
| 209 | }; |
| 210 | |
| 211 | const { dom, runtimeErrors } = await bootClient({ |
| 212 | savedSession: { |
| 213 | token: 'saved-token', |
| 214 | dir: null, |
| 215 | rootPath: null, |
| 216 | lastOperationAt: Date.now(), |
| 217 | }, |
| 218 | }); |
| 219 | expect(runtimeErrors).toEqual([]); |
| 220 | |
| 221 | const rootTile = dom.window.document.querySelector('.file-item[data-path="/data-a"]'); |
| 222 | expect(rootTile).toBeTruthy(); |
| 223 | |
| 224 | rootTile.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true })); |
| 225 | await new Promise(resolve => dom.window.setTimeout(resolve, 40)); |
| 226 | |
| 227 | expect(rootTile.classList.contains('selected')).toBe(true); |
| 228 | expect(dom.window.document.querySelector('.file-item[data-path="/data-b"]')).toBeTruthy(); |
| 229 | expect(dom.window.document.getElementById('sidebar-meta').textContent).toContain('Used'); |
| 230 | expect(dom.window.document.getElementById('sidebar-meta').textContent).toContain('Remaining'); |
| 231 | |
| 232 | rootTile.dispatchEvent(new dom.window.MouseEvent('dblclick', { bubbles: true })); |
| 233 | await new Promise(resolve => dom.window.setTimeout(resolve, 80)); |
| 234 | |
| 235 | expect(dom.window.document.querySelector('.file-item[data-path="/data-a/docs"]')).toBeTruthy(); |
| 236 | |
| 237 | dom.window.close(); |
| 238 | }); |
| 239 | |
| 240 | test('root sidebar shows alias and original name, and excludes Move to action', async () => |
| 241 | { |
| 242 | rootsFixture = [ |
| 243 | { label: 'Shared', path: '/data-a', originalName: 'data-a', alias: 'Shared' }, |
| 244 | { label: 'Root B', path: '/data-b', originalName: 'data-b', alias: null }, |
| 245 | ]; |
| 246 | meFixture = { |
| 247 | username: 'tester', |
| 248 | isAdmin: false, |
| 249 | permissions: { |
| 250 | 'data-a': { read: true, write: true }, |
| 251 | 'data-b': { read: true, write: false }, |
| 252 | }, |
| 253 | }; |
| 254 | spaceFixture = { |
| 255 | '/data-a': { usedBytes: 1024, freeBytes: 2048 }, |
| 256 | '/data-b': { usedBytes: 2048, freeBytes: 1024 }, |
| 257 | }; |
| 258 | rootAccessFixture = { |
| 259 | '/data-a': [ |
| 260 | { username: 'alice', mode: 'R' }, |
| 261 | { username: 'bob', mode: 'R+W' }, |
| 262 | ], |
| 263 | }; |
| 264 | |
| 265 | const { dom, runtimeErrors } = await bootClient({ |
| 266 | savedSession: { |
| 267 | token: 'saved-token', |
| 268 | dir: null, |
| 269 | rootPath: null, |
| 270 | lastOperationAt: Date.now(), |
| 271 | }, |
| 272 | }); |
| 273 | |
| 274 | expect(runtimeErrors).toEqual([]); |
| 275 | |
| 276 | const rootTile = dom.window.document.querySelector('.file-item[data-path="/data-a"]'); |
| 277 | expect(rootTile).toBeTruthy(); |
| 278 | rootTile.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true })); |
| 279 | await new Promise(resolve => dom.window.setTimeout(resolve, 40)); |
| 280 | |
| 281 | const sidebarText = dom.window.document.getElementById('sidebar-meta').textContent; |
| 282 | expect(sidebarText).toContain('Alias'); |
| 283 | expect(sidebarText).toContain('Shared'); |
| 284 | expect(sidebarText).toContain('Original name'); |
| 285 | expect(sidebarText).toContain('data-a'); |
| 286 | expect(sidebarText).toContain('Access'); |
| 287 | expect(sidebarText).toContain('alice R'); |
| 288 | expect(sidebarText).toContain('bob R+W'); |
| 289 | |
| 290 | const actionButtons = Array.from(dom.window.document.querySelectorAll('#sidebar-actions button')).map(b => b.textContent); |
| 291 | expect(actionButtons).toContain('Rename'); |
| 292 | expect(actionButtons).not.toContain('Move to'); |
| 293 | expect(actionButtons).not.toContain('Delete'); |
| 294 | |
| 295 | dom.window.close(); |
| 296 | }); |
| 297 | |
| 298 | test('base-level root sidebar shows access list with no item selected', async () => |
| 299 | { |
| 300 | rootAccessFixture = { |
| 301 | '/data': [ |
| 302 | { username: 'tim', mode: 'R+W' }, |
| 303 | { username: 'guest', mode: 'R' }, |
| 304 | ], |
| 305 | }; |
| 306 | |
| 307 | const { dom, runtimeErrors } = await bootClient(); |
| 308 | expect(runtimeErrors).toEqual([]); |
| 309 | |
| 310 | await new Promise(resolve => dom.window.setTimeout(resolve, 80)); |
| 311 | const sidebarText = dom.window.document.getElementById('sidebar-meta').textContent; |
| 312 | expect(sidebarText).toContain('Access'); |
| 313 | expect(sidebarText).toContain('tim R+W'); |
| 314 | expect(sidebarText).toContain('guest R'); |
| 315 | |
| 316 | dom.window.close(); |
| 317 | }); |
| 318 | |
| 319 | test('shows pending icon spinner when image thumbnail cache is not ready', async () => |
| 320 | { |
| 321 | listFixture = { |
| 322 | '/data': [ |
| 323 | { |
| 324 | name: 'photo.png', |
| 325 | path: '/data/photo.png', |
| 326 | isDir: false, |
| 327 | size: 1024, |
| 328 | mtime: Date.now(), |
| 329 | }, |
| 330 | ], |
| 331 | }; |
| 332 | thumbnailStatusFixture['/data/photo.png'] = 404; |
| 333 | |
| 334 | const { dom, runtimeErrors } = await bootClient(); |
| 335 | expect(runtimeErrors).toEqual([]); |
| 336 | |
| 337 | await new Promise(resolve => dom.window.setTimeout(resolve, 80)); |
| 338 | const icon = dom.window.document.querySelector('.icon[data-preview-path="/data/photo.png"]'); |
| 339 | expect(icon).toBeTruthy(); |
| 340 | expect(icon.classList.contains('thumb-pending')).toBe(true); |
| 341 | |
| 342 | dom.window.close(); |
| 343 | }); |
| 344 | }); |