@@ -22,6 +22,8 @@ const { readFile, writeFile } = await import('node:fs/promises');
2222const { createSolutionFiles } = await import (
2323 '../../src/initialize/createSolutionFiles.js'
2424) ;
25+ const { replaceTokens } = await import ( '../../src/initialize/replaceTokens.js' ) ;
26+ const { getSolutionFileName } = await import ( '../../src/solutions/solutionRunner.js' ) ;
2527
2628describe ( 'initialize' , ( ) => {
2729 describe ( 'createSolutionFiles()' , ( ) => {
@@ -50,39 +52,129 @@ describe('initialize', () => {
5052 expect ( ensureDir ) . toHaveBeenCalledWith ( 'asdf' ) ;
5153 } ) ;
5254
53- test ( 'loads template solution file' , async ( ) => {
55+ test ( 'loads default template solution file' , async ( ) => {
5456 getConfigValue . mockImplementation ( ( key ) => {
5557 switch ( key ) {
5658 case 'paths.solutionsDir' :
5759 return 'asdf' ;
58- case 'paths.templates.solution ' :
59- return 'asdf .txt' ;
60+ case 'paths.templates.solutionDefault ' :
61+ return 'default .txt' ;
6062 case 'aoc.validation.days' :
6163 return [ ] ;
6264 default :
6365 return undefined ;
6466 }
6567 } ) ;
6668 await createSolutionFiles ( { year : 2022 } ) ;
67- expect ( readFile ) . toHaveBeenCalledWith ( 'asdf .txt' , expect . anything ( ) ) ;
69+ expect ( readFile ) . toHaveBeenCalledWith ( 'default .txt' , expect . anything ( ) ) ;
6870 } ) ;
6971
70- test ( 'creates file for each day' , async ( ) => {
71- const days = [ 1 , 2 , 3 , 4 , 5 ] ;
72+ test ( 'loads last day template solution file' , async ( ) => {
7273 getConfigValue . mockImplementation ( ( key ) => {
7374 switch ( key ) {
7475 case 'paths.solutionsDir' :
7576 return 'asdf' ;
76- case 'paths.templates.solution' :
77- return 'asdf.txt' ;
77+ case 'paths.templates.solutionLastDay' :
78+ return 'lastDay.txt' ;
79+ case 'aoc.validation.days' :
80+ return [ ] ;
81+ default :
82+ return undefined ;
83+ }
84+ } ) ;
85+ await createSolutionFiles ( { year : 2022 } ) ;
86+ expect ( readFile ) . toHaveBeenCalledWith ( 'lastDay.txt' , expect . anything ( ) ) ;
87+ } ) ;
88+
89+ test ( 'creates a file for each day' , async ( ) => {
90+ const days = [ 1 , 2 , 3 , 4 , 5 ] ;
91+
92+ // setup mock to return days.
93+ getConfigValue . mockImplementation ( ( key ) => {
94+ switch ( key ) {
7895 case 'aoc.validation.days' :
7996 return days ;
8097 default :
8198 return undefined ;
8299 }
83100 } ) ;
101+
84102 await createSolutionFiles ( { year : 2022 } ) ;
85103 expect ( writeFile ) . toHaveBeenCalledTimes ( days . length ) ;
86104 } ) ;
105+
106+ test ( 'creates (days - 1) default solution files' , async ( ) => {
107+ const days = [ 1 , 2 , 3 , 4 , 5 ] ;
108+ const defaultTemplateName = 'default.txt' ;
109+ const defaultTemplateContents = 'DEFAULT' ;
110+
111+ // setup mocks to ensure expected values are passed through
112+ getConfigValue . mockImplementation ( ( key ) => {
113+ switch ( key ) {
114+ case 'paths.templates.solutionDefault' :
115+ return defaultTemplateName ;
116+ case 'aoc.validation.days' :
117+ return days ;
118+ default :
119+ return undefined ;
120+ }
121+ } ) ;
122+ replaceTokens . mockImplementation ( ( tokens , args , target ) => target ) ;
123+ readFile . mockImplementation ( ( fileName ) => {
124+ switch ( fileName ) {
125+ case defaultTemplateName :
126+ return defaultTemplateContents ;
127+ default :
128+ return 'WRONG' ;
129+ }
130+ } ) ;
131+ getSolutionFileName . mockImplementation ( ( day ) => day ) ;
132+
133+ await createSolutionFiles ( { year : 2022 } ) ;
134+
135+ // expect num of writes with default file contents to equal n - 1
136+ const allButLastDay = new Set ( days . slice ( 0 , - 1 ) ) ;
137+ const writes = writeFile . mock . calls . filter (
138+ ( [ name , content ] ) =>
139+ allButLastDay . has ( name ) && content === defaultTemplateContents
140+ ) ;
141+ expect ( writes . length ) . toBe ( days . length - 1 ) ;
142+ } ) ;
143+
144+ test ( 'creates last day solution file' , async ( ) => {
145+ const days = [ 1 , 2 , 3 , 4 , 5 ] ;
146+ const lastDayTemplateName = 'lastDay.txt' ;
147+ const lastDayContents = 'LAST' ;
148+
149+ // setup mocks to ensure expected values are passed through
150+ getConfigValue . mockImplementation ( ( key ) => {
151+ switch ( key ) {
152+ case 'paths.templates.solutionLastDay' :
153+ return lastDayTemplateName ;
154+ case 'aoc.validation.days' :
155+ return days ;
156+ default :
157+ return undefined ;
158+ }
159+ } ) ;
160+ replaceTokens . mockImplementation ( ( tokens , args , target ) => target ) ;
161+ readFile . mockImplementation ( ( fileName ) => {
162+ switch ( fileName ) {
163+ case lastDayTemplateName :
164+ return lastDayContents ;
165+ default :
166+ return 'WRONG' ;
167+ }
168+ } ) ;
169+ getSolutionFileName . mockImplementation ( ( day ) => day ) ;
170+
171+ await createSolutionFiles ( { year : 2022 } ) ;
172+
173+ // expect one write with last day file name.
174+ const writes = writeFile . mock . calls . filter (
175+ ( [ name , content ] ) => name === days . at ( - 1 ) && content === lastDayContents
176+ ) ;
177+ expect ( writes . length ) . toBe ( 1 ) ;
178+ } ) ;
87179 } ) ;
88180} ) ;
0 commit comments