branding.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. * Tests for the visual companion's Superpowers/Prime Radiant branding.
  3. */
  4. const { spawn } = require('child_process');
  5. const http = require('http');
  6. const fs = require('fs');
  7. const path = require('path');
  8. const assert = require('assert');
  9. const REPO_ROOT = path.join(__dirname, '../..');
  10. const SERVER_PATH = path.join(REPO_ROOT, 'skills/brainstorming/scripts/server.cjs');
  11. const PACKAGE_VERSION = JSON.parse(
  12. fs.readFileSync(path.join(REPO_ROOT, 'package.json'), 'utf-8')
  13. ).version;
  14. const TOKEN = 'testtoken-branding-0123456789abcdef';
  15. const ASSET_URL = 'https://primeradiant.com/brand/superpowers-visual-brainstorming-logo.png';
  16. function cleanup(dir) {
  17. if (fs.existsSync(dir)) {
  18. fs.rmSync(dir, { recursive: true });
  19. }
  20. }
  21. function sleep(ms) {
  22. return new Promise(resolve => setTimeout(resolve, ms));
  23. }
  24. function startServer({ port, dir, env = {}, serverPath = SERVER_PATH }) {
  25. cleanup(dir);
  26. return spawn('node', [serverPath], {
  27. env: {
  28. ...process.env,
  29. BRAINSTORM_PORT: String(port),
  30. BRAINSTORM_DIR: dir,
  31. BRAINSTORM_TOKEN: TOKEN,
  32. ...env
  33. }
  34. });
  35. }
  36. function waitForServer(server) {
  37. let stdout = '';
  38. let stderr = '';
  39. return new Promise((resolve, reject) => {
  40. const timeout = setTimeout(() => reject(new Error(`Server did not start. stderr: ${stderr}`)), 5000);
  41. server.stdout.on('data', (data) => {
  42. stdout += data.toString();
  43. if (stdout.includes('server-started')) {
  44. clearTimeout(timeout);
  45. resolve();
  46. }
  47. });
  48. server.stderr.on('data', (data) => { stderr += data.toString(); });
  49. server.on('error', reject);
  50. });
  51. }
  52. function fetchHtml(port) {
  53. return new Promise((resolve, reject) => {
  54. const headers = { Cookie: `brainstorm-key-${port}=${TOKEN}` };
  55. http.get(`http://localhost:${port}/`, { headers }, (res) => {
  56. let body = '';
  57. res.on('data', chunk => { body += chunk; });
  58. res.on('end', () => resolve(body));
  59. }).on('error', reject);
  60. });
  61. }
  62. function writeFragment(dir) {
  63. const contentDir = path.join(dir, 'content');
  64. fs.mkdirSync(contentDir, { recursive: true });
  65. fs.writeFileSync(path.join(contentDir, 'screen.html'), '<h2>Pick a layout</h2>');
  66. }
  67. function createPackagedServerFixture(version) {
  68. const root = fs.mkdtempSync(path.join('/tmp', 'superpowers-packaged-server-'));
  69. const scriptDir = path.join(root, 'skills/brainstorming/scripts');
  70. fs.cpSync(path.join(REPO_ROOT, 'skills/brainstorming/scripts'), scriptDir, { recursive: true });
  71. fs.mkdirSync(path.join(root, '.codex-plugin'), { recursive: true });
  72. fs.writeFileSync(
  73. path.join(root, '.codex-plugin/plugin.json'),
  74. JSON.stringify({ name: 'superpowers', version }, null, 2)
  75. );
  76. return {
  77. root,
  78. serverPath: path.join(scriptDir, 'server.cjs')
  79. };
  80. }
  81. async function withServer(options, fn) {
  82. const server = startServer(options);
  83. try {
  84. await waitForServer(server);
  85. await fn();
  86. } finally {
  87. if (server.exitCode === null && server.signalCode === null) {
  88. server.kill();
  89. await new Promise(resolve => server.once('exit', resolve));
  90. }
  91. await sleep(100);
  92. cleanup(options.dir);
  93. }
  94. }
  95. let passed = 0;
  96. let failed = 0;
  97. async function test(name, fn) {
  98. try {
  99. await fn();
  100. console.log(` PASS: ${name}`);
  101. passed++;
  102. } catch (e) {
  103. console.log(` FAIL: ${name}`);
  104. console.log(` ${e.message}`);
  105. failed++;
  106. }
  107. }
  108. function assertBrandedWithLogo(html, version = PACKAGE_VERSION) {
  109. assert(
  110. html.includes(`Superpowers v${version}`),
  111. 'branding text should include dynamic package version'
  112. );
  113. assert(
  114. !html.includes(`Superpowers v${version} by`),
  115. 'branding text should not include "by" when the logo is visible'
  116. );
  117. assert(
  118. /<img class="brand-logo"[^>]*>\s*<span class="brand-copy">Superpowers v/.test(html),
  119. 'visible logo should appear before the Superpowers version text'
  120. );
  121. assert(
  122. /\.brand a\s*\{[^}]*line-height:\s*1/i.test(html),
  123. 'brand row should align the logo and version text by their visual height'
  124. );
  125. assert(
  126. /\.brand a\s*\{[^}]*gap:\s*0\.5rem/i.test(html),
  127. 'brand row should keep the logo and version text close together'
  128. );
  129. assert(
  130. /\.brand a\s*\{[^}]*max-width:\s*100%/i.test(html),
  131. 'brand link should be constrained so it cannot overlap the status column'
  132. );
  133. assert(
  134. /\.brand\s*\{[^}]*line-height:\s*1/i.test(html),
  135. 'brand wrapper should not inherit the page line height'
  136. );
  137. assert(
  138. /\.brand\s*\{[^}]*overflow:\s*hidden/i.test(html),
  139. 'brand wrapper should clip before it reaches the status column'
  140. );
  141. }
  142. function assertBrandedFallbackText(html, version = PACKAGE_VERSION) {
  143. assert(
  144. html.includes(`Prime Radiant Superpowers v${version}`),
  145. 'disabled telemetry should keep plain text Prime Radiant/Superpowers branding'
  146. );
  147. }
  148. function assertTelemetryImage(html, version = PACKAGE_VERSION) {
  149. const expectedUrl = `${ASSET_URL}?v=${encodeURIComponent(version)}`;
  150. assert(html.includes(`src="${expectedUrl}"`), 'remote image should use the dedicated main-domain asset with only v=');
  151. assert(!html.includes('event='), 'remote image URL must not include event=');
  152. assert(!html.includes('surface='), 'remote image URL must not include surface=');
  153. assert(!html.includes('launch_id='), 'remote image URL must not include launch_id=');
  154. assert(!html.includes('lid='), 'remote image URL must not include lid=');
  155. }
  156. function assertLogoKeepsTransparentBackground(html) {
  157. assert(
  158. /\.brand-logo\s*\{[^}]*height:\s*1em/i.test(html),
  159. 'logo should match the surrounding brand text size'
  160. );
  161. assert(
  162. /\.brand-logo\s*\{[^}]*display:\s*block/i.test(html),
  163. 'logo should not reserve inline-image descender space'
  164. );
  165. assert(
  166. /\.brand-copy\s*\{[^}]*line-height:\s*1/i.test(html),
  167. 'version text should use the same compact line height as the logo'
  168. );
  169. assert(
  170. /\.brand-copy\s*\{[^}]*min-width:\s*0/i.test(html),
  171. 'version text should be allowed to shrink inside the brand row'
  172. );
  173. assert(
  174. /\.brand-copy\s*\{[^}]*transform:\s*translateY\(-1px\)/i.test(html),
  175. 'version text should compensate for bottom padding inside the logo asset'
  176. );
  177. assert(
  178. /\.brand-logo\s*\{[^}]*filter:\s*invert\(1\)/i.test(html),
  179. 'white logo asset should invert on light backgrounds'
  180. );
  181. assert(
  182. !/\.brand-logo\s*\{[^}]*background:/i.test(html),
  183. 'logo should keep its transparent background'
  184. );
  185. assert(
  186. !/\.brand-logo\s*\{[^}]*padding:/i.test(html),
  187. 'logo should not rely on a padded backing'
  188. );
  189. }
  190. function assertFramedLogoSupportsDarkTheme(html) {
  191. assert(
  192. /@media\s*\(prefers-color-scheme:\s*dark\)[\s\S]*\.brand-logo\s*\{[^}]*filter:\s*none/i.test(html),
  193. 'framed screens should leave the white logo unfiltered in dark mode'
  194. );
  195. }
  196. function assertFramedScreenUsesBrandHeader(html) {
  197. const logoCount = (html.match(/class="brand-logo"/g) || []).length;
  198. assert.strictEqual(logoCount, 1, 'framed screens should render the logo only in the header');
  199. assert(!html.includes('<div class="indicator-bar">'), 'framed screens should not render footer chrome');
  200. assert(
  201. /<div class="header">[\s\S]*<div class="brand">[\s\S]*<div class="status">Connecting…<\/div>/.test(html),
  202. 'header should contain branding and connection status'
  203. );
  204. assert(!html.includes('id="indicator-text"'), 'header should not render the selection indicator text');
  205. assert(!html.includes('Click an option above'), 'header should not render the selection instruction');
  206. }
  207. function assertHeaderAvoidsNarrowOverlap(html) {
  208. assert(
  209. /grid-template-columns:\s*minmax\(0,\s*1fr\)\s*auto/i.test(html),
  210. 'header should allocate shrinkable space to branding before the status column'
  211. );
  212. assert(
  213. /\.header \.status\s*\{[^}]*grid-column:\s*2/i.test(html),
  214. 'status should live in the final fixed-width grid column'
  215. );
  216. assert(
  217. /\.header \.brand\s*\{[^}]*width:\s*100%/i.test(html),
  218. 'header brand should fill its grid track so overflow clipping prevents overlap'
  219. );
  220. }
  221. async function main() {
  222. console.log('\n--- Visual Companion Branding ---');
  223. await test('framed screens render versioned Prime Radiant logo by default', async () => {
  224. const port = 3451;
  225. const dir = '/tmp/brainstorm-branding-default';
  226. await withServer({ port, dir }, async () => {
  227. writeFragment(dir);
  228. await sleep(300);
  229. const html = await fetchHtml(port);
  230. assertBrandedWithLogo(html);
  231. assertTelemetryImage(html);
  232. assertLogoKeepsTransparentBackground(html);
  233. assertFramedLogoSupportsDarkTheme(html);
  234. assertFramedScreenUsesBrandHeader(html);
  235. assertHeaderAvoidsNarrowOverlap(html);
  236. });
  237. });
  238. await test('waiting screen renders versioned Prime Radiant logo by default', async () => {
  239. const port = 3452;
  240. const dir = '/tmp/brainstorm-branding-waiting';
  241. await withServer({ port, dir }, async () => {
  242. const html = await fetchHtml(port);
  243. assert(html.includes('Waiting for the agent'), 'waiting page should still render');
  244. assertBrandedWithLogo(html);
  245. assertTelemetryImage(html);
  246. assertLogoKeepsTransparentBackground(html);
  247. });
  248. });
  249. await test('packaged Codex plugin reads version from .codex-plugin manifest', async () => {
  250. const port = 3457;
  251. const dir = '/tmp/brainstorm-branding-packaged-codex';
  252. const packagedVersion = '7.8.9';
  253. const fixture = createPackagedServerFixture(packagedVersion);
  254. try {
  255. await withServer({ port, dir, serverPath: fixture.serverPath }, async () => {
  256. writeFragment(dir);
  257. await sleep(300);
  258. const html = await fetchHtml(port);
  259. assertBrandedWithLogo(html, packagedVersion);
  260. assertTelemetryImage(html, packagedVersion);
  261. assert(!html.includes('Superpowers vunknown'), 'packaged plugin should not fall back to unknown version');
  262. });
  263. } finally {
  264. cleanup(fixture.root);
  265. }
  266. });
  267. await test('SUPERPOWERS_DISABLE_TELEMETRY=true omits remote image but keeps local branding', async () => {
  268. const port = 3453;
  269. const dir = '/tmp/brainstorm-branding-disabled';
  270. await withServer({ port, dir, env: { SUPERPOWERS_DISABLE_TELEMETRY: 'true' } }, async () => {
  271. writeFragment(dir);
  272. await sleep(300);
  273. const html = await fetchHtml(port);
  274. assertBrandedFallbackText(html);
  275. assert(!html.includes(ASSET_URL), 'disabled telemetry should omit the remote image');
  276. });
  277. });
  278. await test('SUPERPOWERS_DISABLE_TELEMETRY=yes also omits the remote image on the waiting screen', async () => {
  279. const port = 3454;
  280. const dir = '/tmp/brainstorm-branding-disabled-waiting';
  281. await withServer({ port, dir, env: { SUPERPOWERS_DISABLE_TELEMETRY: 'yes' } }, async () => {
  282. const html = await fetchHtml(port);
  283. assertBrandedFallbackText(html);
  284. assert(!html.includes(ASSET_URL), 'disabled telemetry should omit the remote image');
  285. });
  286. });
  287. await test('DISABLE_TELEMETRY=true omits remote image for Claude Code telemetry opt-out', async () => {
  288. const port = 3455;
  289. const dir = '/tmp/brainstorm-branding-claude-disable-telemetry';
  290. await withServer({ port, dir, env: { DISABLE_TELEMETRY: 'true' } }, async () => {
  291. writeFragment(dir);
  292. await sleep(300);
  293. const html = await fetchHtml(port);
  294. assertBrandedFallbackText(html);
  295. assert(!html.includes(ASSET_URL), 'Claude Code telemetry opt-out should omit the remote image');
  296. });
  297. });
  298. await test('CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 omits remote image for Claude Code traffic opt-out', async () => {
  299. const port = 3456;
  300. const dir = '/tmp/brainstorm-branding-claude-disable-nonessential';
  301. await withServer({ port, dir, env: { CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1' } }, async () => {
  302. const html = await fetchHtml(port);
  303. assertBrandedFallbackText(html);
  304. assert(!html.includes(ASSET_URL), 'Claude Code non-essential traffic opt-out should omit the remote image');
  305. });
  306. });
  307. console.log(`\n--- Results: ${passed} passed, ${failed} failed ---`);
  308. if (failed > 0) process.exitCode = 1;
  309. }
  310. main().catch((err) => {
  311. console.error('Test failed:', err);
  312. process.exit(1);
  313. });