* feat(history): concept heading and summary The Prompt history page takes the approved mock's heading and summary surface (docs/concepts/workspace/app.js renderHistory a591-598, #249 F1): - Heading description is "Prompt history · one positive, one shared gallery"; the actions stay "← Prompt library", "Edit metadata" and button.send "Send to ComfyUI" (aria-label "Send prompt pair to ComfyUI: <title>"), on one row at 1600. Send is disabled only while the selected pair's roles are loading (D24). - The summary is article.surface > span.chip category, the positive and selected negative prompt blocks, then .card-meta "N tracked runs · N images · N negative variants". Tracked runs come from the recorded history window's recorded_run_count (hidden until a read completes, kept while a refresh is in flight); images and variants from the saved positive's image_count and variant_count. - The summary's copy/send actions are named after the prompt, as in the concept ("Copy positive prompt: <title>"); mountPromptBlocks gains setTitle so an in-place metadata edit renames them without a remount. - mountRecordedHistory gains an optional onRecordedCount callback. Selector mapping: - .library-surface (history summary) -> .library-history article.surface - .library-surface .library-chip -> article.surface > span.chip - "Copy|Send <role> prompt[ to ComfyUI]" in "Saved prompt texts" -> "...: <title>" - heading p.muted "One positive prompt. Reusable negative variants. One shared gallery." -> "Prompt history · one positive, one shared gallery" - Unchanged: region "Saved prompt texts", .library-variants, .library-shared-gallery, .recorded-history (still .library-surface). Tests: workspace-history "history heading actions stay on one row at 1600 and are enabled after load" and "summary meta uses recorded runs" (both failed before), a prompt-blocks setTitle unit, and the mapped selectors in workspace-visual, workspace-library-editor and the canonical comparison. * feat(history): concept negative variants panel The history's Negative variants panel is the concept's (app.js a598, style.css 1059-1154): section.surface.timeline-panel with the panel heading (h2, p.small "Choose a red dot to change the negative and filter this gallery."), row actions Compare negatives (aria-pressed; its section is Task F3), + Add negative and the approved Show archived; a .timeline of .positive-lane and .variant-lane rows (--lane-offset 38 + 47i) with the "−" .variant-dot, "<name> · N runs" and a .run-dot when the pair's run_count > 1; and .timeline-lower with the legend and All images for this positive. D26: All images for this positive opens the concept dialog with the embedded, bounded (Load more) media gallery instead of filtering the shared gallery. The run dot opens Recorded run for the pair's newest recorded execution, else a Repeat run dialog with the pair's newest linked image. Backing data: - library variants carry prompt_pairs.run_count; - GET /prompt_manager/library/{positive_id}/history/pairs/{pair_id}/newest reads the newest run in the positive's exact raw-text scope whose current-epoch saved-pair hint still resolves to that pair (the get_history_run hint rule), inside the fenced history snapshot. - workspace-dialogs gains openContentDialog (onClose releases the body). - mountRecordedHistory returns openRun. D45 (new deviations): - a dot is named "Select negative <title>" when no other loaded lane has that title, else "Select negative variant N", so names stay unique; - the bounded "N of M saved negative variants" count stays a visually hidden status; "Saved combinations, not an execution timeline." is gone; - archived lanes (#235) are concept lanes marked .library-variant-archived at .6 emphasis with their Restore button; coarse dots keep a 47px target; the heading, legend row and All images button wrap at 390px and 200% zoom. Retired CSS: .library-branches, .library-positive-lane, .library-variant-lane, .library-variant-dot, .library-variants, .library-panel-heading, .library-variant-footer, .library-variant-summary. Selector mapping: - .library-variants -> .library-history .timeline-panel (region "Saved negative variants" unchanged) - .library-branches -> .timeline; .library-positive-lane -> .positive-lane - .library-variant-lane -> .variant-lane; .library-variant-dot -> .variant-dot - .library-variant-archived and .library-variant-restore unchanged - .library-variant-footer / button:last-child -> .timeline-lower / button - .library-variants > p.muted -> .timeline-panel .panel-heading .small - "Select negative variant N" -> "Select negative <title>" for titled pairs (history, native library, inspector, visual and real-host fixtures); untitled pairs unchanged - lane text "· N linked image(s)" -> "· N run(s)" - All images for this positive: aria-pressed filter -> dialog "All images for this positive"; tests that used it for the whole gallery select the lane linking the image, or read the dialog - variant panel row actions gain Compare negatives first Tests: new workspace-history "variants panel matches the concept", "run dot appears only for pairs with more than one run and opens its run", "all images dialog lists every image for the positive"; new workspace-library-editor "show archived and restore still work" (all four failed before the change). Python: variants carry run_count, newest pair run (DB and route). Unit: content dialog. The history fixture counts two canonical pairs and links one image to Clean geometry. * feat(history): compare negative variants Compare negatives now opens the concept's comparison (app.js a599-616, style.css 1155-1179) as a section.surface right after the timeline panel: h2 "Compare negative variants", p "Baseline compared with <name>. Choose another red dot to change the comparison.", a .compare-grid of two .compare-item negative blocks (baseline, selected), p.small "Added terms", then span.diff-added "+ term" per added term or p.small "No added terms.". Choosing another red dot updates the same section in place; pressing Compare negatives again removes it. D25: the baseline is the oldest active variant; with no selection the selected side falls back to it, as the concept's v does. The pure addedTerms(base, selected) in history-state.mjs is the concept's rule: comma split, trim, drop empty terms, exact case-sensitive difference that keeps the selected order and repeats. D46 (new deviations): - lanes are newest first, so the baseline is the lowest-id active variant, not the first lane; while the variant page has more, the oldest is not loaded and the section says "Load more negative variants to reach the baseline." instead of guessing; - the two blocks are named "Baseline" and "Selected" (regions and copy/send actions), so no name repeats on the page; - Compare negatives is disabled when there is no active variant. Selector mapping: none changed; the section is new (.library-history .timeline-panel + section.surface). Tests: unit "added terms split, trim and exact-diff like the concept"; workspace-history "compare toggles, follows the selected dot and stacks at 390" and "compare waits for the page holding the oldest active negative" (all failed before the change). * test(history): guard the newest-pair route and record D45/D46 TESTING.md lists the Prompt history page's spec decisions (D24-D26) and its new deviations D45 and D46 beside D23 and D40-D44, notes under D46 that production opens on the newest lane while the concept opens on its baseline, adds GET /prompt_manager/library/{positive_id}/history/pairs/ {pair_id}/newest to the history route list with its guards, and documents the detail variants' run_count. Tests: the newest-pair read skips a stale reference_epoch row, stops at 16 stale-hint candidates (17 newer stale rows hide an older valid run and report {run: null}), and rejects a negative-only or deleted positive; the route answers 404 for an ineligible positive, 503 storage-unavailable for storage failures without leaking details, and 409 history-changed for a restore during the snapshot. * fix(history): run-dot touch target, unique dot names and dialog cleanup - D45: under pointer: coarse the 12px run dot gets a 44px ::after target, and while a run dot shows the lane text wraps within 139px before it. - D45: dot names compare trimmed titles and never use a title that reads like a fallback "variant N", so names stay unique. - All images and Repeat run share one dialog-scope slot: each open replaces the previous scope instead of adding a cleanup per open. - A run-dot lookup that settles after the view went stale opens nothing. - Retire the unused .library-main .library-chip rule and the stale "Saved combinations" comparison note. Tests (workspace-history, Chromium and touch): the coarse run-dot target and lane width; a failed newest lookup shows the toast and Repeat run; Recorded run that cannot open (recorded history failed) falls back to Repeat run; an archived pair's Repeat run reads archive=archived; Compare negatives is disabled with no active variant; dot names stay unique. * fix(workspace): one inert handle for aborted history and prompt-block mounts An aborted parent no longer gets a separate never-called stub per method: mountRecordedHistory returns its hoisted openRun (it answers false before reading later state), and mountPromptBlocks returns one shared no-op for update, setState, setTitle and dispose. This keeps the history.mjs and prompt-blocks.mjs function-coverage floors (98) without lowering them. Tests: test:coverage (Chromium and touch, 922 workflows) passes for all 61 files; it failed before on history.mjs 94.44 and prompt-blocks.mjs 93.75 function coverage.
266 lines
22 KiB
JavaScript
266 lines
22 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {createScope} from '../../web/js/shared/toolkit/core.js';
|
|
import * as module from '../../web/js/workspace/prompt-inspector.mjs';
|
|
|
|
function dom() {
|
|
class Element {
|
|
constructor(tag){this.tagName=tag;this.children=[];this.dataset={};this.attributes={};this.listeners=new Map();this.classList={add:()=>{}};this.className='';}
|
|
append(...children){for(const child of children){child.parent=this;this.children.push(child);}}
|
|
replaceChildren(...children){this.children=[];this.append(...children);}
|
|
setAttribute(key,value){this.attributes[key]=value;}
|
|
addEventListener(type,listener,{signal}={}){this.listeners.set(type,listener);signal?.addEventListener('abort',()=>this.listeners.delete(type),{once:true});}
|
|
closest(selector){if(selector==='section'&&this.tagName==='section')return this;if(selector==='[data-send-role]'&&Object.hasOwn(this.dataset,'sendRole'))return this;if(selector==='[data-copy-role]'&&Object.hasOwn(this.dataset,'copyRole'))return this;if(selector==='[data-retry-prompts]'&&Object.hasOwn(this.dataset,'retryPrompts'))return this;return this.parent?.closest(selector);}
|
|
click(target){this.listeners.get('click')?.({target});}
|
|
}
|
|
const previous=global.document,toast=new Element('div');global.document={createElement:tag=>new Element(tag),createElementNS:(_,tag)=>new Element(tag),getElementById:id=>id==='workspace-toast'?toast:null};
|
|
const walk=(root,predicate)=>[...(predicate(root)?[root]:[]),...root.children.flatMap(child=>walk(child,predicate))];
|
|
return {Element,walk,toast,restore:()=>{global.document=previous;}};
|
|
}
|
|
const record=text=>({status:'resolved',text,source:'library'});
|
|
test('shared prompt blocks send complete selected library values and explicit empty negatives',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div'),sent=[];
|
|
try{
|
|
const view=module.mountPromptBlocks(container,{parentScope:scope,sender:{open:roles=>sent.push(roles)},prompts:{positive:record('<literal>\nfull positive'),negative:{status:'unresolved',message:'Choose a saved negative.'}}});
|
|
assert.ok(f.walk(container,node=>node.textContent==='<literal>\nfull positive').length);
|
|
assert.equal(f.walk(container,node=>node.dataset.sendRole==='pair').length,0);
|
|
view.update({positive:record('Complete positive'),negative:record('')});
|
|
const pair=f.walk(container,node=>node.dataset.sendRole==='pair')[0];container.click(pair);
|
|
assert.deepEqual(sent,[{positive:'Complete positive',negative:''}]);
|
|
assert.equal(f.walk(container,node=>node.textContent==='Saved library prompt').length,0,'no per-block source line');
|
|
view.dispose();container.click(pair);assert.equal(sent.length,1);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
test('positive-only cards never display or send a hidden negative or pair',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div'),sent=[];
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,roles:['positive'],prompts:{positive:record('positive'),negative:record('hidden')},sender:{open:roles=>sent.push(roles)}});
|
|
assert.equal(f.walk(container,node=>node.attributes['aria-label']==='Negative prompt').length,0);
|
|
assert.equal(f.walk(container,node=>node.dataset.sendRole==='pair').length,0);
|
|
const button=f.walk(container,node=>node.dataset.sendRole==='positive')[0];container.click(button);assert.deepEqual(sent,[{positive:'positive'}]);
|
|
scope.dispose();container.click(button);assert.equal(sent.length,1);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
|
|
test('disposed blocks ignore updates and aborted parents never mount content',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
try{
|
|
const view=module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:record('original')}});
|
|
view.dispose();view.update({positive:record('late')});assert.equal(f.walk(container,node=>node.textContent==='late').length,0);
|
|
scope.dispose();const other=new f.Element('div');module.mountPromptBlocks(other,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:record('late')}});assert.equal(other.children.length,0);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
|
|
test('frozen fields keep null, empty and whitespace actions distinct without a source line',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div'),sent=[];
|
|
const field=(text,source)=>({status:text===null?'unresolved':'resolved',text,source:'execution',evidence:{provenance:'recorded',source,reason:null}});
|
|
try{
|
|
const view=module.mountPromptBlocks(container,{parentScope:scope,sender:{open:roles=>sent.push(roles)},prompts:{positive:field('positive','runtime_return'),negative:field(null,'unknown')}});
|
|
assert.equal(f.walk(container,node=>node.dataset.sendRole==='negative'||node.dataset.sendRole==='pair'||node.dataset.copyRole==='negative').length,0);
|
|
assert.equal(f.walk(container,node=>node.textContent?.includes('runtime_return')).length,0,'no per-block source line');
|
|
view.update({positive:field('positive','runtime_return'),negative:field('','host_cache_output')});
|
|
assert.equal(f.walk(container,node=>node.textContent?.includes('host_cache_output')).length,0);
|
|
const pair=f.walk(container,node=>node.dataset.sendRole==='pair')[0];container.click(pair);
|
|
assert.deepEqual(sent,[{positive:'positive',negative:''}]);
|
|
view.update({positive:field('positive','runtime_return'),negative:field(' \n\t ','host_cache_output')});
|
|
assert.equal(f.walk(container,node=>node.tagName==='p'&&node.className==='prompt-text'&&node.textContent===' \n\t ').length,1);
|
|
container.click(f.walk(container,node=>node.dataset.sendRole==='negative')[0]);
|
|
assert.deepEqual(sent[1],{negative:' \n\t '});
|
|
view.setState({prompts:{positive:field('positive','runtime_return'),negative:field('old','host_cache_output')},actionsEnabled:false});
|
|
container.click(f.walk(container,node=>node.dataset.sendRole==='pair')[0]);assert.equal(sent.length,2);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
|
|
test('a zeroed conditioning negative shows as an explicit empty negative without a source line',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div'),sent=[];
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:roles=>sent.push(roles)},prompts:{positive:{status:'resolved',text:'positive',source:'graph-inferred'},negative:{status:'resolved',text:'',source:'zeroed-conditioning'}}});
|
|
assert.equal(f.walk(container,node=>node.textContent==='Zeroed conditioning (no negative text)').length,0);
|
|
assert.equal(f.walk(container,node=>node.className==='prompt-text'&&node.textContent==='No negative prompt (explicitly empty)').length,1);
|
|
container.click(f.walk(container,node=>node.dataset.sendRole==='pair')[0]);
|
|
assert.deepEqual(sent,[{positive:'positive',negative:''}]);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
|
|
const unresolved=message=>({status:'unresolved',text:null,source:null,...(message===undefined?{}:{message})});
|
|
const statusLines=(f,container)=>f.walk(container,node=>node.tagName==='p'&&node.className==='prompt-text muted').map(node=>node.textContent);
|
|
test('unsupported node status names the class',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
const line='Prompt uses a node the inspector does not support (DualCFGGuider).';
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:unresolved(line),negative:unresolved(line)}});
|
|
assert.deepEqual(statusLines(f,container),[line,line]);
|
|
assert.equal(f.walk(container,node=>node.dataset.sendRole!==undefined||node.dataset.copyRole!==undefined).length,0,'no action implies a prompt exists');
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
test('runtime and composite statuses',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
const runtime='Prompt is chosen at run time (PromptSearchList).',composite='Prompt combines several texts (ConditioningCombine).';
|
|
try{
|
|
const view=module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:unresolved(runtime),negative:unresolved(runtime)}});
|
|
assert.deepEqual(statusLines(f,container),[runtime,runtime]);
|
|
view.update({positive:unresolved(composite),negative:{status:'resolved',text:'kept',source:'execution'}});
|
|
assert.deepEqual(statusLines(f,container),[composite]);
|
|
assert.equal(f.walk(container,node=>node.tagName==='p'&&node.className==='prompt-text'&&node.textContent==='kept').length,1);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
test('limit status',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
const line='Prompt exceeds inspection limits.';
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:unresolved(line),negative:unresolved(line)}});
|
|
assert.deepEqual(statusLines(f,container),[line,line]);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
test('roles without a usable line keep the status copy, and a candidate is never shown as a prompt',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:unresolved(''),negative:{status:'ambiguous',text:'candidate text',source:null}}});
|
|
assert.deepEqual(statusLines(f,container),['Prompt could not be resolved.','Multiple possible prompts. Source could not be determined.']);
|
|
assert.equal(f.walk(container,node=>node.textContent==='candidate text').length,0);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
test('class names in status lines render as text, never markup',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
const line='Prompt uses a node the inspector does not support (<img src=x onerror=alert(1)>).';
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:unresolved(line),negative:unresolved(line)}});
|
|
assert.deepEqual(statusLines(f,container),[line,line]);
|
|
assert.equal(f.walk(container,node=>node.tagName==='img').length,0);
|
|
assert.equal(f.walk(container,node=>Object.hasOwn(node,'innerHTML')).length,0);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
|
|
test('a context label names nested prompt regions and actions apart from the frozen blocks while the visible heading stays',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},contextLabel:'Current file',prompts:{positive:record('p'),negative:record('n')}});
|
|
const labels=f.walk(container,node=>node.attributes['aria-label']).map(node=>node.attributes['aria-label']);
|
|
for(const name of ['Current file positive prompt','Current file negative prompt','Copy current file positive prompt','Send current file positive prompt to ComfyUI','Copy current file negative prompt','Send current file negative prompt to ComfyUI']) assert.ok(labels.includes(name),name);
|
|
assert.equal(labels.filter(name=>/^(Positive|Negative) prompt$/.test(name)).length,0);
|
|
assert.deepEqual(f.walk(container,node=>node.className==='role-label').map(node=>node.textContent),['+ Positive prompt','\u2212 Negative prompt']);
|
|
const plain=new f.Element('div');module.mountPromptBlocks(plain,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:record('p')}});
|
|
assert.ok(f.walk(plain,node=>node.attributes['aria-label']==='Positive prompt').length);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
|
|
test('a title names each role action after its prompt as the concept does',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},title:'Alpine observatory',prompts:{positive:record('p'),negative:record('n')}});
|
|
const actions=f.walk(container,node=>node.tagName==='button').map(node=>node.attributes['aria-label']);
|
|
assert.deepEqual(actions,['Copy positive prompt: Alpine observatory','Send positive prompt to ComfyUI: Alpine observatory',
|
|
'Copy negative prompt: Alpine observatory','Send negative prompt to ComfyUI: Alpine observatory','Send prompt pair']);
|
|
const nested=new f.Element('div');
|
|
module.mountPromptBlocks(nested,{parentScope:scope,sender:{open:()=>{}},contextLabel:'Current file',title:'Alpine observatory',includePairAction:false,prompts:{positive:record('p')}});
|
|
assert.deepEqual(f.walk(nested,node=>node.tagName==='button').map(node=>node.attributes['aria-label']),
|
|
['Copy current file positive prompt: Alpine observatory','Send current file positive prompt to ComfyUI: Alpine observatory']);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
|
|
test('a title names each action after its prompt (concept "Copy positive prompt: <title>") while regions keep their names',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},title:'Alpine observatory',roles:['positive'],prompts:{positive:record('p')}});
|
|
const labels=f.walk(container,node=>node.attributes['aria-label']).map(node=>node.attributes['aria-label']);
|
|
assert.deepEqual(labels,['Positive prompt','Copy positive prompt: Alpine observatory','Send positive prompt to ComfyUI: Alpine observatory']);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
test('setTitle renames the actions in place and keeps the last prompts and state',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
try{
|
|
const view=module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},title:'Old name',roles:['positive'],prompts:{positive:record('p')}});
|
|
view.setState({prompts:{positive:record('kept')},actionsEnabled:false});
|
|
const labels=()=>f.walk(container,node=>node.attributes['aria-label']).map(node=>node.attributes['aria-label']);
|
|
const rendered=container.children[0];view.setTitle('Old name');assert.equal(container.children[0],rendered,'an unchanged title does not re-render');
|
|
view.setTitle('New name');
|
|
assert.deepEqual(labels(),['Positive prompt','Copy positive prompt: New name','Send positive prompt to ComfyUI: New name']);
|
|
assert.ok(f.walk(container,node=>node.textContent==='kept').length);
|
|
assert.ok(f.walk(container,node=>node.dataset.copyRole==='positive').every(node=>node.disabled===true));
|
|
view.setTitle('');assert.deepEqual(labels(),['Positive prompt','Copy positive prompt','Send positive prompt to ComfyUI']);
|
|
scope.dispose();view.setTitle('After dispose');
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
const shape=node=>({tag:node.tagName,className:node.className,...(node.attributes['aria-label']?{label:node.attributes['aria-label']}:{}),
|
|
...(node.children.length?{children:node.children.filter(child=>child.tagName!=='svg').map(shape)}:{text:node.textContent})});
|
|
test('prompt block emits concept markup without a source line',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div');
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,includePairAction:false,sender:{open:()=>{}},prompts:{positive:{...record('a lighthouse'),evidence:{provenance:'recorded',source:'runtime_return'}},negative:record('blurry')}});
|
|
const section=(role,label,text)=>({tag:'section',className:`prompt-section ${role}`,label:`${role[0].toUpperCase()}${role.slice(1)} prompt`,children:[
|
|
{tag:'div',className:'prompt-heading',children:[{tag:'span',className:'role-label',text:label},{tag:'div',className:'prompt-actions',children:[
|
|
{tag:'button',className:'icon-btn',label:`Copy ${role} prompt`,children:[]},{tag:'button',className:'send icon-btn',label:`Send ${role} prompt to ComfyUI`,children:[]}]}]},
|
|
{tag:'p',className:'prompt-text',text}]});
|
|
assert.deepEqual(container.children.map(shape),[section('positive','+ Positive prompt','a lighthouse'),section('negative','\u2212 Negative prompt','blurry')]);
|
|
for(const button of f.walk(container,node=>node.tagName==='button')) assert.equal(button.children.filter(child=>child.tagName==='svg').length,1,'one icon per action');
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
test('empty negative uses the concept copy and still sends an explicit clear',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div'),sent=[];
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:roles=>sent.push(roles)},prompts:{positive:record('positive'),negative:record('')}});
|
|
const negative=container.children[1];
|
|
assert.equal(negative.children[1].className,'prompt-text');assert.equal(negative.children[1].textContent,'No negative prompt (explicitly empty)');
|
|
container.click(f.walk(negative,node=>node.dataset.sendRole==='negative')[0]);
|
|
container.click(f.walk(container,node=>node.dataset.sendRole==='pair')[0]);
|
|
assert.deepEqual(sent,[{negative:''},{positive:'positive',negative:''}]);
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
test('unknown roles never offer copy or send',()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div'),sent=[];
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,roles:['positive','negative','style'],sender:{open:roles=>sent.push(roles)},
|
|
prompts:{positive:{status:'absent'},negative:{status:'ambiguous',text:'candidate'},style:record('not a role')}});
|
|
assert.deepEqual(container.children.map(node=>node.className),['prompt-section positive','prompt-section negative'],'an unknown role name renders nothing');
|
|
assert.deepEqual(statusLines(f,container),['Not recorded in this file.','Multiple possible prompts. Source could not be determined.']);
|
|
assert.equal(f.walk(container,node=>node.dataset.sendRole!==undefined||node.dataset.copyRole!==undefined||node.className==='prompt-actions').length,0);
|
|
const forged=new f.Element('button');forged.dataset.sendRole='style';container.append(forged);container.click(forged);
|
|
assert.equal(sent.length,0,'a forged action for an unknown role sends nothing');
|
|
}finally{scope.dispose();f.restore();}
|
|
});
|
|
test('copy shows the shared toast "Prompt copied." with the exact role text',async()=>{
|
|
const f=dom(),scope=createScope(),container=new f.Element('div'),copied=[];
|
|
const previous=Object.getOwnPropertyDescriptor(globalThis,'navigator');
|
|
Object.defineProperty(globalThis,'navigator',{configurable:true,value:{clipboard:{writeText:async text=>{copied.push(text);}}}});
|
|
try{
|
|
module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:record(' exact\ntext '),negative:record('')}});
|
|
await container.listeners.get('click')({target:f.walk(container,node=>node.dataset.copyRole==='positive')[0]});
|
|
assert.deepEqual(copied,[' exact\ntext ']);assert.equal(f.toast.textContent,'Prompt copied.');
|
|
assert.equal(f.walk(container,node=>node.attributes.role==='status').length,0,'no per-block copy status line');
|
|
}finally{scope.dispose();f.restore();if(previous)Object.defineProperty(globalThis,'navigator',previous);else delete globalThis.navigator;}
|
|
});
|
|
|
|
/** Runs one copy click with `clipboard` as `navigator.clipboard`, recording what the fallback seam receives. */
|
|
async function copyWithClipboard(clipboard,{detach=false}={}) {
|
|
const f=dom(),scope=createScope(),container=new f.Element('div'),fallbacks=[];
|
|
const previous=Object.getOwnPropertyDescriptor(globalThis,'navigator');
|
|
Object.defineProperty(globalThis,'navigator',{configurable:true,value:clipboard===undefined?{}:{clipboard}});
|
|
try{
|
|
const view=module.mountPromptBlocks(container,{parentScope:scope,sender:{open:()=>{}},prompts:{positive:record('exact text'),negative:record('')},
|
|
copyFallback:(text,options)=>fallbacks.push({text,...options})});
|
|
const copy=f.walk(container,node=>node.dataset.copyRole==='positive')[0];
|
|
let current=copy;
|
|
if(detach) {
|
|
// The blocks re-render while the clipboard is pending: the clicked button is detached, its successor is not.
|
|
copy.isConnected=false;container.querySelector=selector=>selector==='[data-copy-role="positive"]'?current:null;
|
|
}
|
|
const pending=container.listeners.get('click')({target:copy});
|
|
if(detach) {view.update({positive:record('exact text'),negative:record('')});current=f.walk(container,node=>node.dataset.copyRole==='positive')[0];}
|
|
await pending;
|
|
return {fallbacks,toast:f.toast.textContent,copy,current,scope};
|
|
}finally{scope.dispose();f.restore();if(previous)Object.defineProperty(globalThis,'navigator',previous);else delete globalThis.navigator;}
|
|
}
|
|
test('copy without a clipboard opens the select-to-copy fallback and shows no toast',async()=>{
|
|
const {fallbacks,toast,copy,scope}=await copyWithClipboard(undefined);
|
|
assert.equal(fallbacks.length,1);assert.equal(fallbacks[0].text,'exact text');assert.equal(fallbacks[0].trigger,copy);assert.ok(fallbacks[0].scope);
|
|
assert.notEqual(toast,'Prompt copied.');assert.equal(scope.signal.aborted,true);
|
|
});
|
|
test('a refused clipboard write opens the fallback instead of the toast',async()=>{
|
|
const {fallbacks,toast}=await copyWithClipboard({writeText:async()=>{throw new Error('Denied');}});
|
|
assert.equal(fallbacks.length,1);assert.equal(fallbacks[0].text,'exact text');assert.notEqual(toast,'Prompt copied.');
|
|
});
|
|
test('a fallback after a re-render returns focus to the same-role copy button, not the detached one',async()=>{
|
|
const {fallbacks,copy,current}=await copyWithClipboard({writeText:async()=>{throw new Error('Denied');}},{detach:true});
|
|
assert.equal(fallbacks.length,1);assert.notEqual(current,copy);assert.equal(fallbacks[0].trigger,current);
|
|
});
|