/** * Test script for view-model-taxonomy rule */ const rule = require('./view-model-taxonomy.js'); const { Linter } = require('eslint'); const linter = new Linter(); // Register the plugin linter.defineRule('gridpilot-rules/view-model-taxonomy', rule); // Test 1: DTO import should be caught const codeWithDtoImport = ` import type { RecordEngagementOutputDTO } from '@/lib/types/generated/RecordEngagementOutputDTO'; export class RecordEngagementOutputViewModel { eventId: string; engagementWeight: number; constructor(dto: RecordEngagementOutputDTO) { this.eventId = dto.eventId; this.engagementWeight = dto.engagementWeight; } } `; // Test 2: Inline ViewData interface should be caught const codeWithInlineViewData = ` export interface RaceViewData { id: string; name: string; } export class RaceViewModel { private readonly data: RaceViewData; constructor(data: RaceViewData) { this.data = data; } } `; // Test 3: Valid code (no violations) const validCode = ` import { RaceViewData } from '@/lib/view-data/RaceViewData'; export class RaceViewModel { private readonly data: RaceViewData; constructor(data: RaceViewData) { this.data = data; } } `; // Test 4: Disallowed import from service layer (should be caught) const codeWithServiceImport = ` import { SomeService } from '@/lib/services/SomeService'; export class RaceViewModel { private readonly service: SomeService; constructor(service: SomeService) { this.service = service; } } `; // Test 5: Strict import violation (import from non-allowed path) const codeWithStrictImportViolation = ` import { SomeOtherThing } from '@/lib/other/SomeOtherThing'; export class RaceViewModel { private readonly thing: SomeOtherThing; constructor(thing: SomeOtherThing) { this.thing = thing; } } `; console.log('=== Test 1: DTO import ==='); const messages1 = linter.verify(codeWithDtoImport, { parser: '@typescript-eslint/parser', parserOptions: { ecmaVersion: 2020, sourceType: 'module', }, rules: { 'gridpilot-rules/view-model-taxonomy': 'error', }, }); console.log('Messages:', messages1); console.log('Expected: Should have 1 error for DTO import'); console.log('Actual: ' + messages1.length + ' error(s)'); console.log(''); console.log('=== Test 2: Inline ViewData interface ==='); const messages2 = linter.verify(codeWithInlineViewData, { parser: '@typescript-eslint/parser', parserOptions: { ecmaVersion: 2020, sourceType: 'module', }, rules: { 'gridpilot-rules/view-model-taxonomy': 'error', }, }); console.log('Messages:', messages2); console.log('Expected: Should have 1 error for inline ViewData interface'); console.log('Actual: ' + messages2.length + ' error(s)'); console.log(''); console.log('=== Test 3: Valid code ==='); const messages3 = linter.verify(validCode, { parser: '@typescript-eslint/parser', parserOptions: { ecmaVersion: 2020, sourceType: 'module', }, rules: { 'gridpilot-rules/view-model-taxonomy': 'error', }, }); console.log('Messages:', messages3); console.log('Expected: Should have 0 errors'); console.log('Actual: ' + messages3.length + ' error(s)'); console.log(''); console.log('=== Test 4: Service import (should be caught) ==='); const messages4 = linter.verify(codeWithServiceImport, { parser: '@typescript-eslint/parser', parserOptions: { ecmaVersion: 2020, sourceType: 'module', }, rules: { 'gridpilot-rules/view-model-taxonomy': 'error', }, }); console.log('Messages:', messages4); console.log('Expected: Should have 1 error for service import'); console.log('Actual: ' + messages4.length + ' error(s)'); console.log(''); console.log('=== Test 5: Strict import violation ==='); const messages5 = linter.verify(codeWithStrictImportViolation, { parser: '@typescript-eslint/parser', parserOptions: { ecmaVersion: 2020, sourceType: 'module', }, rules: { 'gridpilot-rules/view-model-taxonomy': 'error', }, }); console.log('Messages:', messages5); console.log('Expected: Should have 1 error for strict import violation'); console.log('Actual: ' + messages5.length + ' error(s)'); console.log(''); console.log('=== Summary ==='); console.log('Test 1 (DTO import): ' + (messages1.length === 1 ? '✓ PASS' : '✗ FAIL')); console.log('Test 2 (Inline ViewData): ' + (messages2.length === 1 ? '✓ PASS' : '✗ FAIL')); console.log('Test 3 (Valid code): ' + (messages3.length === 0 ? '✓ PASS' : '✗ FAIL')); console.log('Test 4 (Service import): ' + (messages4.length === 1 ? '✓ PASS' : '✗ FAIL')); console.log('Test 5 (Strict import): ' + (messages5.length === 1 ? '✓ PASS' : '✗ FAIL'));