Files
ComfyUI_frontend/src/utils/searchAndReplace.test.ts
Alexander Brown 10feb1fd5b chore: migrate tests from tests-ui/ to colocate with source files (#7811)
## Summary

Migrates all unit tests from `tests-ui/` to colocate with their source
files in `src/`, improving discoverability and maintainability.

## Changes

- **What**: Relocated all unit tests to be adjacent to the code they
test, following the `<source>.test.ts` naming convention
- **Config**: Updated `vitest.config.ts` to remove `tests-ui` include
pattern and `@tests-ui` alias
- **Docs**: Moved testing documentation to `docs/testing/` with updated
paths and patterns

## Review Focus

- Migration patterns documented in
`temp/plans/migrate-tests-ui-to-src.md`
- Tests use `@/` path aliases instead of relative imports
- Shared fixtures placed in `__fixtures__/` directories

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-7811-chore-migrate-tests-from-tests-ui-to-colocate-with-source-files-2da6d73d36508147a4cce85365dee614)
by [Unito](https://www.unito.io)

---------

Co-authored-by: Amp <amp@ampcode.com>
Co-authored-by: GitHub Action <action@github.com>
2026-01-05 16:32:24 -08:00

87 lines
2.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { LGraph } from '@/lib/litegraph/src/litegraph'
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
import { applyTextReplacements } from '@/utils/searchAndReplace'
describe('applyTextReplacements', () => {
// Test specifically the filename sanitization part
describe('filename sanitization', () => {
it('should replace invalid filename characters with underscores', () => {
// Mock the minimal app structure needed
const mockNodes = [
{
title: 'TestNode',
widgets: [
{
name: 'testWidget',
value:
'file/name?with<invalid>chars\\:*|"control\x00chars\x1F\x7F'
}
]
} as LGraphNode
]
const mockGraph = new LGraph()
for (const node of mockNodes) {
mockGraph.add(node)
}
const result = applyTextReplacements(mockGraph, '%TestNode.testWidget%')
// The expected result should have all invalid characters replaced with underscores
expect(result).toBe('file_name_with_invalid_chars_____control_chars__')
})
it('should handle various invalid filename characters individually', () => {
const testCases = [
{ input: '/', expected: '_' },
{ input: '?', expected: '_' },
{ input: '<', expected: '_' },
{ input: '>', expected: '_' },
{ input: '\\', expected: '_' },
{ input: ':', expected: '_' },
{ input: '*', expected: '_' },
{ input: '|', expected: '_' },
{ input: '"', expected: '_' },
{ input: '\x00', expected: '_' }, // NULL character
{ input: '\x1F', expected: '_' }, // Unit separator
{ input: '\x7F', expected: '_' } // Delete character
]
for (const { input, expected } of testCases) {
const mockNodes = [
{
title: 'TestNode',
widgets: [{ name: 'testWidget', value: input }]
} as LGraphNode
]
const mockGraph = new LGraph()
for (const node of mockNodes) {
mockGraph.add(node)
}
const result = applyTextReplacements(mockGraph, '%TestNode.testWidget%')
expect(result).toBe(expected)
}
})
it('should not modify valid filename characters', () => {
const validChars = 'abcABC123.-_ '
const mockNodes = [
{
title: 'TestNode',
widgets: [{ name: 'testWidget', value: validChars }]
} as LGraphNode
]
const mockGraph = new LGraph()
for (const node of mockNodes) {
mockGraph.add(node)
}
const result = applyTextReplacements(mockGraph, '%TestNode.testWidget%')
expect(result).toBe(validChars)
})
})
})