|
| 1 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | + |
| 5 | +import { MockOs } from '../mocks/system'; |
| 6 | +import { InitializeOrchestrator } from '../../../src/orchestrators/initialize'; |
| 7 | +import path from 'node:path'; |
| 8 | +import { MockReporter } from '../mocks/reporter'; |
| 9 | +import { MockResource, MockResourceConfig } from '../mocks/resource'; |
| 10 | + |
| 11 | +vi.mock('../mocks/get-mock-resources.js', async () => { |
| 12 | + return { |
| 13 | + getMockResources: () => ([ |
| 14 | + new class extends MockResource { |
| 15 | + getSettings(){ |
| 16 | + return { id: 'customType1' } |
| 17 | + } |
| 18 | + }, |
| 19 | + new class extends MockResource { |
| 20 | + getSettings(){ |
| 21 | + return { id: 'customType2' } |
| 22 | + } |
| 23 | + } |
| 24 | + ]) |
| 25 | + } |
| 26 | +}) |
| 27 | + |
| 28 | +vi.mock('node:fs/promises', async () => { |
| 29 | + const { fs } = await import('memfs'); |
| 30 | + return fs.promises; |
| 31 | +}) |
| 32 | + |
| 33 | +vi.mock('fs', async () => { |
| 34 | + const { fs } = await import('memfs'); |
| 35 | + return fs; |
| 36 | +}) |
| 37 | + |
| 38 | +vi.mock('../../../src/plugins/plugin.js', async () => { |
| 39 | + const { MockPlugin } = await import('../mocks/plugin.js'); |
| 40 | + return { Plugin: MockPlugin }; |
| 41 | +}) |
| 42 | + |
| 43 | +describe('Parser integration tests', () => { |
| 44 | + it('Finds the correct files to initialize and initializes all files within a folder', async () => { |
| 45 | + const file1Contents = |
| 46 | + `[ |
| 47 | + { "type": "customType1" } |
| 48 | +]` |
| 49 | + |
| 50 | + const file2Contents = |
| 51 | + `[ |
| 52 | + { "type": "customType2" } |
| 53 | +]` |
| 54 | + const folder = path.resolve(os.homedir(), 'Downloads', 'untitled folder') |
| 55 | + fs.mkdirSync(folder, { recursive: true }); |
| 56 | + |
| 57 | + fs.writeFileSync(path.resolve(folder, 'home.codify.json'), file1Contents); |
| 58 | + fs.writeFileSync(path.resolve(folder, 'home-2.codify.json'), file2Contents); |
| 59 | + |
| 60 | + const reporter = new MockReporter({ |
| 61 | + |
| 62 | + }); |
| 63 | + |
| 64 | + const cwdSpy = vi.spyOn(process, 'cwd'); |
| 65 | + cwdSpy.mockReturnValue(folder); |
| 66 | + |
| 67 | + const { project, pluginManager, dependencyMap } = await InitializeOrchestrator.run({}, reporter); |
| 68 | + |
| 69 | + console.log(project); |
| 70 | + expect(project).toMatchObject({ |
| 71 | + path: folder, |
| 72 | + resourceConfigs: expect.arrayContaining([ |
| 73 | + expect.objectContaining({ |
| 74 | + type: 'customType1', |
| 75 | + }), |
| 76 | + expect.objectContaining({ |
| 77 | + type: 'customType2', |
| 78 | + }) |
| 79 | + ]) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('Finds codify.json files in a previous dir', async () => { |
| 84 | + const file1Contents = |
| 85 | + `[ |
| 86 | + { "type": "customType1" } |
| 87 | +]` |
| 88 | + |
| 89 | + const file2Contents = |
| 90 | + `[ |
| 91 | + { "type": "customType2" } |
| 92 | +]` |
| 93 | + const folder = path.resolve(os.homedir(), 'Downloads', 'untitled folder') |
| 94 | + const innerFolder = path.resolve(folder, 'inner folder') |
| 95 | + |
| 96 | + fs.mkdirSync(folder, { recursive: true }); |
| 97 | + fs.mkdirSync(innerFolder, { recursive: true }); |
| 98 | + |
| 99 | + fs.writeFileSync(path.resolve(folder, 'home.codify.json'), file1Contents); |
| 100 | + fs.writeFileSync(path.resolve(folder, 'home-2.codify.json'), file2Contents); |
| 101 | + |
| 102 | + const reporter = new MockReporter({ |
| 103 | + |
| 104 | + }); |
| 105 | + |
| 106 | + const cwdSpy = vi.spyOn(process, 'cwd'); |
| 107 | + cwdSpy.mockReturnValue(innerFolder); |
| 108 | + |
| 109 | + const { project, pluginManager, dependencyMap } = await InitializeOrchestrator.run({}, reporter); |
| 110 | + |
| 111 | + console.log(project); |
| 112 | + expect(project).toMatchObject({ |
| 113 | + path: folder, |
| 114 | + resourceConfigs: expect.arrayContaining([ |
| 115 | + expect.objectContaining({ |
| 116 | + type: 'customType1', |
| 117 | + }), |
| 118 | + expect.objectContaining({ |
| 119 | + type: 'customType2', |
| 120 | + }) |
| 121 | + ]) |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + afterEach(() => { |
| 126 | + vi.resetAllMocks(); |
| 127 | + MockOs.reset(); |
| 128 | + }) |
| 129 | +}) |
0 commit comments