Skip to content

Commit 98c581c

Browse files
committed
fix: Fixed most tests
1 parent 6f652df commit 98c581c

File tree

7 files changed

+42
-34
lines changed

7 files changed

+42
-34
lines changed

src/orchestrators/destroy.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ export class DestroyOrchestrator {
6969
pluginManager.apply(uninstallProject, filteredPlan)
7070
)
7171

72-
await reporter.displayApplyComplete([]);
72+
await reporter.displayMessage(`
73+
🎉 Finished applying 🎉
74+
Open a new terminal or source '.zshrc' for the new changes to be reflected`);
7375
}
7476

7577
private static async validate(project: Project, pluginManager: PluginManager, dependencyMap: DependencyMap): Promise<void> {

src/orchestrators/import.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class ImportOrchestrator {
6666
}
6767

6868
/** Import new resources. Type ids supplied. This will ask for any required parameters */
69-
static async runNewImport(typeIds: string[], reporter: Reporter, initializeResult: InitializationResult): Promise<ResourceConfig[]> {
69+
static async runNewImport(typeIds: string[], reporter: Reporter, initializeResult: InitializationResult): Promise<void> {
7070
const { project, pluginManager, typeIdsToDependenciesMap } = initializeResult;
7171

7272
const matchedTypes = this.matchTypeIds(typeIds, [...typeIdsToDependenciesMap.keys()])
@@ -87,7 +87,7 @@ export class ImportOrchestrator {
8787
}
8888

8989
/** Update an existing project. This will use the existing resources as the parameters (no user input required). */
90-
static async runExistingProject(reporter: Reporter, initializeResult: InitializationResult): Promise<ResourceConfig[]> {
90+
static async runExistingProject(reporter: Reporter, initializeResult: InitializationResult): Promise<void> {
9191
const { pluginManager, project } = initializeResult;
9292

9393
await pluginManager.validate(project);
@@ -106,8 +106,6 @@ export class ImportOrchestrator {
106106
resourceInfoList,
107107
project.codifyFiles[0],
108108
);
109-
110-
return project.resourceConfigs;
111109
}
112110

113111
static async import(

src/ui/components/default-component.test.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,4 @@ describe('DefaultComponent', () => {
5353

5454
expect(lastFrame()).toContain('Password:');
5555
});
56-
57-
it('renders import parameter form when renderStatus is IMPORT_PROMPT', () => {
58-
store.set(store.renderState, { status: RenderStatus.IMPORT_PROMPT, data: new Map() });
59-
const { lastFrame } = render(<DefaultComponent emitter={emitter} />);
60-
61-
expect(lastFrame()).toContain('Mock Import Parameters Form');
62-
});
6356
});

src/utils/file-modification-calculator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ export class FileModificationCalculator {
9090
newFile = this.insert(newFile, newResourcesToInsert, insertionIndex);
9191

9292
const lastCharacterIndex = this.existingFile.contents.lastIndexOf(']')
93-
const ending = this.existingFile.contents.slice(Math.min(lastCharacterIndex + 1, this.existingFile.contents.length - 1));
94-
newFile += ending;
93+
if (lastCharacterIndex < this.existingFile.contents.length - 1) {
94+
const ending = this.existingFile.contents.slice(lastCharacterIndex + 1);
95+
newFile += ending;
96+
}
9597

9698
return {
9799
newFile: newFile,

test/orchestrator/apply/apply.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Apply orchestrator tests', () => {
3232
});
3333

3434
const applyConfirmationSpy = vi.spyOn(reporter, 'promptConfirmation');
35-
const applyCompleteSpy = vi.spyOn(reporter, 'displayApplyComplete');
35+
const applyCompleteSpy = vi.spyOn(reporter, 'displayMessage');
3636

3737
console.log(MockOs.get('xcode-tools'))
3838
expect(MockOs.get('mock')).to.be.undefined;
@@ -69,7 +69,7 @@ describe('Apply orchestrator tests', () => {
6969
});
7070

7171
const applyConfirmationSpy = vi.spyOn(reporter, 'promptConfirmation');
72-
const applyCompleteSpy = vi.spyOn(reporter, 'displayApplyComplete');
72+
const applyCompleteSpy = vi.spyOn(reporter, 'displayMessage');
7373

7474
MockOs.destroy('xcode-tools');
7575
expect(MockOs.get('xcode-tools')).to.be.undefined;

test/orchestrator/initialize/initialize.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ describe('Parser integration tests', () => {
6868

6969
console.log(project);
7070
expect(project).toMatchObject({
71-
path: folder,
71+
codifyFiles: expect.arrayContaining([
72+
path.resolve(folder, 'home.codify.json'),
73+
path.resolve(folder, 'home-2.codify.json')
74+
]),
7275
resourceConfigs: expect.arrayContaining([
7376
expect.objectContaining({
7477
type: 'customType1',
@@ -110,7 +113,10 @@ describe('Parser integration tests', () => {
110113

111114
console.log(project);
112115
expect(project).toMatchObject({
113-
path: folder,
116+
codifyFiles: expect.arrayContaining([
117+
path.resolve(folder, 'home.codify.json'),
118+
path.resolve(folder, 'home-2.codify.json')
119+
]),
114120
resourceConfigs: expect.arrayContaining([
115121
expect.objectContaining({
116122
type: 'customType1',

test/orchestrator/mocks/reporter.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { SpawnStatus, SudoRequestData, SudoRequestResponseData } from 'codify-schemas';
22

33
import { Plan } from '../../../src/entities/plan.js';
4-
import { ImportResult, RequiredParameters, UserSuppliedParameters } from '../../../src/orchestrators/import.js';
4+
import { ResourceConfig } from '../../../src/entities/resource-config.js';
5+
import { ResourceInfo } from '../../../src/entities/resource-info.js';
6+
import { ImportResult } from '../../../src/orchestrators/import.js';
57
import { prettyFormatPlan } from '../../../src/ui/plan-pretty-printer.js';
6-
import { Reporter } from '../../../src/ui/reporters/reporter.js';
8+
import { PromptType, Reporter } from '../../../src/ui/reporters/reporter.js';
9+
import { FileModificationResult } from '../../../src/utils/file-modification-calculator.js';
710

811
export interface MockReporterConfig {
912
validatePlan?: (plan: Plan) => Promise<void> | void;
10-
validateApplyComplete?: (message: string[]) => Promise<void> | void;
13+
validateMessage?: (message: string) => Promise<void> | void;
1114
validateImport?: (result: ImportResult) => Promise<void> | void;
1215
promptApplyConfirmation?: () => boolean;
13-
askRequiredParametersForImport?: (requiredParameters: RequiredParameters) => Promise<UserSuppliedParameters> | UserSuppliedParameters;
16+
promptOptions?: (message: string, options: string[]) => string;
17+
promptUserForValues?: (resourceInfo: ResourceInfo[]) => Promise<ResourceConfig[]> | ResourceConfig[];
1418
displayImportResult?: (importResult: ImportResult) => Promise<void> | void;
19+
displayFileModifications?: (diff: { file: string; modification: FileModificationResult; }[]) => void,
1520
}
1621

1722
export class MockReporter implements Reporter {
@@ -21,9 +26,17 @@ export class MockReporter implements Reporter {
2126
this.config = config ?? null;
2227
}
2328

24-
async displayApplyComplete(message: string[]): Promise<void> {
29+
async promptOptions(message: string, options: string[]): Promise<string> {
30+
return this.config?.promptOptions?.(message, options) ?? options[0];
31+
}
32+
33+
async displayFileModifications(diff: { file: string; modification: FileModificationResult; }[]): Promise<void> {
34+
this.config?.displayFileModifications?.(diff);
35+
}
36+
37+
async displayMessage(message: string): Promise<void> {
2538
console.log(JSON.stringify(message, null, 2));
26-
await this.config?.validateApplyComplete?.(message);
39+
await this.config?.validateMessage?.(message);
2740
}
2841

2942
async displayPlan(plan: Plan): Promise<void> {
@@ -42,18 +55,12 @@ export class MockReporter implements Reporter {
4255
}
4356
}
4457

45-
async promptUserForParameterValues(requiredParameters: RequiredParameters): Promise<UserSuppliedParameters> {
46-
if (this.config?.askRequiredParametersForImport) {
47-
return this.config.askRequiredParametersForImport(requiredParameters);
48-
}
49-
50-
const result = new Map<string, Record<string, string>>();
51-
52-
for (const parameter of requiredParameters) {
53-
result.set(parameter[0], Object.fromEntries(parameter[1].map((p) => [p, ''])))
58+
async promptUserForValues(resourceInfo: ResourceInfo[], promptType: PromptType): Promise<ResourceConfig[]> {
59+
if (this.config?.promptUserForValues) {
60+
return this.config.promptUserForValues(resourceInfo);
5461
}
5562

56-
return result;
63+
return resourceInfo.map((i) => new ResourceConfig({ type: i.type }))
5764
}
5865

5966
displayImportResult(importResult: ImportResult): void {

0 commit comments

Comments
 (0)