Skip to content

Commit 706b9d7

Browse files
committed
fix: Fixed integration tests
1 parent 9ad376c commit 706b9d7

9 files changed

Lines changed: 59 additions & 36 deletions

File tree

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
"js-yaml": "^4.1.0",
2020
"js-yaml-source-map": "^0.2.2",
2121
"json-source-map": "^0.6.1",
22+
"latest-semver": "^4.0.0",
23+
"nanoid": "^5.0.9",
2224
"parse-json": "^8.1.0",
2325
"react": "^18.3.1",
2426
"semver": "^7.5.4",
25-
"latest-semver": "^4.0.0",
26-
"supports-color": "^9.4.0",
27-
"nanoid": "^5.0.9"
27+
"supports-color": "^9.4.0"
2828
},
2929
"description": "Codify allows users to configure settings, install new packages, and automate their systems using code instead of the GUI. Get set up on a new laptop in one click, maintain a Codify file within your project so anyone can get started and never lose your cool apps or favourite settings again.",
3030
"devDependencies": {
@@ -38,15 +38,15 @@
3838
"@types/semver": "^7.5.4",
3939
"@types/strip-ansi": "^5.2.1",
4040
"@typescript-eslint/eslint-plugin": "^8.16.0",
41-
"codify-plugin-lib": "^1.0.129",
41+
"codify-plugin-lib": "^1.0.130",
4242
"esbuild": "^0.24.0",
4343
"esbuild-plugin-copy": "^2.1.1",
4444
"eslint": "^8.51.0",
4545
"eslint-config-oclif": "^5",
46-
"eslint-config-oclif-typescript": "^3",
46+
"eslint-config-oclif-typescript": "^3.1.13",
4747
"eslint-config-prettier": "^9.0.0",
48-
"mocha": "^10",
4948
"memfs": "^4.14.0",
49+
"mocha": "^10",
5050
"oclif": "^4.15.29",
5151
"shx": "^0.3.3",
5252
"strip-ansi": "^7.1.0",

src/common/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
// eslint-disable-next-line @typescript-eslint/ban-types
2-
export type NonMethodKeys<T> = { [P in keyof T]: T[P] extends Function ? never : P }[keyof T];
1+
/* eslint no-use-before-define: 0 */
2+
export type NonMethodKeys<T> = {
3+
[key in keyof T]: T[key] extends Function ? never : key
4+
}[keyof T];
35
export type RemoveMethods<T> = Pick<T, NonMethodKeys<T>>;
46

57
export type RemoveErrorMethods<T> = Omit<RemoveMethods<T>, 'name'>;
8+
/* eslint no-use-before-define: 2 */

src/entities/resource-config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ export class ResourceConfig implements ConfigBlock {
4343
this.sourceMapKey = sourceMapKey;
4444
}
4545

46+
static fromJson(json: ResourceJson): ResourceConfig {
47+
return new ResourceConfig({
48+
...json.core,
49+
...json.parameters,
50+
})
51+
}
52+
4653
get id() {
4754
return this.name ? `${this.type}.${this.name}` : this.type;
4855
}

src/orchestrators/import.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { ResourceConfig , ResourceConfig as SchemaResourceConfig } from 'codify-schemas';
1+
import { ResourceJson } from 'codify-schemas';
22

33
import { Project } from '../entities/project.js';
4+
import { ResourceConfig } from '../entities/resource-config.js';
45
import { ProcessName, SubProcessName, ctx } from '../events/context.js';
56
import { CodifyParser } from '../parser/index.js';
67
import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js';
@@ -102,15 +103,18 @@ export class ImportOrchestrator {
102103
for (const type of typeIds) {
103104
ctx.subprocessStarted(SubProcessName.IMPORT_RESOURCE, type);
104105
try {
105-
const config: SchemaResourceConfig = {
106-
type,
107-
...userSuppliedProperties.get(type),
106+
const config: ResourceJson = {
107+
core: { type },
108+
parameters: userSuppliedProperties.get(type) ?? {},
108109
};
109110

110111
const response = await pluginManager.importResource(config);
111112

112113
if (response.result !== null && response.result.length > 0) {
113-
importedConfigs.push(...response.result);
114+
importedConfigs.push(...response
115+
?.result
116+
?.map((r) => ResourceConfig.fromJson(r)) ?? []
117+
);
114118
} else {
115119
errors.push(`Unable to import resource '${type}', resource not found`);
116120
}

src/plugins/plugin-manager.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
GetResourceInfoResponseData,
33
ImportResponseData,
4+
ResourceJson,
45
ValidateResponseData,
56
} from 'codify-schemas';
67

@@ -66,18 +67,18 @@ export class PluginManager {
6667
return plugin.getResourceInfo(type);
6768
}
6869

69-
async importResource(config: ResourceConfig): Promise<ImportResponseData> {
70-
const pluginName = this.resourceToPluginMapping.get(config.type);
70+
async importResource(config: ResourceJson): Promise<ImportResponseData> {
71+
const pluginName = this.resourceToPluginMapping.get(config.core.type);
7172
if (!pluginName) {
72-
throw new Error(`Unable to find plugin for resource: ${config.type}`);
73+
throw new Error(`Unable to find plugin for resource: ${config.core.type}`);
7374
}
7475

7576
const plugin = this.plugins.get(pluginName)
7677
if (!plugin) {
77-
throw new Error(`Unable to find plugin for resource ${config.type}`);
78+
throw new Error(`Unable to find plugin for resource ${config.core.type}`);
7879
}
7980

80-
return plugin.import(config.toJson());
81+
return plugin.import(config);
8182
}
8283

8384
async getPlan(project: Project): Promise<Plan> {

src/plugins/plugin.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ const getResourceInfoResponseValidator = ajv.compile(GetResourceInfoResponseData
2424
const importResponseValidator = ajv.compile(ImportResponseDataSchema);
2525
const planResponseValidator = ajv.compile(PlanResponseDataSchema);
2626

27-
export class Plugin {
27+
export interface IPlugin {
28+
initialize(secureMode: boolean): Promise<InitializeResponseData>;
29+
validate(configs: ResourceConfig[]): Promise<ValidateResponseData>;
30+
getResourceInfo(type: string): Promise<GetResourceInfoResponseData>;
31+
import(config: ResourceJson): Promise<ImportResponseData>;
32+
plan(request: PlanRequestData): Promise<ResourcePlan>;
33+
apply(plan: ResourcePlan): Promise<void>;
34+
}
35+
36+
export class Plugin implements IPlugin {
2837

2938
process?: PluginProcess;
3039
name: string;

test/orchestrator/destroy/destroy.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ vi.mock('../../../src/plugins/plugin.js', async () => {
1414

1515
describe('Destroy orchestrator tests', () => {
1616
it('Can destroy a resource (simple, no required attributes, from Codify.json)', async () => {
17+
console.log('start')
1718
const reporter = new MockReporter({
1819
validatePlan(plan: Plan) {
1920
expect(plan.getResourcePlan('mock.0')).toMatchObject({
@@ -25,6 +26,7 @@ describe('Destroy orchestrator tests', () => {
2526
}
2627
});
2728

29+
console.log('middle')
2830
MockOs.create('mock', {
2931
propA: 'current',
3032
propB: 1,

test/orchestrator/mocks/plugin.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { Plugin } from 'codify-plugin-lib'
2-
import { GetResourceInfoResponseData, ImportResponseData, InitializeResponseData, ResourceConfig as SchemaResourceConfig, ValidateResponseData } from 'codify-schemas';
1+
import { Plugin as PluginLibrary } from 'codify-plugin-lib'
2+
import { GetResourceInfoResponseData, ImportResponseData, InitializeResponseData,
3+
PlanRequestData, ResourceJson, ValidateResponseData } from 'codify-schemas';
34

45
import { ResourcePlan } from '../../../src/entities/plan.js';
5-
import { PlanRequest } from '../../../src/entities/plan-request.js';
66
import { ResourceConfig } from '../../../src/entities/resource-config.js';
7+
import { IPlugin } from '../../../src/plugins/plugin.js';
78
import { MockResource } from './resource.js';
89

9-
export class MockPlugin {
10+
export class MockPlugin implements IPlugin {
1011
name = 'default';
1112
version = '0.0.0'
1213
path = '/'
13-
plugin!: Plugin;
14+
plugin!: PluginLibrary;
1415

1516
async initialize(secureMode: boolean): Promise<InitializeResponseData> {
16-
this.plugin = Plugin.create(
17+
this.plugin = PluginLibrary.create(
1718
'default',
1819
[new MockResource()],
1920
);
@@ -22,24 +23,19 @@ export class MockPlugin {
2223
}
2324

2425
async validate(configs: ResourceConfig[]): Promise<ValidateResponseData> {
25-
const rawConfigs = configs.map((c) => c.raw);
26-
return this.plugin.validate({ configs: rawConfigs });
26+
return this.plugin.validate({ configs: configs.map((c) => c.toJson()) });
2727
}
2828

2929
async getResourceInfo(type: string): Promise<GetResourceInfoResponseData> {
3030
return this.plugin.getResourceInfo({ type });
3131
}
3232

33-
async import(config: SchemaResourceConfig): Promise<ImportResponseData> {
34-
return this.plugin.import({ config })
33+
async import(config: ResourceJson): Promise<ImportResponseData> {
34+
return this.plugin.import(config)
3535
}
3636

37-
async plan(request: PlanRequest): Promise<ResourcePlan> {
38-
const data = await this.plugin.plan({
39-
desired: request.desired,
40-
state: request.state,
41-
isStateful: request.isStateful
42-
});
37+
async plan(request: PlanRequestData): Promise<ResourcePlan> {
38+
const data = await this.plugin.plan(request);
4339

4440
return new ResourcePlan(data);
4541
}

test/orchestrator/mocks/resource.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { CreatePlan, DestroyPlan, ModifyPlan, ParameterChange, Resource, ResourceSettings } from 'codify-plugin-lib';
22
import { StringIndexedObject } from 'codify-schemas';
33

4-
import schema from './resource-schema.json';
54
import { MockOs } from './system.js';
65

6+
const schema = (await import('./resource-schema.json', { assert: { type: 'json' } })).default;
7+
78
export interface MockResourceConfig extends StringIndexedObject {
89
propA: string;
910
propB: number;

0 commit comments

Comments
 (0)