mirror of
https://github.com/obra/superpowers.git
synced 2026-06-16 15:49:05 +08:00
168 lines
5.1 KiB
JavaScript
168 lines
5.1 KiB
JavaScript
/**
|
|
* Tests for the visual companion's Superpowers/Prime Radiant branding.
|
|
*/
|
|
|
|
const { spawn } = require('child_process');
|
|
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const assert = require('assert');
|
|
|
|
const REPO_ROOT = path.join(__dirname, '../..');
|
|
const SERVER_PATH = path.join(REPO_ROOT, 'skills/brainstorming/scripts/server.cjs');
|
|
const PACKAGE_VERSION = JSON.parse(
|
|
fs.readFileSync(path.join(REPO_ROOT, 'package.json'), 'utf-8')
|
|
).version;
|
|
const TOKEN = 'testtoken-branding-0123456789abcdef';
|
|
const ASSET_URL = 'https://primeradiant.com/brand/superpowers-visual-brainstorming-logo.png';
|
|
|
|
function cleanup(dir) {
|
|
if (fs.existsSync(dir)) {
|
|
fs.rmSync(dir, { recursive: true });
|
|
}
|
|
}
|
|
|
|
function sleep(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
function startServer({ port, dir, disableTelemetry = false }) {
|
|
cleanup(dir);
|
|
return spawn('node', [SERVER_PATH], {
|
|
env: {
|
|
...process.env,
|
|
BRAINSTORM_PORT: String(port),
|
|
BRAINSTORM_DIR: dir,
|
|
BRAINSTORM_TOKEN: TOKEN,
|
|
...(disableTelemetry ? { SUPERPOWERS_DISABLE_TELEMETRY: '1' } : {})
|
|
}
|
|
});
|
|
}
|
|
|
|
function waitForServer(server) {
|
|
let stdout = '';
|
|
let stderr = '';
|
|
|
|
return new Promise((resolve, reject) => {
|
|
server.stdout.on('data', (data) => {
|
|
stdout += data.toString();
|
|
if (stdout.includes('server-started')) resolve();
|
|
});
|
|
server.stderr.on('data', (data) => { stderr += data.toString(); });
|
|
server.on('error', reject);
|
|
setTimeout(() => reject(new Error(`Server did not start. stderr: ${stderr}`)), 5000);
|
|
});
|
|
}
|
|
|
|
function fetchHtml(port) {
|
|
return new Promise((resolve, reject) => {
|
|
const headers = { Cookie: `brainstorm-key-${port}=${TOKEN}` };
|
|
http.get(`http://localhost:${port}/`, { headers }, (res) => {
|
|
let body = '';
|
|
res.on('data', chunk => { body += chunk; });
|
|
res.on('end', () => resolve(body));
|
|
}).on('error', reject);
|
|
});
|
|
}
|
|
|
|
function writeFragment(dir) {
|
|
const contentDir = path.join(dir, 'content');
|
|
fs.mkdirSync(contentDir, { recursive: true });
|
|
fs.writeFileSync(path.join(contentDir, 'screen.html'), '<h2>Pick a layout</h2>');
|
|
}
|
|
|
|
async function withServer(options, fn) {
|
|
const server = startServer(options);
|
|
try {
|
|
await waitForServer(server);
|
|
await fn();
|
|
} finally {
|
|
server.kill();
|
|
await sleep(100);
|
|
cleanup(options.dir);
|
|
}
|
|
}
|
|
|
|
let passed = 0;
|
|
let failed = 0;
|
|
|
|
async function test(name, fn) {
|
|
try {
|
|
await fn();
|
|
console.log(` PASS: ${name}`);
|
|
passed++;
|
|
} catch (e) {
|
|
console.log(` FAIL: ${name}`);
|
|
console.log(` ${e.message}`);
|
|
failed++;
|
|
}
|
|
}
|
|
|
|
function assertBranded(html) {
|
|
assert(
|
|
html.includes(`Superpowers v${PACKAGE_VERSION} by Prime Radiant`),
|
|
'branding text should include dynamic package version'
|
|
);
|
|
}
|
|
|
|
function assertTelemetryImage(html) {
|
|
const expectedUrl = `${ASSET_URL}?v=${encodeURIComponent(PACKAGE_VERSION)}`;
|
|
assert(html.includes(`src="${expectedUrl}"`), 'remote image should use the dedicated main-domain asset with only v=');
|
|
assert(!html.includes('event='), 'remote image URL must not include event=');
|
|
assert(!html.includes('surface='), 'remote image URL must not include surface=');
|
|
assert(!html.includes('launch_id='), 'remote image URL must not include launch_id=');
|
|
assert(!html.includes('lid='), 'remote image URL must not include lid=');
|
|
}
|
|
|
|
console.log('\n--- Visual Companion Branding ---');
|
|
|
|
test('framed screens render versioned Prime Radiant logo by default', async () => {
|
|
const port = 3451;
|
|
const dir = '/tmp/brainstorm-branding-default';
|
|
await withServer({ port, dir }, async () => {
|
|
writeFragment(dir);
|
|
await sleep(300);
|
|
const html = await fetchHtml(port);
|
|
assertBranded(html);
|
|
assertTelemetryImage(html);
|
|
});
|
|
});
|
|
|
|
test('waiting screen renders versioned Prime Radiant logo by default', async () => {
|
|
const port = 3452;
|
|
const dir = '/tmp/brainstorm-branding-waiting';
|
|
await withServer({ port, dir }, async () => {
|
|
const html = await fetchHtml(port);
|
|
assert(html.includes('Waiting for the agent'), 'waiting page should still render');
|
|
assertBranded(html);
|
|
assertTelemetryImage(html);
|
|
});
|
|
});
|
|
|
|
test('SUPERPOWERS_DISABLE_TELEMETRY=1 omits remote image but keeps local branding', async () => {
|
|
const port = 3453;
|
|
const dir = '/tmp/brainstorm-branding-disabled';
|
|
await withServer({ port, dir, disableTelemetry: true }, async () => {
|
|
writeFragment(dir);
|
|
await sleep(300);
|
|
const html = await fetchHtml(port);
|
|
assertBranded(html);
|
|
assert(!html.includes(ASSET_URL), 'disabled telemetry should omit the remote image');
|
|
});
|
|
});
|
|
|
|
test('disabled telemetry also omits the remote image on the waiting screen', async () => {
|
|
const port = 3454;
|
|
const dir = '/tmp/brainstorm-branding-disabled-waiting';
|
|
await withServer({ port, dir, disableTelemetry: true }, async () => {
|
|
const html = await fetchHtml(port);
|
|
assertBranded(html);
|
|
assert(!html.includes(ASSET_URL), 'disabled telemetry should omit the remote image');
|
|
});
|
|
});
|
|
|
|
process.on('beforeExit', () => {
|
|
console.log(`\n--- Results: ${passed} passed, ${failed} failed ---`);
|
|
if (failed > 0) process.exitCode = 1;
|
|
});
|