loopback.test.ts (8408B)
1 import { test } from 'node:test'; 2 import assert from 'node:assert/strict'; 3 import { once } from 'node:events'; 4 import { request } from 'node:http'; 5 import { readFile } from 'node:fs/promises'; 6 import WebSocket from 'ws'; 7 import { createRelayHarness } from './harness/relay.ts'; 8 import { createStaticHarness } from './harness/static.ts'; 9 10 await test('real loopback REQ/CLOSE and active teardown release owned resources', async () => { 11 const relay = await createRelayHarness(); 12 const socket = new WebSocket(relay.url); 13 try { 14 await once(socket, 'open'); 15 let reply = once(socket, 'message'); 16 socket.send(JSON.stringify(['REQ', 'first', {}])); 17 assert.deepEqual(JSON.parse(String((await reply)[0])), ['EOSE', 'first']); 18 assert.equal(relay.state().subscriptions, 1); 19 socket.send(JSON.stringify(['CLOSE', 'first'])); 20 reply = once(socket, 'message'); 21 socket.send(JSON.stringify(['REQ', 'second', {}])); 22 await reply; // Ordered frames prove CLOSE removed first before REQ second. 23 assert.equal(relay.state().subscriptions, 1); 24 } finally { 25 const closed = once(socket, 'close'); 26 await relay.close(); 27 await closed; 28 } 29 assert.deepEqual(relay.state(), { 30 listening: false, 31 connections: 0, 32 subscriptions: 0, 33 childProcesses: 0 34 }); 35 assert.equal(socket.readyState, WebSocket.CLOSED); 36 }); 37 38 await test('navigation fallback serves actual separate output and preserves missing assets', async () => { 39 const fallback = await readFile( 40 new URL('../../build/200.html', import.meta.url), 41 'utf8' 42 ); 43 const search = await readFile( 44 new URL('../../build/search.html', import.meta.url), 45 'utf8' 46 ); 47 const prerendered = new Map([['/search', search]]); 48 for (const pathname of [ 49 '/sell', 50 '/selling', 51 '/messages', 52 '/about', 53 '/privacy' 54 ]) { 55 prerendered.set( 56 pathname, 57 await readFile( 58 new URL('../../build' + pathname + '.html', import.meta.url), 59 'utf8' 60 ) 61 ); 62 } 63 for (const pathname of ['/sell', '/selling', '/messages']) { 64 const html = prerendered.get(pathname); 65 assert.ok(html); 66 assert.match( 67 html, 68 pathname === '/sell' 69 ? /<title>New listing · HarvestCircle<\/title>/ 70 : /<title>HarvestCircle<\/title>/ 71 ); 72 assert.match(html, /name="robots" content="noindex"/); 73 if (pathname === '/sell') { 74 assert.match(html, /Connect to continue/); 75 assert.match(html, /<button[^>]*disabled[^>]*>Connect extension/); 76 assert.doesNotMatch(html, /Inbox setup|Check current preference/); 77 } else if (pathname === '/selling') { 78 assert.match(html, /Connect to continue/); 79 assert.match(html, /<button[^>]*disabled[^>]*>Connect extension/); 80 assert.match(html, /href="\/sell"/); 81 assert.match(html, /List food/); 82 assert.match(html, /Inbox settings/); 83 assert.match(html, /Inbox setup is unavailable/); 84 assert.doesNotMatch( 85 html, 86 /No listings yet\.|<h2>Needs attention|<h2>Drafts|<h2>Listings/ 87 ); 88 } else { 89 assert.match(html, /Messaging is disabled in this build\./); 90 assert.match(html, /Connect to continue/); 91 assert.match(html, /<button[^>]*disabled[^>]*>Connect extension/); 92 assert.match( 93 html, 94 /Messages stay hidden until you connect and explicitly unlock your inbox\./ 95 ); 96 } 97 assert.doesNotMatch(html, /<form|<input|<textarea/); 98 } 99 assert.notEqual(search, fallback); 100 const about = prerendered.get('/about'); 101 assert.ok(about); 102 assert.match(about, /About HarvestCircle/); 103 assert.match(about, /Search without connecting\./); 104 assert.match(about, /Support has not been configured\./); 105 assert.match(about, /id="report"/); 106 assert.doesNotMatch( 107 about, 108 /Contact support|Selected report preview|Share message text|mailto:|[0-9a-f]{64}/ 109 ); 110 assert.match(search, /<title>Search food — HarvestCircle<\/title>/); 111 assert.match(search, /Search sources are unavailable\./); 112 const privacy = prerendered.get('/privacy'); 113 assert.ok(privacy); 114 assert.match(privacy, /Hiding a publisher is local/); 115 assert.match(privacy, /Blocking a person is local/); 116 assert.match( 117 privacy, 118 /Private messages have no forward secrecy or post-compromise security\./ 119 ); 120 assert.match( 121 privacy, 122 /does not delete listings, remote copies or your publication receipts/ 123 ); 124 assert.match( 125 privacy, 126 /Guest browsing\s+does not expose an account inventory/ 127 ); 128 assert.doesNotMatch(privacy, /Undo hide|Undo block|[0-9a-f]{64}/); 129 const document = { accept: 'text/html', 'sec-fetch-dest': 'document' }; 130 const server = await createStaticHarness(); 131 try { 132 for (const pathname of [ 133 '/search?q=food', 134 '/sell', 135 '/selling', 136 '/messages', 137 '/about', 138 '/privacy', 139 '/products/unsupported-coordinate', 140 '/products/unsupported-coordinate/edit', 141 '/selling/drafts/unknown-draft', 142 '/messages/unknown-conversation' 143 ]) { 144 const result = await fetch(server.url + pathname, { headers: document }); 145 assert.equal(result.status, 200, pathname); 146 assert.equal(result.headers.get('content-type'), 'text/html'); 147 assert.equal( 148 await result.text(), 149 prerendered.get(new URL(pathname, server.url).pathname) ?? fallback 150 ); 151 } 152 for (const pathname of [ 153 '/missing.js', 154 '/missing.css', 155 '/missing.json', 156 '/missing.png', 157 '/_app/missing', 158 '/assets/missing', 159 '/products/missing.js', 160 '/unowned', 161 '/products/', 162 '/products/a/extra', 163 '/products/a.b' 164 ]) { 165 const result = await fetch(server.url + pathname, { headers: document }); 166 assert.equal(result.status, 404, pathname); 167 assert.doesNotMatch(result.headers.get('content-type') ?? '', /html/); 168 assert.equal(await result.text(), ''); 169 } 170 const rejectedHeaders: Record<string, string>[] = [ 171 { accept: '*/*' }, 172 { accept: 'application/json' }, 173 { accept: 'text/html;q=0' }, 174 { accept: 'text/html', 'sec-fetch-dest': 'script' } 175 ]; 176 for (const headers of rejectedHeaders) { 177 assert.equal( 178 (await fetch(server.url + '/products/a', { headers })).status, 179 404 180 ); 181 } 182 assert.equal( 183 ( 184 await fetch(server.url + '/products/a', { 185 method: 'POST', 186 headers: document 187 }) 188 ).status, 189 405 190 ); 191 // node:http retains raw paths that URL/fetch would normalize before sending. 192 for (const pathname of [ 193 '/../index.html', 194 '/products/../index.html', 195 '/%2e%2e/index.html', 196 '/products%2fa', 197 '/products/%252e%252e', 198 '/products/%5cfoo', 199 '/products/%00', 200 '/products/%zz', 201 '/products/' + 'a'.repeat(2048) 202 ]) { 203 const status = await new Promise<number | undefined>( 204 (resolve, reject) => { 205 const req = request( 206 server.url, 207 { path: pathname, headers: document }, 208 (response) => { 209 response.resume(); 210 response.on('end', () => resolve(response.statusCode)); 211 } 212 ); 213 req.on('error', reject); 214 req.end(); 215 } 216 ); 217 assert.equal(status, 404, pathname); 218 } 219 const metadata = await fetch(server.url + '/build-info.json', { 220 headers: document 221 }); 222 assert.equal(metadata.status, 200); 223 assert.equal(metadata.headers.get('content-type'), 'application/json'); 224 assert.deepEqual( 225 await metadata.json(), 226 JSON.parse( 227 await readFile( 228 new URL('../../build/build-info.json', import.meta.url), 229 'utf8' 230 ) 231 ) 232 ); 233 } finally { 234 await server.close(); 235 } 236 assert.deepEqual(server.state(), { listening: false, childProcesses: 0 }); 237 }); 238 239 await test('loopback static fixture serves actual output and closes its listener', async () => { 240 const server = await createStaticHarness(); 241 try { 242 const result = await fetch(server.url); 243 assert.equal(result.status, 200); 244 assert.match( 245 await result.text(), 246 /<a[^>]*href="\/"[^>]*>HarvestCircle<\/a>/ 247 ); 248 assert.equal((await fetch(`${server.url}/missing.js`)).status, 404); 249 } finally { 250 await server.close(); 251 } 252 assert.deepEqual(server.state(), { listening: false, childProcesses: 0 }); 253 await assert.rejects(fetch(server.url)); 254 });