Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion src/utils/searchAndReplace.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
)
Comment on lines +62 to +64

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Assert that valid date tokens do not emit warnings

I can remove either date-specific warning guard in applyTextReplacements without changing any value asserted by this table. The console setup records warnings but does not fail tests for them, so a valid %date:yyyy.MM% could again report an unavailable node, or a multi-dot format could report an invalid pattern, while the suite stays green. One assertion here protects that diagnostic behavior without adding another case.

Suggested change
expect(applyTextReplacements(new LGraph(), `%date:${format}%`)).toBe(
expected
)
expect(applyTextReplacements(new LGraph(), `%date:${format}%`)).toBe(
expected
)
expect(console.warn).not.toHaveBeenCalled()

})

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', () => {
Expand Down
21 changes: 9 additions & 12 deletions src/utils/searchAndReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading