|
| 1 | +import { |
| 2 | + issueSourceSchema, |
| 3 | + sourceFileLocationSchema, |
| 4 | + sourceUrlLocationSchema, |
| 5 | +} from './source.js'; |
| 6 | + |
| 7 | +describe('sourceFileLocationSchema', () => { |
| 8 | + it('should accept valid file location with position', () => { |
| 9 | + expect(() => |
| 10 | + sourceFileLocationSchema.parse({ |
| 11 | + file: 'src/index.ts', |
| 12 | + position: { startLine: 10, endLine: 15 }, |
| 13 | + }), |
| 14 | + ).not.toThrow(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should accept file location without position', () => { |
| 18 | + expect(() => |
| 19 | + sourceFileLocationSchema.parse({ file: 'src/utils.ts' }), |
| 20 | + ).not.toThrow(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should reject empty file path', () => { |
| 24 | + expect(() => sourceFileLocationSchema.parse({ file: '' })).toThrow( |
| 25 | + 'Too small', |
| 26 | + ); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('sourceUrlLocationSchema', () => { |
| 31 | + it('should accept valid URL location with all fields', () => { |
| 32 | + expect(() => |
| 33 | + sourceUrlLocationSchema.parse({ |
| 34 | + url: 'https://example.com/page', |
| 35 | + snippet: '<img src="logo.png">', |
| 36 | + selector: 'img.logo', |
| 37 | + }), |
| 38 | + ).not.toThrow(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should accept URL location with only required url field', () => { |
| 42 | + expect(() => |
| 43 | + sourceUrlLocationSchema.parse({ url: 'https://example.com' }), |
| 44 | + ).not.toThrow(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should accept URL location with snippet only', () => { |
| 48 | + expect(() => |
| 49 | + sourceUrlLocationSchema.parse({ |
| 50 | + url: 'https://example.com/dashboard', |
| 51 | + snippet: '<button disabled>Submit</button>', |
| 52 | + }), |
| 53 | + ).not.toThrow(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should accept URL location with selector only', () => { |
| 57 | + expect(() => |
| 58 | + sourceUrlLocationSchema.parse({ |
| 59 | + url: 'https://example.com/form', |
| 60 | + selector: '#submit-btn', |
| 61 | + }), |
| 62 | + ).not.toThrow(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should reject invalid URL', () => { |
| 66 | + expect(() => |
| 67 | + sourceUrlLocationSchema.parse({ url: 'not-a-valid-url' }), |
| 68 | + ).toThrow('Invalid URL'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should reject missing URL', () => { |
| 72 | + expect(() => |
| 73 | + sourceUrlLocationSchema.parse({ snippet: '<div>No URL provided</div>' }), |
| 74 | + ).toThrow('Invalid input'); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe('issueSourceSchema', () => { |
| 79 | + it('should accept file-based source', () => { |
| 80 | + expect(() => |
| 81 | + issueSourceSchema.parse({ |
| 82 | + file: 'src/app.ts', |
| 83 | + position: { startLine: 1 }, |
| 84 | + }), |
| 85 | + ).not.toThrow(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should accept URL-based source', () => { |
| 89 | + expect(() => |
| 90 | + issueSourceSchema.parse({ |
| 91 | + url: 'https://example.com', |
| 92 | + selector: '#main', |
| 93 | + }), |
| 94 | + ).not.toThrow(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should reject source with neither file nor url', () => { |
| 98 | + expect(() => |
| 99 | + issueSourceSchema.parse({ position: { startLine: 1 } }), |
| 100 | + ).toThrow('Invalid input'); |
| 101 | + }); |
| 102 | +}); |
0 commit comments