104 lines
4.7 KiB
JavaScript
104 lines
4.7 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {mkdtemp, mkdir, readFile, writeFile} from 'node:fs/promises';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import WebSocket from 'ws';
|
|
import {createServer} from '../dist/index.js';
|
|
|
|
async function fixture() {
|
|
const root = await mkdtemp(path.join(os.tmpdir(), 'iceberg-dev-'));
|
|
await mkdir(path.join(root, 'src'), {recursive: true});
|
|
await mkdir(path.join(root, 'public'), {recursive: true});
|
|
await writeFile(path.join(root, 'index.html'), '<!doctype html><html><head><title>Dev</title></head><body><div id="app"></div><script type="module" src="/src/main.ts"></script></body></html>');
|
|
await writeFile(path.join(root, 'src/main.ts'), "import './style.css'; import { value } from './value.ts'; import logo from './logo.svg'; document.querySelector('#app').textContent = value + logo;");
|
|
await writeFile(path.join(root, 'src/value.ts'), 'export const value: string = "ready";');
|
|
await writeFile(path.join(root, 'src/style.css'), 'body { color: blue; }');
|
|
await writeFile(path.join(root, 'public/robots.txt'), 'User-agent: *');
|
|
await writeFile(path.join(root, 'src/logo.svg'), '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"><path d="M0 0h1v1H0z"/></svg>');
|
|
return root;
|
|
}
|
|
|
|
async function withServer(fn) {
|
|
const root = await fixture();
|
|
const previous = process.cwd();
|
|
process.chdir(root);
|
|
const server = await createServer({server: {host: '127.0.0.1', port: 0}});
|
|
try {
|
|
await server.listen();
|
|
return await fn(server, root);
|
|
} finally {
|
|
await server.close();
|
|
process.chdir(previous);
|
|
}
|
|
}
|
|
|
|
function waitForMessage(socket, predicate) {
|
|
return new Promise((resolve, reject) => {
|
|
const timeout = setTimeout(() => reject(new Error('Timed out waiting for WebSocket message')), 3000);
|
|
socket.on('message', data => {
|
|
const message = JSON.parse(data.toString());
|
|
if (predicate(message)) {
|
|
clearTimeout(timeout);
|
|
resolve(message);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
test('serves HTML, transformed TypeScript, CSS, public files, and WebSocket reload events', async () => {
|
|
await withServer(async (server, root) => {
|
|
const base = server.url;
|
|
const html = await (await fetch(base)).text();
|
|
assert.match(html, /@iceberg\/client\.js/);
|
|
const js = await (await fetch(`${base}src/main.ts`)).text();
|
|
assert.match(js, /@iceberg\/asset-module/);
|
|
assert.doesNotMatch(js, /: string/);
|
|
const assetModuleUrl = `${base}${js.match(/\/\@iceberg\/asset-module\?file=[^"']+/)[0].slice(1)}`;
|
|
assert.match(await (await fetch(assetModuleUrl)).text(), /export default/);
|
|
const css = await (await fetch(`${base}src/style.css`)).text();
|
|
assert.match(css, /color:\s*(blue|#00f)/);
|
|
assert.equal(await (await fetch(`${base}robots.txt`)).text(), 'User-agent: *');
|
|
assert.equal((await fetch(`${base}.env`)).status, 404);
|
|
const socket = new WebSocket(`${base.replace('http', 'ws')}__iceberg_ws`);
|
|
const connected = waitForMessage(socket, message => message.type === 'connected');
|
|
await new Promise((resolve, reject) => {
|
|
socket.once('open', resolve);
|
|
socket.once('error', reject);
|
|
});
|
|
await connected;
|
|
const reload = waitForMessage(socket, message => message.type === 'reload');
|
|
await writeFile(path.join(root, 'src/main.ts'), 'document.body.dataset.changed = "yes";');
|
|
await reload;
|
|
socket.close();
|
|
});
|
|
});
|
|
|
|
test('sends CSS updates without requiring a page reload', async () => {
|
|
await withServer(async (server, root) => {
|
|
const socket = new WebSocket(`${server.url.replace('http', 'ws')}__iceberg_ws`);
|
|
const connected = waitForMessage(socket, message => message.type === 'connected');
|
|
await new Promise((resolve, reject) => {
|
|
socket.once('open', resolve);
|
|
socket.once('error', reject);
|
|
});
|
|
await connected;
|
|
const updatePromise = waitForMessage(socket, message => message.type === 'css-update');
|
|
await writeFile(path.join(root, 'src/style.css'), 'body { color: red; }');
|
|
const update = await updatePromise;
|
|
assert.match(update.path, /src[\\/]style\.css/);
|
|
socket.close();
|
|
});
|
|
});
|
|
test('falls back to the next port when strictPort is disabled', async () => {
|
|
await withServer(async (server, root) => {
|
|
const second = await createServer({root, server: {host: '127.0.0.1', port: server.port}});
|
|
try {
|
|
await second.listen();
|
|
assert.notEqual(second.port, server.port);
|
|
} finally {
|
|
await second.close();
|
|
}
|
|
});
|
|
});
|