|
| 1 | +import { |
| 2 | + formatPromptText, |
| 3 | + __test__ |
| 4 | +} from "../../web/js/auto-formatter.js"; |
| 5 | + |
| 6 | +const { |
| 7 | + shouldAutoFormat |
| 8 | +} = __test__; |
| 9 | + |
| 10 | +describe('AutoFormatter Functions', () => { |
| 11 | + |
| 12 | + describe('shouldAutoFormat', () => { |
| 13 | + const mockNodeInfo = (nodeType, inputName) => ({ |
| 14 | + nodeType, |
| 15 | + inputName |
| 16 | + }); |
| 17 | + |
| 18 | + test('should return false for empty text', () => { |
| 19 | + expect(shouldAutoFormat('', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(false); |
| 20 | + expect(shouldAutoFormat(' ', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(false); |
| 21 | + }); |
| 22 | + |
| 23 | + test('should return false for blocklisted nodes', () => { |
| 24 | + expect(shouldAutoFormat('some text,', mockNodeInfo('Power Puter (rgthree)', 'code'))).toBe(false); |
| 25 | + expect(shouldAutoFormat('some text,', mockNodeInfo('LoraLoaderBlockWeight //Inspire', 'block_vector'))).toBe(false); |
| 26 | + }); |
| 27 | + |
| 28 | + test('should return false for numeric data or single-letter placeholders', () => { |
| 29 | + expect(shouldAutoFormat('0,0,0,1,1,1', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(false); |
| 30 | + expect(shouldAutoFormat('0.5, -1.2, 0.8', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(false); |
| 31 | + expect(shouldAutoFormat('A,B,R', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(false); |
| 32 | + expect(shouldAutoFormat('X, 1.5, Y', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + test('should return true for text with "word + comma" pattern', () => { |
| 36 | + expect(shouldAutoFormat('1girl, blue hair,', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(true); |
| 37 | + expect(shouldAutoFormat('tag1, tag2', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should return false if "word + comma" pattern is not found', () => { |
| 41 | + expect(shouldAutoFormat('hello world', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(false); |
| 42 | + expect(shouldAutoFormat('tag1 tag2', mockNodeInfo('CLIPTextEncode', 'text'))).toBe(false); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('formatPromptText', () => { |
| 47 | + test('should format text by adding comma and space after tags', () => { |
| 48 | + const input = 'tag1,tag2,tag3'; |
| 49 | + const expected = 'tag1, tag2, tag3, '; |
| 50 | + expect(formatPromptText(input)).toBe(expected); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should remove extra spaces around tags', () => { |
| 54 | + const input = ' tag1 , tag2 '; |
| 55 | + const expected = 'tag1, tag2, '; |
| 56 | + expect(formatPromptText(input)).toBe(expected); |
| 57 | + }); |
| 58 | + |
| 59 | + test('should preserve special syntax like weights', () => { |
| 60 | + const input = '(tag1:1.2), [tag2]'; |
| 61 | + // Note: The current implementation splits by comma. |
| 62 | + // If the input is "(tag1:1.2), [tag2]", it splits into "(tag1:1.2)" and "[tag2]". |
| 63 | + // Then joins with ", ". |
| 64 | + const expected = '(tag1:1.2), [tag2], '; |
| 65 | + expect(formatPromptText(input)).toBe(expected); |
| 66 | + }); |
| 67 | + |
| 68 | + test('should handle multiple lines', () => { |
| 69 | + const input = 'tag1, tag2\ntag3, tag4'; |
| 70 | + const expected = 'tag1, tag2, \ntag3, tag4, '; |
| 71 | + expect(formatPromptText(input)).toBe(expected); |
| 72 | + }); |
| 73 | + |
| 74 | + test('should keep empty lines unchanged', () => { |
| 75 | + const input = 'tag1, tag2\n\ntag3, tag4'; |
| 76 | + const expected = 'tag1, tag2, \n\ntag3, tag4, '; |
| 77 | + expect(formatPromptText(input)).toBe(expected); |
| 78 | + }); |
| 79 | + |
| 80 | + test('should handle empty input', () => { |
| 81 | + expect(formatPromptText('')).toBe(''); |
| 82 | + expect(formatPromptText(null)).toBe(null); |
| 83 | + expect(formatPromptText(undefined)).toBe(undefined); |
| 84 | + }); |
| 85 | + |
| 86 | + test('should handle input with only spaces', () => { |
| 87 | + expect(formatPromptText(' ')).toBe(' '); |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments