|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { describe, it, expect, beforeEach, vi } from 'vitest' |
| 4 | +import { setupTest, runRule } from '../utils.js' |
| 5 | +import rule from '../../src/rules/shared-ui-index-required.js' |
| 6 | + |
| 7 | +const mockFileSystem = (hasIndex = true, indexFilename = 'index.ts') => { |
| 8 | + vi.spyOn(fs, 'existsSync').mockImplementation((p) => { |
| 9 | + const s = String(p) |
| 10 | + // simulate shared/ui path: '/shared/ui' |
| 11 | + if (s.includes(`${path.sep}shared${path.sep}ui`)) { |
| 12 | + if (s.endsWith(indexFilename)) return hasIndex |
| 13 | + return true |
| 14 | + } |
| 15 | + return false |
| 16 | + }) |
| 17 | +} |
| 18 | + |
| 19 | +describe('vue-modular/shared-ui-index-required', () => { |
| 20 | + beforeEach(setupTest) |
| 21 | + |
| 22 | + it('reports when shared/ui index missing', () => { |
| 23 | + mockFileSystem(false) |
| 24 | + const filename = path.join(process.cwd(), 'src', 'shared', 'ui', 'Button.vue') |
| 25 | + const ctx = runRule(rule, filename, [{ shared: 'shared', index: 'index.ts' }]) |
| 26 | + expect(ctx.report).toHaveBeenCalled() |
| 27 | + }) |
| 28 | + |
| 29 | + it('does not report when shared/ui index present', () => { |
| 30 | + mockFileSystem(true) |
| 31 | + const filename = path.join(process.cwd(), 'src', 'shared', 'ui', 'Button.vue') |
| 32 | + const ctx = runRule(rule, filename, [{ shared: 'shared', index: 'index.ts' }]) |
| 33 | + expect(ctx.report).not.toHaveBeenCalled() |
| 34 | + }) |
| 35 | + |
| 36 | + it('does nothing when not under shared/ui', () => { |
| 37 | + mockFileSystem(false) |
| 38 | + const filename = path.join(process.cwd(), 'src', 'shared', 'components', 'Button.vue') |
| 39 | + const ctx = runRule(rule, filename, [{ shared: 'shared', index: 'index.ts' }]) |
| 40 | + expect(ctx.report).not.toHaveBeenCalled() |
| 41 | + }) |
| 42 | + |
| 43 | + it('does nothing when configured shared segment is not in filename path', () => { |
| 44 | + mockFileSystem(false) |
| 45 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'ui', 'Button.vue') |
| 46 | + const ctx = runRule(rule, filename, [{ shared: 'shared', index: 'index.ts' }]) |
| 47 | + expect(ctx.report).not.toHaveBeenCalled() |
| 48 | + }) |
| 49 | + |
| 50 | + it('runs only once per session', () => { |
| 51 | + mockFileSystem(false) |
| 52 | + const filename = path.join(process.cwd(), 'src', 'shared', 'ui', 'Button.vue') |
| 53 | + const first = runRule(rule, filename, [{ shared: 'shared', index: 'index.ts' }]) |
| 54 | + const second = runRule(rule, filename, [{ shared: 'shared', index: 'index.ts' }]) |
| 55 | + |
| 56 | + expect(first.report).toHaveBeenCalled() |
| 57 | + expect(second.report).not.toHaveBeenCalled() |
| 58 | + }) |
| 59 | + |
| 60 | + it('does nothing for virtual filenames', () => { |
| 61 | + mockFileSystem(false) |
| 62 | + const ctx = runRule(rule, '<input>', [{ shared: 'shared', index: 'index.ts' }]) |
| 63 | + expect(ctx.report).not.toHaveBeenCalled() |
| 64 | + }) |
| 65 | +}) |
0 commit comments