1+ import { TestPlan } from '../test-utils' ;
2+ import { writeFileSync , mkdirSync , rmSync } from 'fs' ;
3+ import { join } from 'path' ;
4+
5+ describe ( 'TestPlan Utility Example' , ( ) => {
6+ const testDir = join ( __dirname , 'test-fixtures' ) ;
7+
8+ beforeAll ( ( ) => {
9+ mkdirSync ( testDir , { recursive : true } ) ;
10+ } ) ;
11+
12+ afterAll ( ( ) => {
13+ rmSync ( testDir , { recursive : true , force : true } ) ;
14+ } ) ;
15+
16+ it ( 'demonstrates TestPlan usage with a simple plan' , ( ) => {
17+ // Create a simple test plan file
18+ const planContent = `%syntax-version=1.0.0
19+ %project=test-project
20+ %uri=https://github.com/test/project
21+
22+ # Initial schema
23+ users_table 2024-01-01T00:00:00Z Developer <[email protected] > # Create users table 24+ posts_table [users_table] 2024-01-02T00:00:00Z Developer <[email protected] > # Create posts table 25+
26+ # Add tag after posts_table
27+ @v1.0.0 2024-01-02T12:00:00Z Developer <[email protected] > # Version 1.0.0 28+
29+ # More changes
30+ comments_table [posts_table] 2024-01-03T00:00:00Z Developer <[email protected] > # Create comments table 31+ ` ;
32+
33+ // Write to a temporary location
34+ const tempPlanPath = join ( testDir , 'example.plan' ) ;
35+ writeFileSync ( tempPlanPath , planContent ) ;
36+
37+ // Create TestPlan instance with absolute path
38+ const testPlan = new TestPlan ( tempPlanPath ) ;
39+
40+ // Basic assertions
41+ expect ( testPlan . isValid ( ) ) . toBe ( true ) ;
42+ expect ( testPlan . getErrors ( ) ) . toHaveLength ( 0 ) ;
43+
44+ // Get insights
45+ const insights = testPlan . getInsights ( ) ;
46+ console . log ( 'Actual stats:' , insights . stats ) ;
47+ console . log ( 'Line count:' , testPlan . getLines ( ) . length ) ;
48+
49+ // Don't check exact line count as it depends on trailing newlines
50+ expect ( insights . stats . pragmaCount ) . toBe ( 3 ) ;
51+ expect ( insights . stats . changeCount ) . toBe ( 3 ) ;
52+ expect ( insights . stats . tagCount ) . toBe ( 1 ) ;
53+ expect ( insights . stats . commentCount ) . toBe ( 3 ) ; // Includes empty comment lines
54+
55+ // Check pragmas
56+ const pragmas = testPlan . getPragmas ( ) ;
57+ expect ( pragmas ) . toHaveLength ( 3 ) ;
58+
59+ const projectPragma = pragmas . find ( p => p . metadata ?. pragmaName === 'project' ) ;
60+ expect ( projectPragma ?. metadata ?. pragmaValue ) . toBe ( 'test-project' ) ;
61+
62+ // Check changes
63+ const changes = testPlan . getChanges ( ) ;
64+ expect ( changes ) . toHaveLength ( 3 ) ;
65+ expect ( changes [ 0 ] . metadata ?. changeName ) . toBe ( 'users_table' ) ;
66+ expect ( changes [ 1 ] . metadata ?. changeName ) . toBe ( 'posts_table' ) ;
67+ expect ( changes [ 1 ] . metadata ?. dependencies ) . toEqual ( [ 'users_table' ] ) ;
68+
69+ // Check tags
70+ const tags = testPlan . getTags ( ) ;
71+ expect ( tags ) . toHaveLength ( 1 ) ;
72+ expect ( tags [ 0 ] . metadata ?. tagName ) . toBe ( '@v1.0.0' ) ;
73+
74+ // Line-by-line insight
75+ const line5 = testPlan . getLineInsight ( 5 ) ;
76+ expect ( line5 ?. type ) . toBe ( 'comment' ) ;
77+ expect ( line5 ?. content ) . toContain ( 'Initial schema' ) ;
78+
79+ // Print insights for debugging (optional)
80+ console . log ( '\n=== TestPlan Demo ===' ) ;
81+ console . log ( 'Stats:' , insights . stats ) ;
82+ console . log ( 'Changes:' , changes . map ( c => c . metadata ?. changeName ) ) ;
83+ console . log ( 'Tags:' , tags . map ( t => t . metadata ?. tagName ) ) ;
84+ console . log ( '===================\n' ) ;
85+ } ) ;
86+
87+ it ( 'demonstrates error detection' , ( ) => {
88+ // Create an invalid plan file
89+ const invalidPlanContent = `%syntax-version=1.0.0
90+ # Missing %project pragma
91+
92+ invalid:change:name 2024-01-01T00:00:00Z Developer <[email protected] > # Invalid name 93+ good_change [missing_dependency] 2024-01-02T00:00:00Z Developer <[email protected] > # Missing dep 94+ ` ;
95+
96+ const tempPlanPath = join ( testDir , 'invalid.plan' ) ;
97+ writeFileSync ( tempPlanPath , invalidPlanContent ) ;
98+
99+ const testPlan = new TestPlan ( tempPlanPath ) ;
100+
101+ console . log ( 'Invalid plan errors:' , testPlan . getErrors ( ) ) ;
102+ console . log ( 'Is valid?' , testPlan . isValid ( ) ) ;
103+
104+ expect ( testPlan . isValid ( ) ) . toBe ( false ) ;
105+
106+ const errors = testPlan . getErrors ( ) ;
107+ expect ( errors . length ) . toBeGreaterThan ( 0 ) ;
108+
109+ // Check for specific errors
110+ // The parser doesn't check for missing %project pragma, only invalid syntax
111+ const hasInvalidNameError = errors . some ( e =>
112+ e . message . includes ( 'Invalid change name' )
113+ ) ;
114+ expect ( hasInvalidNameError ) . toBe ( true ) ;
115+
116+ // Get lines with errors
117+ const errorLines = testPlan . getLinesWithErrors ( ) ;
118+ expect ( errorLines . length ) . toBeGreaterThan ( 0 ) ;
119+
120+ console . log ( '\n=== Error Detection Demo ===' ) ;
121+ console . log ( 'Errors found:' , errors . length ) ;
122+ errors . forEach ( err => {
123+ console . log ( ` Line ${ err . line } : ${ err . message } ` ) ;
124+ } ) ;
125+ console . log ( '==========================\n' ) ;
126+ } ) ;
127+ } ) ;
0 commit comments