11import { describe , it , expect , beforeEach , afterEach } from 'bun:test' ;
22import {
33 exposeGraphDots ,
4+ withEdgeFontColor ,
45 initBubbleChart ,
56 initDisharmonyCharts ,
67 initWasmGraphs ,
@@ -441,7 +442,66 @@ describe('initDisharmonyCharts', () => {
441442 } ) ;
442443} ) ;
443444
445+ describe ( 'withEdgeFontColor (vizdom bakes label color at parse time)' , ( ) => {
446+ it ( 'appends fontcolor to edge statements with an attribute list' , ( ) => {
447+ const dot = 'strict digraph G {\nActiveTestSuite -> TestSuite [ label = "6" weight = "6" ];\n}' ;
448+ expect ( withEdgeFontColor ( dot , '#9fb0c0' ) ) . toBe (
449+ 'strict digraph G {\nActiveTestSuite -> TestSuite [ label = "6" weight = "6" fontcolor = "#9fb0c0" ];\n}' ) ;
450+ } ) ;
451+
452+ it ( 'appends fontcolor to every edge in the graph' , ( ) => {
453+ const dot = 'strict digraph G {\nA -> B [ label = "1" ];\nB -> C [ label = "2" ];\n}' ;
454+ const themed = withEdgeFontColor ( dot , '#9fb0c0' ) ;
455+ expect ( themed ) . toContain ( 'B [ label = "1" fontcolor = "#9fb0c0" ];' ) ;
456+ expect ( themed ) . toContain ( 'C [ label = "2" fontcolor = "#9fb0c0" ];' ) ;
457+ } ) ;
458+
459+ it ( 'keeps red cycle edges red while recoloring their labels' , ( ) => {
460+ const dot = 'A -> B [ label = "2" weight = "2" color = "red" ];' ;
461+ expect ( withEdgeFontColor ( dot , '#9fb0c0' ) ) . toBe (
462+ 'A -> B [ label = "2" weight = "2" color = "red" fontcolor = "#9fb0c0" ];' ) ;
463+ } ) ;
464+
465+ it ( 'respects an edge-level fontcolor already set in the dot' , ( ) => {
466+ const dot = 'A -> B [ label = "1" fontcolor = "blue" ];' ;
467+ expect ( withEdgeFontColor ( dot , '#9fb0c0' ) ) . toBe ( dot ) ;
468+ } ) ;
469+
470+ it ( 'does not touch node statements or graph headers' , ( ) => {
471+ const dot = 'strict digraph G {\nTestSuite [ label = "TestSuite" ];\n}' ;
472+ expect ( withEdgeFontColor ( dot , '#9fb0c0' ) ) . toBe ( dot ) ;
473+ } ) ;
474+
475+ it ( 'returns non-string input unchanged' , ( ) => {
476+ expect ( withEdgeFontColor ( undefined , '#9fb0c0' ) ) . toBeUndefined ( ) ;
477+ expect ( withEdgeFontColor ( '' , '#9fb0c0' ) ) . toBe ( '' ) ;
478+ } ) ;
479+
480+ it ( 'handles compact attribute spacing' , ( ) => {
481+ expect ( withEdgeFontColor ( 'A -> B [label = "6"];' , '#abc' ) ) . toBe ( 'A -> B [label = "6" fontcolor = "#abc"];' ) ;
482+ } ) ;
483+ } ) ;
484+
444485describe ( 'initWasmGraphs' , ( ) => {
486+ // Minimal DotParser stub: records the parsed dot and yields a fixed SVG.
487+ function stubVizdom ( ) {
488+ const captured = [ ] ;
489+ window . Vizdom = {
490+ DotParser : class {
491+ parse ( dot ) {
492+ captured . push ( dot ) ;
493+ return {
494+ to_directed ( ) { return this ; } ,
495+ layout ( ) { return this ; } ,
496+ to_svg ( ) { return { to_string : ( ) => '<svg width="1"></svg>' } ; }
497+ } ;
498+ }
499+ }
500+ } ;
501+ return captured ;
502+ }
503+ afterEach ( ( ) => { delete window . Vizdom ; delete window . matchMedia ; } ) ;
504+
445505 it ( 'resolves gracefully when the WASM loader is unavailable' , async ( ) => {
446506 document . body . innerHTML = '<div id="classGraph"></div>' ;
447507 // No network/WASM in tests: must resolve, not reject
@@ -453,8 +513,84 @@ describe('initWasmGraphs', () => {
453513 await initWasmGraphs ( { classMap : { dot : 'x' , dotThresholdExceeded : true } } ) ;
454514 expect ( document . getElementById ( 'classGraph' ) . innerHTML ) . toBe ( '' ) ;
455515 } ) ;
516+
517+ it ( 'parses the dot unchanged while the light palette is active' , async ( ) => {
518+ installThemeRadios ( 'light' , false ) ;
519+ const graph = document . createElement ( 'div' ) ;
520+ graph . id = 'classGraph' ;
521+ document . body . appendChild ( graph ) ;
522+ const captured = stubVizdom ( ) ;
523+ const dot = 'strict digraph G {\nA -> B [ label = "6" ];\n}' ;
524+ await initWasmGraphs ( { classMap : { dot, dotThresholdExceeded : false } } ) ;
525+ expect ( captured ) . toEqual ( [ dot ] ) ;
526+ } ) ;
527+
528+ it ( 'appends fontcolor = #9fb0c0 to edge labels while the dark palette is active' , async ( ) => {
529+ installThemeRadios ( 'dark' , false ) ;
530+ const graph = document . createElement ( 'div' ) ;
531+ graph . id = 'classGraph' ;
532+ document . body . appendChild ( graph ) ;
533+ const captured = stubVizdom ( ) ;
534+ const dot = 'strict digraph G {\nA -> B [ label = "6" weight = "6" ];\n}' ;
535+ await initWasmGraphs ( { classMap : { dot, dotThresholdExceeded : false } } ) ;
536+ expect ( captured ) . toEqual ( [
537+ 'strict digraph G {\nA -> B [ label = "6" weight = "6" fontcolor = "#9fb0c0" ];\n}'
538+ ] ) ;
539+ expect ( graph . innerHTML ) . toBe ( '<svg class="fullscreen-svg" width="1"></svg>' ) ;
540+ } ) ;
541+
542+ it ( 're-parses graphs with fresh label colors when a theme radio changes' , async ( ) => {
543+ installThemeRadios ( 'light' , false ) ;
544+ document . documentElement . removeAttribute ( 'data-rf-theme-redraw-bound' ) ;
545+ bindThemeChartRedraw ( ) ;
546+ const graph = document . createElement ( 'div' ) ;
547+ graph . id = 'classGraph' ;
548+ document . body . appendChild ( graph ) ;
549+ const captured = stubVizdom ( ) ;
550+ const dot = 'strict digraph G {\nA -> B [ label = "6" ];\n}' ;
551+ await initWasmGraphs ( { classMap : { dot, dotThresholdExceeded : false } } ) ;
552+ const lightParses = captured . length ;
553+ expect ( captured . every ( d => ! d . includes ( 'fontcolor' ) ) ) . toBe ( true ) ;
554+
555+ const dark = document . getElementById ( 'rf-theme-dark' ) ;
556+ dark . checked = true ;
557+ dark . dispatchEvent ( new window . Event ( 'change' , { bubbles : true } ) ) ;
558+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
559+ expect ( captured . length ) . toBeGreaterThan ( lightParses ) ;
560+ expect ( captured [ captured . length - 1 ] ) . toContain ( 'fontcolor = "#9fb0c0"' ) ;
561+
562+ // Switching back re-renders with the plain dot again.
563+ const lightBack = document . getElementById ( 'rf-theme-light' ) ;
564+ lightBack . checked = true ;
565+ lightBack . dispatchEvent ( new window . Event ( 'change' , { bubbles : true } ) ) ;
566+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
567+ expect ( captured [ captured . length - 1 ] ) . toBe ( dot ) ;
568+ } ) ;
569+
570+ it ( 'prunes graphs whose container left the DOM on theme change' , async ( ) => {
571+ installThemeRadios ( 'light' , false ) ;
572+ document . documentElement . removeAttribute ( 'data-rf-theme-redraw-bound' ) ;
573+ bindThemeChartRedraw ( ) ;
574+ const graph = document . createElement ( 'div' ) ;
575+ graph . id = 'classGraph' ;
576+ document . body . appendChild ( graph ) ;
577+ const captured = stubVizdom ( ) ;
578+ const dot = 'strict digraph G {\nA -> B [ label = "6" ];\n}' ;
579+ await initWasmGraphs ( { classMap : { dot, dotThresholdExceeded : false } } ) ;
580+ const initial = captured . length ;
581+
582+ // The re-render threw the graph container away: next theme change must
583+ // not re-parse this graph at all.
584+ graph . remove ( ) ;
585+ const dark = document . getElementById ( 'rf-theme-dark' ) ;
586+ dark . checked = true ;
587+ dark . dispatchEvent ( new window . Event ( 'change' , { bubbles : true } ) ) ;
588+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
589+ expect ( captured . length ) . toBe ( initial ) ;
590+ } ) ;
456591} ) ;
457592
593+
458594describe ( 'popup helpers' , ( ) => {
459595 beforeEach ( ( ) => {
460596 document . body . innerHTML = `
0 commit comments