diff --git a/src/utils/searchAndReplace.test.ts b/src/utils/searchAndReplace.test.ts index d2835152b68..6eaba9a88a0 100644 --- a/src/utils/searchAndReplace.test.ts +++ b/src/utils/searchAndReplace.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { applyTextReplacements } from '@/utils/searchAndReplace' @@ -12,6 +12,71 @@ function graphWithWidgetValue(value: string): LGraph { } describe('applyTextReplacements', () => { + describe('date replacements', () => { + it('preserves a date-prefixed node reference when its widget is missing', () => { + vi.setSystemTime(new Date(2026, 9, 28, 14, 5, 9)) + const graph = new LGraph() + const node = new LGraphNode('date:portrait') + node.addWidget('string', 'renamedWidget', 'portrait', () => undefined, {}) + graph.add(node) + + expect(applyTextReplacements(graph, '%date:portrait.testWidget%')).toBe( + '%date:portrait.testWidget%' + ) + expect(console.warn).toHaveBeenCalledWith( + 'Unable to find widget', + 'testWidget', + 'on node', + 'date:portrait', + node + ) + }) + + it.for([ + { source: 'title', title: 'date:portrait', alias: 'PortraitNode' }, + { source: 'alias', title: 'PortraitNode', alias: 'date:portrait' } + ])( + 'preserves node references with a date-prefixed $source', + ({ title, alias }) => { + vi.setSystemTime(new Date(2026, 9, 28, 14, 5, 9)) + const graph = new LGraph() + const node = new LGraphNode(title) + node.properties['Node name for S&R'] = alias + node.addWidget('string', 'testWidget', 'portrait', () => undefined, {}) + graph.add(node) + + expect(applyTextReplacements(graph, '%date:portrait.testWidget%')).toBe( + 'portrait' + ) + } + ) + + it.for([ + ['yyyy-MM-dd', '2026-10-28'], + ['yyyy.MM', '2026.10'], + ['yyyy.MM.dd', '2026.10.28'], + ['hh.mm.ss', '14.05.09'] + ])('preserves separators in the date format %s', ([format, expected]) => { + vi.setSystemTime(new Date(2026, 9, 28, 14, 5, 9)) + + expect(applyTextReplacements(new LGraph(), `%date:${format}%`)).toBe( + expected + ) + }) + + it('combines dated folders and filenames with node widget values', () => { + vi.setSystemTime(new Date(2026, 9, 28, 14, 5, 9)) + const graph = graphWithWidgetValue('portrait') + + expect( + applyTextReplacements( + graph, + 'outputs/%date:yyyy.MM.dd%/%TestNode.testWidget%_%date:hh.mm.ss%' + ) + ).toBe('outputs/2026.10.28/portrait_14.05.09') + }) + }) + // Test specifically the filename sanitization part describe('filename sanitization', () => { it('should replace invalid filename characters with underscores', () => { diff --git a/src/utils/searchAndReplace.ts b/src/utils/searchAndReplace.ts index c7ecd96bc74..282336ad67f 100644 --- a/src/utils/searchAndReplace.ts +++ b/src/utils/searchAndReplace.ts @@ -9,31 +9,28 @@ export function applyTextReplacements( const allNodes = collectAllNodes(graph) return value.replace(/%([^%]+)%/g, function (match, text) { + const isDate = text.startsWith('date:') + const fallbackReplacement = isDate + ? formatDate(text.substring(5), new Date()) + : match + const split = text.split('.') if (split.length !== 2) { - // Special handling for dates - if (split[0].startsWith('date:')) { - return formatDate(split[0].substring(5), new Date()) - } - - if (text !== 'width' && text !== 'height') { - // Dont warn on standard replacements + if (!isDate && text !== 'width' && text !== 'height') { console.warn('Invalid replacement pattern', text) } - return match + return fallbackReplacement } - // Find node with matching S&R property name let nodes = allNodes.filter( (n) => n.properties['Node name for S&R'] === split[0] ) - // If we can't, see if there is a node with that title if (!nodes.length) { nodes = allNodes.filter((n) => n.title === split[0]) } if (!nodes.length) { - console.warn('Unable to find node', split[0]) - return match + if (!isDate) console.warn('Unable to find node', split[0]) + return fallbackReplacement } if (nodes.length > 1) {