1
0

branding.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 = {} }) {
  25. cleanup(dir);
  26. return spawn('node', [SERVER_PATH], {
  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. async function withServer(options, fn) {
  68. const server = startServer(options);
  69. try {
  70. await waitForServer(server);
  71. await fn();
  72. } finally {
  73. if (server.exitCode === null && server.signalCode === null) {
  74. server.kill();
  75. await new Promise(resolve => server.once('exit', resolve));
  76. }
  77. await sleep(100);
  78. cleanup(options.dir);
  79. }
  80. }
  81. let passed = 0;
  82. let failed = 0;
  83. async function test(name, fn) {
  84. try {
  85. await fn();
  86. console.log(` PASS: ${name}`);
  87. passed++;
  88. } catch (e) {
  89. console.log(` FAIL: ${name}`);
  90. console.log(` ${e.message}`);
  91. failed++;
  92. }
  93. }
  94. function assertBrandedWithLogo(html) {
  95. assert(
  96. html.includes(`Superpowers v${PACKAGE_VERSION}`),
  97. 'branding text should include dynamic package version'
  98. );
  99. assert(
  100. !html.includes(`Superpowers v${PACKAGE_VERSION} by`),
  101. 'branding text should not include "by" when the logo is visible'
  102. );
  103. assert(
  104. /<img class="brand-logo"[^>]*>\s*<span class="brand-copy">Superpowers v/.test(html),
  105. 'visible logo should appear before the Superpowers version text'
  106. );
  107. assert(
  108. /\.brand a\s*\{[^}]*line-height:\s*1/i.test(html),
  109. 'brand row should align the logo and version text by their visual height'
  110. );
  111. assert(
  112. /\.brand a\s*\{[^}]*gap:\s*0\.5rem/i.test(html),
  113. 'brand row should keep the logo and version text close together'
  114. );
  115. assert(
  116. /\.brand a\s*\{[^}]*max-width:\s*100%/i.test(html),
  117. 'brand link should be constrained so it cannot overlap the status column'
  118. );
  119. assert(
  120. /\.brand\s*\{[^}]*line-height:\s*1/i.test(html),
  121. 'brand wrapper should not inherit the page line height'
  122. );
  123. assert(
  124. /\.brand\s*\{[^}]*overflow:\s*hidden/i.test(html),
  125. 'brand wrapper should clip before it reaches the status column'
  126. );
  127. }
  128. function assertBrandedFallbackText(html) {
  129. assert(
  130. html.includes(`Prime Radiant Superpowers v${PACKAGE_VERSION}`),
  131. 'disabled telemetry should keep plain text Prime Radiant/Superpowers branding'
  132. );
  133. }
  134. function assertTelemetryImage(html) {
  135. const expectedUrl = `${ASSET_URL}?v=${encodeURIComponent(PACKAGE_VERSION)}`;
  136. assert(html.includes(`src="${expectedUrl}"`), 'remote image should use the dedicated main-domain asset with only v=');
  137. assert(!html.includes('event='), 'remote image URL must not include event=');
  138. assert(!html.includes('surface='), 'remote image URL must not include surface=');
  139. assert(!html.includes('launch_id='), 'remote image URL must not include launch_id=');
  140. assert(!html.includes('lid='), 'remote image URL must not include lid=');
  141. }
  142. function assertLogoKeepsTransparentBackground(html) {
  143. assert(
  144. /\.brand-logo\s*\{[^}]*height:\s*1em/i.test(html),
  145. 'logo should match the surrounding brand text size'
  146. );
  147. assert(
  148. /\.brand-logo\s*\{[^}]*display:\s*block/i.test(html),
  149. 'logo should not reserve inline-image descender space'
  150. );
  151. assert(
  152. /\.brand-copy\s*\{[^}]*line-height:\s*1/i.test(html),
  153. 'version text should use the same compact line height as the logo'
  154. );
  155. assert(
  156. /\.brand-copy\s*\{[^}]*min-width:\s*0/i.test(html),
  157. 'version text should be allowed to shrink inside the brand row'
  158. );
  159. assert(
  160. /\.brand-copy\s*\{[^}]*transform:\s*translateY\(-1px\)/i.test(html),
  161. 'version text should compensate for bottom padding inside the logo asset'
  162. );
  163. assert(
  164. /\.brand-logo\s*\{[^}]*filter:\s*invert\(1\)/i.test(html),
  165. 'white logo asset should invert on light backgrounds'
  166. );
  167. assert(
  168. !/\.brand-logo\s*\{[^}]*background:/i.test(html),
  169. 'logo should keep its transparent background'
  170. );
  171. assert(
  172. !/\.brand-logo\s*\{[^}]*padding:/i.test(html),
  173. 'logo should not rely on a padded backing'
  174. );
  175. }
  176. function assertFramedLogoSupportsDarkTheme(html) {
  177. assert(
  178. /@media\s*\(prefers-color-scheme:\s*dark\)[\s\S]*\.brand-logo\s*\{[^}]*filter:\s*none/i.test(html),
  179. 'framed screens should leave the white logo unfiltered in dark mode'
  180. );
  181. }
  182. function assertFramedScreenUsesBrandHeader(html) {
  183. const logoCount = (html.match(/class="brand-logo"/g) || []).length;
  184. assert.strictEqual(logoCount, 1, 'framed screens should render the logo only in the header');
  185. assert(!html.includes('<div class="indicator-bar">'), 'framed screens should not render footer chrome');
  186. assert(
  187. /<div class="header">[\s\S]*<div class="brand">[\s\S]*<div class="status">Connecting…<\/div>/.test(html),
  188. 'header should contain branding and connection status'
  189. );
  190. assert(!html.includes('id="indicator-text"'), 'header should not render the selection indicator text');
  191. assert(!html.includes('Click an option above'), 'header should not render the selection instruction');
  192. }
  193. function assertHeaderAvoidsNarrowOverlap(html) {
  194. assert(
  195. /grid-template-columns:\s*minmax\(0,\s*1fr\)\s*auto/i.test(html),
  196. 'header should allocate shrinkable space to branding before the status column'
  197. );
  198. assert(
  199. /\.header \.status\s*\{[^}]*grid-column:\s*2/i.test(html),
  200. 'status should live in the final fixed-width grid column'
  201. );
  202. assert(
  203. /\.header \.brand\s*\{[^}]*width:\s*100%/i.test(html),
  204. 'header brand should fill its grid track so overflow clipping prevents overlap'
  205. );
  206. }
  207. async function main() {
  208. console.log('\n--- Visual Companion Branding ---');
  209. await test('framed screens render versioned Prime Radiant logo by default', async () => {
  210. const port = 3451;
  211. const dir = '/tmp/brainstorm-branding-default';
  212. await withServer({ port, dir }, async () => {
  213. writeFragment(dir);
  214. await sleep(300);
  215. const html = await fetchHtml(port);
  216. assertBrandedWithLogo(html);
  217. assertTelemetryImage(html);
  218. assertLogoKeepsTransparentBackground(html);
  219. assertFramedLogoSupportsDarkTheme(html);
  220. assertFramedScreenUsesBrandHeader(html);
  221. assertHeaderAvoidsNarrowOverlap(html);
  222. });
  223. });
  224. await test('waiting screen renders versioned Prime Radiant logo by default', async () => {
  225. const port = 3452;
  226. const dir = '/tmp/brainstorm-branding-waiting';
  227. await withServer({ port, dir }, async () => {
  228. const html = await fetchHtml(port);
  229. assert(html.includes('Waiting for the agent'), 'waiting page should still render');
  230. assertBrandedWithLogo(html);
  231. assertTelemetryImage(html);
  232. assertLogoKeepsTransparentBackground(html);
  233. });
  234. });
  235. await test('SUPERPOWERS_DISABLE_TELEMETRY=true omits remote image but keeps local branding', async () => {
  236. const port = 3453;
  237. const dir = '/tmp/brainstorm-branding-disabled';
  238. await withServer({ port, dir, env: { SUPERPOWERS_DISABLE_TELEMETRY: 'true' } }, async () => {
  239. writeFragment(dir);
  240. await sleep(300);
  241. const html = await fetchHtml(port);
  242. assertBrandedFallbackText(html);
  243. assert(!html.includes(ASSET_URL), 'disabled telemetry should omit the remote image');
  244. });
  245. });
  246. await test('SUPERPOWERS_DISABLE_TELEMETRY=yes also omits the remote image on the waiting screen', async () => {
  247. const port = 3454;
  248. const dir = '/tmp/brainstorm-branding-disabled-waiting';
  249. await withServer({ port, dir, env: { SUPERPOWERS_DISABLE_TELEMETRY: 'yes' } }, async () => {
  250. const html = await fetchHtml(port);
  251. assertBrandedFallbackText(html);
  252. assert(!html.includes(ASSET_URL), 'disabled telemetry should omit the remote image');
  253. });
  254. });
  255. await test('DISABLE_TELEMETRY=true omits remote image for Claude Code telemetry opt-out', async () => {
  256. const port = 3455;
  257. const dir = '/tmp/brainstorm-branding-claude-disable-telemetry';
  258. await withServer({ port, dir, env: { DISABLE_TELEMETRY: 'true' } }, async () => {
  259. writeFragment(dir);
  260. await sleep(300);
  261. const html = await fetchHtml(port);
  262. assertBrandedFallbackText(html);
  263. assert(!html.includes(ASSET_URL), 'Claude Code telemetry opt-out should omit the remote image');
  264. });
  265. });
  266. await test('CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 omits remote image for Claude Code traffic opt-out', async () => {
  267. const port = 3456;
  268. const dir = '/tmp/brainstorm-branding-claude-disable-nonessential';
  269. await withServer({ port, dir, env: { CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1' } }, async () => {
  270. const html = await fetchHtml(port);
  271. assertBrandedFallbackText(html);
  272. assert(!html.includes(ASSET_URL), 'Claude Code non-essential traffic opt-out should omit the remote image');
  273. });
  274. });
  275. console.log(`\n--- Results: ${passed} passed, ${failed} failed ---`);
  276. if (failed > 0) process.exitCode = 1;
  277. }
  278. main().catch((err) => {
  279. console.error('Test failed:', err);
  280. process.exit(1);
  281. });