Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

5.8 KB
Raw Blame
import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { connect as connectHttp2, constants as http2Constants } from 'node:http2';
import { get, request } from 'node:https';
import { createServer, resolveConfig } from 'vite';
import { expect, it } from 'vitest';
import { devHttps } from './dev-https';

const ca = readFileSync(
  new URL('../../../infra/local/certs/ca.pem', import.meta.url)
);

it('serves HTTPS and HMR with the existing CA and detected hostname', async () => {
  const hostname = execFileSync('hostname', { encoding: 'utf8' })
    .trim()
    .toLowerCase();
  const vite = await createServer({
    configFile: false,
    appType: 'custom',
    logLevel: 'silent',
    plugins: [devHttps({})],
    optimizeDeps: { noDiscovery: true, include: [] },
    server: { host: '127.0.0.1', port: 0 },
  });
  try {
    vite.middlewares.use((_req, res) => res.end('frontend'));
    await vite.listen();
    const address = vite.httpServer?.address();
    if (!address || typeof address === 'string')
      throw new Error('Vite has no TCP listener');
    const options = { hostname: '127.0.0.1', port: address.port, ca };
    const fetchPage = (servername: string, trusted = true) =>
      new Promise<string>((resolve, reject) => {
        get(
          {
            ...options,
            servername,
            ca: trusted ? ca : [],
            path: '/app/',
          },
          (res) => {
            let body = '';
            res.setEncoding('utf8');
            res.on('data', (chunk) => {
              body += chunk;
            });
            res.on('end', () => resolve(body));
            res.on('error', reject);
          }
        ).on('error', reject);
      });
    expect(await fetchPage(hostname)).toBe('frontend');
    expect(await fetchPage('localhost')).toBe('frontend');
    await expect(fetchPage('unrelated.example')).rejects.toMatchObject({
      code: 'ERR_TLS_CERT_ALTNAME_INVALID',
    });
    await expect(fetchPage(hostname, false)).rejects.toThrow();

    // A real TLS WebSocket upgrade with Vite's browser origin/token checks,
    // resolving to the negotiated protocol or the rejection status.
    const upgradeHmr = (host: string) =>
      new Promise<string | undefined>((resolve, reject) => {
        const req = request({
          ...options,
          servername: hostname,
          path: `/?token=${vite.config.webSocketToken}`,
          headers: {
            host,
            origin: `https://${host}`,
            connection: 'Upgrade',
            upgrade: 'websocket',
            'sec-websocket-version': '13',
            'sec-websocket-key': 'dGhlIHNhbXBsZSBub25jZQ==',
            'sec-websocket-protocol': 'vite-hmr',
          },
        });
        req.setTimeout(3000, () =>
          req.destroy(new Error('HMR upgrade timeout'))
        );
        req.on('error', reject);
        req.on('response', (res) => {
          res.resume();
          resolve(`HTTP ${res.statusCode}`);
        });
        req.on('upgrade', (res, socket) => {
          socket.destroy();
          resolve(res.headers['sec-websocket-protocol']);
        });
        req.end();
      });
    for (const [host, expected] of [
      [`${hostname}:${address.port}`, 'vite-hmr'],
      // Tailscale Serve forwards the devbox's MagicDNS name as the Host.
      ['devbox.example-tailnet.ts.net', 'vite-hmr'],
      ['unrelated.example', 'HTTP 400'],
    ]) {
      expect(await upgradeHmr(host), host).toBe(expected);
    }
  } finally {
    await vite.close();
  }
});

it.each([
  { TAURI_ENV_PLATFORM: 'linux' },
  { TAURI_DEV_HOST: '192.168.1.2' },
  { MACRO_LOCAL_BACKEND_PROXY: 'https://localhost:8090' },
  { MACRO_DEV_HTTPS: 'false' },
])('preserves HTTP for %j', async (env) => {
  const config = await resolveConfig(
    { configFile: false, plugins: [devHttps(env)] },
    'serve'
  );
  expect(config.server.https).toBeUndefined();
});

it.each(['build', 'preview'] as const)(
  'does not issue certificates for %s',
  async (command) => {
    const config = await resolveConfig(
      { configFile: false, plugins: [devHttps({})] },
      command === 'preview' ? 'serve' : 'build',
      'development',
      'development',
      command === 'preview'
    );
    expect(config.server.https).toBeUndefined();
  }
);

it('keeps the HTTP/2 session when a reload cancels thousands of module requests', async () => {
  const vite = await createServer({
    configFile: false,
    appType: 'custom',
    logLevel: 'silent',
    plugins: [devHttps({})],
    optimizeDeps: { noDiscovery: true, include: [] },
    server: { host: '127.0.0.1', port: 0 },
  });
  vite.middlewares.use((_req, res) => {
    setTimeout(() => res.end('module'), 50);
  });
  try {
    await vite.listen();
    const address = vite.httpServer?.address();
    if (!address || typeof address === 'string')
      throw new Error('Vite has no TCP listener');
    const session = connectHttp2(`https://localhost:${address.port}`, {
      host: '127.0.0.1',
      servername: 'localhost',
      ca,
    });
    try {
      await new Promise((resolve) => session.once('connect', resolve));
      const abandoned = Array.from({ length: 3000 }, (_, i) =>
        session.request({ ':path': `/app/module-${i}` })
      );
      for (const stream of abandoned) stream.on('error', () => {});
      await new Promise((resolve) => setTimeout(resolve, 20));
      for (const stream of abandoned) stream.close(http2Constants.NGHTTP2_CANCEL);
      await new Promise((resolve) => setTimeout(resolve, 300));

      const status = await new Promise((resolve, reject) => {
        const stream = session.request({ ':path': '/app/' });
        stream.on('response', (headers) => resolve(headers[':status']));
        stream.on('error', reject);
        stream.resume();
      });
      expect(status).toBe(200);
    } finally {
      session.destroy();
    }
  } finally {
    await vite.close();
  }
});