11import { Logger } from '@nestjs/common' ;
22import { readFile , writeFile } from 'fs/promises' ;
33import { basename , resolve } from 'path' ;
4+
45import { describe , expect , test } from 'vitest' ;
56
7+ import { FileModification } from '@app/unraid-api/unraid-file-modifier/file-modification' ;
68import DefaultPageLayoutModification from '@app/unraid-api/unraid-file-modifier/modifications/default-page-layout.modification' ;
79import NotificationsPageModification from '@app/unraid-api/unraid-file-modifier/modifications/notifications-page.modification' ;
8- import { FileModification } from '@app/unraid-api/unraid-file-modifier/file-modification' ;
910import SSOFileModification from '@app/unraid-api/unraid-file-modifier/modifications/sso.modification' ;
1011
1112interface ModificationTestCase {
@@ -16,40 +17,41 @@ interface ModificationTestCase {
1617const testCases : ModificationTestCase [ ] = [
1718 {
1819 ModificationClass : DefaultPageLayoutModification ,
19- fileUrl : 'https://github.com/unraid/webgui/raw/refs/heads/master/emhttp/plugins/dynamix/include/DefaultPageLayout.php' ,
20+ fileUrl :
21+ 'https://github.com/unraid/webgui/raw/refs/heads/master/emhttp/plugins/dynamix/include/DefaultPageLayout.php' ,
2022 } ,
2123 {
2224 ModificationClass : NotificationsPageModification ,
23- fileUrl : "https://github.com/unraid/webgui/raw/refs/heads/master/emhttp/plugins/dynamix/Notifications.page" ,
25+ fileUrl :
26+ 'https://github.com/unraid/webgui/raw/refs/heads/master/emhttp/plugins/dynamix/Notifications.page' ,
2427 } ,
2528 {
26- fileUrl : 'https://github.com/unraid/webgui/raw/refs/heads/master/emhttp/plugins/dynamix/include/.login.php' ,
29+ fileUrl :
30+ 'https://github.com/unraid/webgui/raw/refs/heads/master/emhttp/plugins/dynamix/include/.login.php' ,
2731 ModificationClass : SSOFileModification ,
28- }
32+ } ,
2933] ;
3034
3135async function testModification ( testCase : ModificationTestCase ) {
3236 // First download the file from Github
3337 const fileName = basename ( testCase . fileUrl ) ;
34-
38+
3539 const path = resolve ( __dirname , `../__fixtures__/downloaded/${ fileName } ` ) ;
36- const fileContent = await fetch ( testCase . fileUrl ) . then ( response => response . text ( ) ) ;
37- await writeFile ( path , fileContent ) ;
40+ const originalContent = await fetch ( testCase . fileUrl ) . then ( ( response ) => response . text ( ) ) ;
41+ await writeFile ( path , originalContent ) ;
3842
39- expect ( fileContent . length ) . toBeGreaterThan ( 0 ) ;
43+ expect ( originalContent . length ) . toBeGreaterThan ( 0 ) ;
4044
4145 const logger = new Logger ( ) ;
4246 const patcher = await new testCase . ModificationClass ( logger ) ;
4347 // @ts -ignore - Ignore for testing purposes
4448 patcher . filePath = path ;
45-
49+
4650 // @ts -ignore - Ignore for testing purposes
4751 const patch = await patcher . generatePatch ( ) ;
4852
4953 // Test patch matches snapshot
50- await expect ( patch ) . toMatchFileSnapshot (
51- `../patches/${ patcher . id } .patch`
52- ) ;
54+ await expect ( patch ) . toMatchFileSnapshot ( `../patches/${ patcher . id } .patch` ) ;
5355
5456 // Apply patch and verify modified file
5557 await patcher . apply ( ) ;
@@ -60,13 +62,11 @@ async function testModification(testCase: ModificationTestCase) {
6062 // Rollback and verify original state
6163 await patcher . rollback ( ) ;
6264 const revertedContent = await readFile ( path , 'utf-8' ) ;
63- await expect ( revertedContent ) . toMatchFileSnapshot (
64- `snapshots/${ fileName } .original.php`
65- ) ;
65+ await expect ( revertedContent ) . toMatch ( originalContent ) ;
6666}
6767
6868describe ( 'File modifications' , ( ) => {
69- test . each ( testCases ) ( '$name modifier correctly applies to fresh install' , async ( testCase ) => {
69+ test . each ( testCases ) ( `$fileUrl modifier correctly applies to fresh install` , async ( testCase ) => {
7070 await testModification ( testCase ) ;
7171 } ) ;
7272} ) ;
0 commit comments