@codifycli/plugin-test 1.0.0-beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +1 -0
- package/.eslintrc.json +30 -0
- package/.github/workflows/unit-test-ci.yaml +24 -0
- package/.prettierrc.json +1 -0
- package/CLAUDE.md +126 -0
- package/README.md +315 -0
- package/codify.json +9 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin-process.d.ts +19 -0
- package/dist/plugin-process.js +119 -0
- package/dist/plugin-process.js.map +1 -0
- package/dist/plugin-tester.d.ts +20 -0
- package/dist/plugin-tester.js +210 -0
- package/dist/plugin-tester.js.map +1 -0
- package/dist/shell.d.ts +12 -0
- package/dist/shell.js +37 -0
- package/dist/shell.js.map +1 -0
- package/dist/spawn.d.ts +16 -0
- package/dist/spawn.js +71 -0
- package/dist/spawn.js.map +1 -0
- package/dist/test-utils.d.ts +44 -0
- package/dist/test-utils.js +123 -0
- package/dist/test-utils.js.map +1 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +34 -0
- package/dist/utils.js.map +1 -0
- package/package.json +56 -0
- package/src/index.ts +3 -0
- package/src/plugin-process.ts +161 -0
- package/src/plugin-tester.ts +281 -0
- package/src/shell.ts +45 -0
- package/src/spawn.ts +101 -0
- package/src/test-utils.test.ts +83 -0
- package/src/test-utils.ts +147 -0
- package/src/utils.ts +42 -0
- package/test/plugin-tester.test.ts +350 -0
- package/test/test-plugin.ts +215 -0
- package/tsconfig.json +24 -0
- package/tsconfig.test.json +10 -0
package/.eslintignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/dist
|
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"oclif",
|
|
4
|
+
"oclif-typescript",
|
|
5
|
+
"prettier"
|
|
6
|
+
],
|
|
7
|
+
"rules": {
|
|
8
|
+
"object-curly-spacing": [
|
|
9
|
+
"warn",
|
|
10
|
+
"always"
|
|
11
|
+
],
|
|
12
|
+
"perfectionist/sort-classes": "off",
|
|
13
|
+
"perfectionist/sort-interfaces": "off",
|
|
14
|
+
"perfectionist/sort-enums": "off",
|
|
15
|
+
"perfectionist/sort-objects": "off",
|
|
16
|
+
"perfectionist/sort-object-types": "off",
|
|
17
|
+
"unicorn/no-array-reduce": "off",
|
|
18
|
+
"unicorn/no-array-for-each": "off",
|
|
19
|
+
"unicorn/prefer-object-from-entries": "off",
|
|
20
|
+
"unicorn/prefer-type-error": "off",
|
|
21
|
+
"no-await-in-loop": "off",
|
|
22
|
+
"quotes": [
|
|
23
|
+
"error",
|
|
24
|
+
"single"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
"ignorePatterns": [
|
|
28
|
+
"*.test.ts"
|
|
29
|
+
]
|
|
30
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
|
|
3
|
+
|
|
4
|
+
name: Node.js CI
|
|
5
|
+
|
|
6
|
+
on: [ 'push' ]
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build-and-test:
|
|
10
|
+
runs-on: ${{ matrix.os }}
|
|
11
|
+
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
os: [ ubuntu-latest, macos-latest ]
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
- name: Use Node.js 22
|
|
19
|
+
uses: actions/setup-node@v4
|
|
20
|
+
with:
|
|
21
|
+
node-version: '22.x'
|
|
22
|
+
cache: 'npm'
|
|
23
|
+
- run: npm ci
|
|
24
|
+
- run: npm run test
|
package/.prettierrc.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"@oclif/prettier-config"
|
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
This is `@codifycli/plugin-test`, a testing framework for Codify plugins. It provides utilities to test plugin lifecycle operations (initialize, validate, plan, apply, import, destroy) by spawning plugin processes and communicating via IPC messages.
|
|
8
|
+
|
|
9
|
+
## Build and Test Commands
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# Run tests
|
|
13
|
+
npm test
|
|
14
|
+
|
|
15
|
+
# Build the project
|
|
16
|
+
npm run prepublishOnly
|
|
17
|
+
|
|
18
|
+
# Start the project (for testing)
|
|
19
|
+
npm start
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Running Single Tests
|
|
23
|
+
|
|
24
|
+
Tests use Vitest. To run a specific test file:
|
|
25
|
+
```bash
|
|
26
|
+
npx vitest src/test-utils.test.ts
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
To run tests matching a pattern:
|
|
30
|
+
```bash
|
|
31
|
+
npx vitest --grep "pattern"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Architecture
|
|
35
|
+
|
|
36
|
+
### Core Components
|
|
37
|
+
|
|
38
|
+
**PluginTester** (`src/plugin-tester.ts`)
|
|
39
|
+
- Main API for testing plugins end-to-end
|
|
40
|
+
- `fullTest()`: Executes complete plugin lifecycle (validate → plan → apply → import → modify → destroy)
|
|
41
|
+
- `install()`: Tests only installation flow (validate → plan → apply)
|
|
42
|
+
- `uninstall()`: Tests destroy operations
|
|
43
|
+
- Automatically filters configs by OS using `ResourceConfig.os` field
|
|
44
|
+
- Handles multiple configs by adding unique names when needed
|
|
45
|
+
|
|
46
|
+
**PluginProcess** (`src/plugin-process.ts`)
|
|
47
|
+
- Manages individual plugin process lifecycle
|
|
48
|
+
- Spawns plugin using `fork()` with tsx loader (`--import tsx/esm`)
|
|
49
|
+
- Handles bidirectional IPC communication
|
|
50
|
+
- Responds to plugin requests: `COMMAND_REQUEST`, `PRESS_KEY_TO_CONTINUE_REQUEST`, `CODIFY_CREDENTIALS_REQUEST`
|
|
51
|
+
- Methods mirror plugin API: `initialize()`, `validate()`, `plan()`, `apply()`, `import()`
|
|
52
|
+
- Debug mode: Set `DEBUG` env var to enable `--inspect-brk=9221`
|
|
53
|
+
|
|
54
|
+
**Spawn Utilities** (`src/spawn.ts`)
|
|
55
|
+
- `testSpawn()`: Execute commands interactively in PTY (for testing)
|
|
56
|
+
- `spawnSafe()`: Low-level command execution with security checks
|
|
57
|
+
- Blocks direct `sudo` usage (must use `requiresRoot` option)
|
|
58
|
+
- Strips ANSI codes from output
|
|
59
|
+
- Supports stdin passthrough, custom env vars, cwd
|
|
60
|
+
|
|
61
|
+
**TestUtils** (`src/test-utils.ts`)
|
|
62
|
+
- `sendMessageAndAwaitResponse()`: Helper for IPC request/response pattern
|
|
63
|
+
- Shell utilities: Detects bash/zsh, provides shell rc paths and source commands
|
|
64
|
+
- Platform helpers: `isMacOS()`, `isLinux()`
|
|
65
|
+
- Prerequisite installers: `ensureHomebrewInstalledOnMacOs()`, `ensureXcodeInstalledOnMacOs()`
|
|
66
|
+
|
|
67
|
+
### IPC Communication Flow
|
|
68
|
+
|
|
69
|
+
1. Test creates `PluginProcess` which forks plugin with IPC enabled
|
|
70
|
+
2. Test sends IPC messages (e.g., `{cmd: 'plan', data: {...}, requestId: '...'}`
|
|
71
|
+
3. Plugin processes request and may send request messages back (e.g., `COMMAND_REQUEST`)
|
|
72
|
+
4. `PluginProcess` intercepts requests, executes via `spawnSafe()`, sends response
|
|
73
|
+
5. Plugin completes and sends final response matching original `requestId`
|
|
74
|
+
|
|
75
|
+
### Plugin Testing Pattern
|
|
76
|
+
|
|
77
|
+
Typical test structure:
|
|
78
|
+
```typescript
|
|
79
|
+
// Filter configs by OS automatically
|
|
80
|
+
const configs: ResourceConfig[] = [
|
|
81
|
+
{ type: 'my-resource', param: 'value', os: [ResourceOs.MACOS] }
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
await PluginTester.fullTest('/path/to/plugin', configs, {
|
|
85
|
+
validatePlan: (plans) => {
|
|
86
|
+
// Custom assertions on plan results
|
|
87
|
+
},
|
|
88
|
+
validateApply: (plans) => {
|
|
89
|
+
// Custom assertions after apply
|
|
90
|
+
},
|
|
91
|
+
testModify: {
|
|
92
|
+
modifiedConfigs: [/* updated configs */],
|
|
93
|
+
validateModify: (plans) => {
|
|
94
|
+
// Verify modify operation
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### OS Filtering
|
|
101
|
+
|
|
102
|
+
Tests automatically filter configs by OS:
|
|
103
|
+
- Configs with `os: [ResourceOs.MACOS]` only run on macOS
|
|
104
|
+
- Configs with `os: [ResourceOs.LINUX]` only run on Linux
|
|
105
|
+
- No `os` field means runs on all platforms
|
|
106
|
+
- Implemented in `PluginTester.fullTest()` using `getPlatformOs()`
|
|
107
|
+
|
|
108
|
+
## Key Technical Details
|
|
109
|
+
|
|
110
|
+
- **Module System**: ESM with NodeNext module resolution
|
|
111
|
+
- **TypeScript**: Strict mode with decorators support
|
|
112
|
+
- **Output**: Compiled to `dist/` directory
|
|
113
|
+
- **Node Version**: Requires Node 18+ (configured via `codify.json` for NVM)
|
|
114
|
+
- **IPC Validation**: All messages validated against schemas from `@codifycli/schemas` using AJV
|
|
115
|
+
- **Security**: Plugins spawned without direct sudo access; must request via `COMMAND_REQUEST`
|
|
116
|
+
- **PTY Support**: Uses `@homebridge/node-pty-prebuilt-multiarch` for interactive command execution
|
|
117
|
+
- **Shell History**: Commands run with `HISTORY_IGNORE` (bash) or `HISTIGNORE` (zsh) set
|
|
118
|
+
|
|
119
|
+
## Dependencies
|
|
120
|
+
|
|
121
|
+
Runtime dependencies (`@codifycli/schemas`, validation libraries, PTY) vs dev dependencies (`@codifycli/plugin-core` for development).
|
|
122
|
+
|
|
123
|
+
## Environment Variables
|
|
124
|
+
|
|
125
|
+
- `DEBUG`: Enable plugin debug mode with breakpoint
|
|
126
|
+
- `VITE_CODIFY_TEST_JWT`: Required for tests that need Codify credentials
|
package/README.md
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
# @codifycli/plugin-test
|
|
2
|
+
|
|
3
|
+
A comprehensive testing framework for Codify plugins. This package provides utilities to test the complete lifecycle of Codify plugins including initialization, validation, planning, applying, importing, and destroying resources.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save-dev @codifycli/plugin-test
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { PluginTester } from '@codifycli/plugin-test';
|
|
15
|
+
import { ResourceConfig, ResourceOs } from '@codifycli/schemas';
|
|
16
|
+
|
|
17
|
+
// Define the resources you want to test
|
|
18
|
+
const configs: ResourceConfig[] = [
|
|
19
|
+
{
|
|
20
|
+
type: 'homebrew',
|
|
21
|
+
os: [ResourceOs.MACOS]
|
|
22
|
+
}
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
// Run a full lifecycle test
|
|
26
|
+
await PluginTester.fullTest('/path/to/your/plugin/index.js', configs, {
|
|
27
|
+
validatePlan: (plans) => {
|
|
28
|
+
// Assert that the plan is correct
|
|
29
|
+
expect(plans[0].operation).toBe('create');
|
|
30
|
+
},
|
|
31
|
+
validateApply: (plans) => {
|
|
32
|
+
// Verify the resource was created successfully
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Features
|
|
38
|
+
|
|
39
|
+
- **Complete Lifecycle Testing**: Test all plugin operations (initialize, validate, plan, apply, import, destroy)
|
|
40
|
+
- **IPC Communication**: Spawns plugins as child processes and communicates via IPC, just like the Codify CLI
|
|
41
|
+
- **OS Filtering**: Automatically filters and runs tests based on OS compatibility
|
|
42
|
+
- **Interactive Command Execution**: Execute commands in PTY for realistic testing
|
|
43
|
+
- **Security Testing**: Validates that plugins properly request elevated permissions
|
|
44
|
+
- **Modify Testing**: Test resource modification scenarios
|
|
45
|
+
|
|
46
|
+
## API Reference
|
|
47
|
+
|
|
48
|
+
### PluginTester
|
|
49
|
+
|
|
50
|
+
The main API for testing plugins.
|
|
51
|
+
|
|
52
|
+
#### `PluginTester.fullTest(pluginPath, configs, options?)`
|
|
53
|
+
|
|
54
|
+
Runs a complete plugin lifecycle test including validate, plan, apply, import, modify (optional), and destroy.
|
|
55
|
+
|
|
56
|
+
**Parameters:**
|
|
57
|
+
- `pluginPath` (string): Absolute path to the plugin entry point
|
|
58
|
+
- `configs` (ResourceConfig[]): Array of resource configurations to test
|
|
59
|
+
- `options` (object, optional):
|
|
60
|
+
- `skipUninstall` (boolean): Skip the destroy phase
|
|
61
|
+
- `skipImport` (boolean): Skip the import phase
|
|
62
|
+
- `validatePlan` (function): Callback to validate plan results
|
|
63
|
+
- `validateApply` (function): Callback to validate apply results
|
|
64
|
+
- `validateDestroy` (function): Callback to validate destroy results
|
|
65
|
+
- `validateImport` (function): Callback to validate import results
|
|
66
|
+
- `testModify` (object): Configuration for testing modifications
|
|
67
|
+
- `modifiedConfigs` (ResourceConfig[]): Modified configurations
|
|
68
|
+
- `validateModify` (function): Callback to validate modify results
|
|
69
|
+
|
|
70
|
+
**Example:**
|
|
71
|
+
```typescript
|
|
72
|
+
await PluginTester.fullTest('/path/to/plugin', configs, {
|
|
73
|
+
validatePlan: (plans) => {
|
|
74
|
+
expect(plans).toHaveLength(1);
|
|
75
|
+
expect(plans[0].operation).toBe('create');
|
|
76
|
+
},
|
|
77
|
+
testModify: {
|
|
78
|
+
modifiedConfigs: [
|
|
79
|
+
{ type: 'my-resource', newParam: 'updated-value' }
|
|
80
|
+
],
|
|
81
|
+
validateModify: (plans) => {
|
|
82
|
+
expect(plans[0].operation).toBe('modify');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### `PluginTester.install(pluginPath, configs)`
|
|
89
|
+
|
|
90
|
+
Tests only the installation flow (validate → plan → apply).
|
|
91
|
+
|
|
92
|
+
**Parameters:**
|
|
93
|
+
- `pluginPath` (string): Absolute path to the plugin entry point
|
|
94
|
+
- `configs` (ResourceConfig[]): Array of resource configurations to install
|
|
95
|
+
|
|
96
|
+
**Example:**
|
|
97
|
+
```typescript
|
|
98
|
+
await PluginTester.install('/path/to/plugin', [
|
|
99
|
+
{ type: 'homebrew', os: [ResourceOs.MACOS] }
|
|
100
|
+
]);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### `PluginTester.uninstall(pluginPath, configs, options?)`
|
|
104
|
+
|
|
105
|
+
Tests the destroy operation for resources.
|
|
106
|
+
|
|
107
|
+
**Parameters:**
|
|
108
|
+
- `pluginPath` (string): Absolute path to the plugin entry point
|
|
109
|
+
- `configs` (ResourceConfig[]): Array of resource configurations to destroy
|
|
110
|
+
- `options` (object, optional):
|
|
111
|
+
- `validateDestroy` (function): Callback to validate destroy results
|
|
112
|
+
|
|
113
|
+
**Example:**
|
|
114
|
+
```typescript
|
|
115
|
+
await PluginTester.uninstall('/path/to/plugin', configs, {
|
|
116
|
+
validateDestroy: (plans) => {
|
|
117
|
+
expect(plans[0].operation).toBe('destroy');
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### PluginProcess
|
|
123
|
+
|
|
124
|
+
Low-level API for managing individual plugin processes.
|
|
125
|
+
|
|
126
|
+
**Example:**
|
|
127
|
+
```typescript
|
|
128
|
+
import { PluginProcess } from '@codifycli/plugin-test';
|
|
129
|
+
|
|
130
|
+
const plugin = new PluginProcess('/path/to/plugin');
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
// Initialize the plugin
|
|
134
|
+
const initResult = await plugin.initialize();
|
|
135
|
+
|
|
136
|
+
// Validate configs
|
|
137
|
+
const validateResult = await plugin.validate({
|
|
138
|
+
configs: [{ core: { type: 'my-resource' }, parameters: {} }]
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Create a plan
|
|
142
|
+
const plan = await plugin.plan({
|
|
143
|
+
core: { type: 'my-resource' },
|
|
144
|
+
desired: { param: 'value' },
|
|
145
|
+
isStateful: false,
|
|
146
|
+
state: undefined
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// Apply the plan
|
|
150
|
+
await plugin.apply({ planId: plan.planId });
|
|
151
|
+
} finally {
|
|
152
|
+
plugin.kill();
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### TestUtils
|
|
157
|
+
|
|
158
|
+
Utility functions for common testing scenarios.
|
|
159
|
+
|
|
160
|
+
#### `TestUtils.sendMessageAndAwaitResponse(process, message)`
|
|
161
|
+
|
|
162
|
+
Sends an IPC message to a plugin process and waits for the response.
|
|
163
|
+
|
|
164
|
+
#### Shell Utilities
|
|
165
|
+
|
|
166
|
+
- `TestUtils.getShell()`: Returns the current shell ('bash' or 'zsh')
|
|
167
|
+
- `TestUtils.getPrimaryShellRc()`: Returns the path to the primary shell RC file
|
|
168
|
+
- `TestUtils.getSourceCommand()`: Returns the source command for the shell RC file
|
|
169
|
+
- `TestUtils.getShellCommand(command)`: Returns a command that sources the shell RC file first
|
|
170
|
+
- `TestUtils.getInteractiveCommand(command)`: Returns an interactive shell command
|
|
171
|
+
|
|
172
|
+
#### Platform Utilities
|
|
173
|
+
|
|
174
|
+
- `TestUtils.isMacOS()`: Returns true if running on macOS
|
|
175
|
+
- `TestUtils.isLinux()`: Returns true if running on Linux
|
|
176
|
+
|
|
177
|
+
#### Prerequisite Utilities
|
|
178
|
+
|
|
179
|
+
- `TestUtils.ensureHomebrewInstalledOnMacOs(pluginPath)`: Ensures Homebrew is installed
|
|
180
|
+
- `TestUtils.ensureXcodeInstalledOnMacOs(pluginPath)`: Ensures Xcode tools are installed
|
|
181
|
+
|
|
182
|
+
### Spawn Utilities
|
|
183
|
+
|
|
184
|
+
#### `testSpawn(command, options?)`
|
|
185
|
+
|
|
186
|
+
Execute a command interactively in a PTY (for testing).
|
|
187
|
+
|
|
188
|
+
**Parameters:**
|
|
189
|
+
- `command` (string): Command to execute
|
|
190
|
+
- `options` (object, optional):
|
|
191
|
+
- `cwd` (string): Working directory
|
|
192
|
+
- `env` (object): Environment variables
|
|
193
|
+
- `interactive` (boolean): Run in interactive mode (default: true)
|
|
194
|
+
- `requiresRoot` (boolean): Run with sudo
|
|
195
|
+
- `stdin` (boolean): Enable stdin passthrough
|
|
196
|
+
- `throws` (boolean): Throw on non-zero exit code
|
|
197
|
+
|
|
198
|
+
**Example:**
|
|
199
|
+
```typescript
|
|
200
|
+
import { testSpawn } from '@codifycli/plugin-test';
|
|
201
|
+
|
|
202
|
+
const result = await testSpawn('which brew');
|
|
203
|
+
if (result.status === SpawnStatus.SUCCESS) {
|
|
204
|
+
console.log('Homebrew is installed at:', result.data);
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Testing Patterns
|
|
209
|
+
|
|
210
|
+
### OS-Specific Tests
|
|
211
|
+
|
|
212
|
+
Tests automatically filter by OS using the `os` field:
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
const configs = [
|
|
216
|
+
{ type: 'homebrew', os: [ResourceOs.MACOS] }, // Only runs on macOS
|
|
217
|
+
{ type: 'apt', os: [ResourceOs.LINUX] }, // Only runs on Linux
|
|
218
|
+
{ type: 'git' } // Runs on all platforms
|
|
219
|
+
];
|
|
220
|
+
|
|
221
|
+
await PluginTester.fullTest('/path/to/plugin', configs);
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Testing Modifications
|
|
225
|
+
|
|
226
|
+
Test that your plugin properly handles resource modifications:
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
await PluginTester.fullTest('/path/to/plugin', [
|
|
230
|
+
{ type: 'my-resource', version: '1.0.0' }
|
|
231
|
+
], {
|
|
232
|
+
testModify: {
|
|
233
|
+
modifiedConfigs: [
|
|
234
|
+
{ type: 'my-resource', version: '2.0.0' }
|
|
235
|
+
],
|
|
236
|
+
validateModify: (plans) => {
|
|
237
|
+
expect(plans[0].operation).toBe('modify');
|
|
238
|
+
expect(plans[0].changes).toContain('version');
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Testing Multiple Resources
|
|
245
|
+
|
|
246
|
+
Test multiple resources in a single test run:
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
const configs = [
|
|
250
|
+
{ type: 'homebrew', name: 'homebrew-main' },
|
|
251
|
+
{ type: 'homebrew-package', name: 'wget', formula: 'wget' },
|
|
252
|
+
{ type: 'homebrew-package', name: 'curl', formula: 'curl' }
|
|
253
|
+
];
|
|
254
|
+
|
|
255
|
+
await PluginTester.fullTest('/path/to/plugin', configs, {
|
|
256
|
+
validateApply: (plans) => {
|
|
257
|
+
expect(plans).toHaveLength(3);
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Prerequisite Installation
|
|
263
|
+
|
|
264
|
+
Ensure prerequisites are installed before running tests:
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
import { TestUtils, PluginTester } from '@codifycli/plugin-test';
|
|
268
|
+
|
|
269
|
+
// Ensure Homebrew is installed before testing Homebrew packages
|
|
270
|
+
await TestUtils.ensureHomebrewInstalledOnMacOs('/path/to/core-plugin');
|
|
271
|
+
|
|
272
|
+
await PluginTester.fullTest('/path/to/plugin', [
|
|
273
|
+
{ type: 'homebrew-package', formula: 'wget' }
|
|
274
|
+
]);
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Environment Variables
|
|
278
|
+
|
|
279
|
+
- `DEBUG`: Enable plugin debug mode with breakpoint at `--inspect-brk=9221`
|
|
280
|
+
- `VITE_CODIFY_TEST_JWT`: JWT token for tests that require Codify credentials
|
|
281
|
+
|
|
282
|
+
## Development
|
|
283
|
+
|
|
284
|
+
### Running Tests
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
npm test
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Running a Single Test
|
|
291
|
+
|
|
292
|
+
```bash
|
|
293
|
+
npx vitest src/test-utils.test.ts
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Building
|
|
297
|
+
|
|
298
|
+
```bash
|
|
299
|
+
npm run prepublishOnly
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## How It Works
|
|
303
|
+
|
|
304
|
+
The testing framework spawns your plugin as a child process (using Node's `fork()`) and communicates with it via IPC messages, exactly how the Codify CLI does. This ensures your tests accurately reflect real-world plugin behavior.
|
|
305
|
+
|
|
306
|
+
When your plugin requests elevated permissions or needs to execute commands, the framework intercepts these requests and handles them automatically, allowing you to test plugin behavior in a controlled environment.
|
|
307
|
+
|
|
308
|
+
## Requirements
|
|
309
|
+
|
|
310
|
+
- Node.js >= 18.0.0
|
|
311
|
+
- Works on macOS, Linux, and Windows
|
|
312
|
+
|
|
313
|
+
## License
|
|
314
|
+
|
|
315
|
+
ISC
|
package/codify.json
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { ApplyRequestData, ImportRequestData, ImportResponseData, InitializeResponseData, PlanRequestData, PlanResponseData, ValidateRequestData, ValidateResponseData } from '@codifycli/schemas';
|
|
3
|
+
import { ChildProcess } from 'node:child_process';
|
|
4
|
+
export declare class PluginProcess {
|
|
5
|
+
childProcess: ChildProcess;
|
|
6
|
+
/**
|
|
7
|
+
* PluginTester is a helper class to integration test plugins. It launches plugins via fork() just like CodifyCLI does.
|
|
8
|
+
*
|
|
9
|
+
* @param pluginPath A fully qualified path
|
|
10
|
+
*/
|
|
11
|
+
constructor(pluginPath: string);
|
|
12
|
+
initialize(): Promise<InitializeResponseData>;
|
|
13
|
+
validate(data: ValidateRequestData): Promise<ValidateResponseData>;
|
|
14
|
+
plan(data: PlanRequestData): Promise<PlanResponseData>;
|
|
15
|
+
apply(data: ApplyRequestData): Promise<void>;
|
|
16
|
+
import(data: ImportRequestData): Promise<ImportResponseData>;
|
|
17
|
+
kill(): void;
|
|
18
|
+
private handleIncomingRequests;
|
|
19
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { CommandRequestDataSchema, IpcMessageSchema, MessageCmd } from '@codifycli/schemas';
|
|
2
|
+
import Ajv from 'ajv';
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
4
|
+
import { fork } from 'node:child_process';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { spawnSafe } from './spawn.js';
|
|
7
|
+
import { TestUtils } from './test-utils.js';
|
|
8
|
+
const ajv = new Ajv.default({
|
|
9
|
+
strict: true
|
|
10
|
+
});
|
|
11
|
+
const ipcMessageValidator = ajv.compile(IpcMessageSchema);
|
|
12
|
+
const commandRequestValidator = ajv.compile(CommandRequestDataSchema);
|
|
13
|
+
export class PluginProcess {
|
|
14
|
+
childProcess;
|
|
15
|
+
/**
|
|
16
|
+
* PluginTester is a helper class to integration test plugins. It launches plugins via fork() just like CodifyCLI does.
|
|
17
|
+
*
|
|
18
|
+
* @param pluginPath A fully qualified path
|
|
19
|
+
*/
|
|
20
|
+
constructor(pluginPath) {
|
|
21
|
+
if (!path.isAbsolute(pluginPath)) {
|
|
22
|
+
throw new Error('A fully qualified path must be supplied to PluginTester');
|
|
23
|
+
}
|
|
24
|
+
const debug = (process.env.DEBUG) ? ['--inspect-brk=9221'] : [];
|
|
25
|
+
this.childProcess = fork(pluginPath, [], {
|
|
26
|
+
// Use default true to test plugins in secure mode (un-able to request sudo directly)
|
|
27
|
+
// detached: true,
|
|
28
|
+
env: { ...process.env },
|
|
29
|
+
execArgv: ['--import', 'tsx/esm', ...debug],
|
|
30
|
+
stdio: 'pipe',
|
|
31
|
+
});
|
|
32
|
+
this.childProcess.stderr?.pipe(process.stderr);
|
|
33
|
+
this.childProcess.stdout?.pipe(process.stdout);
|
|
34
|
+
this.handleIncomingRequests(this.childProcess);
|
|
35
|
+
}
|
|
36
|
+
async initialize() {
|
|
37
|
+
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
|
|
38
|
+
cmd: 'initialize',
|
|
39
|
+
data: { verbosityLevel: 3 },
|
|
40
|
+
requestId: nanoid(6),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async validate(data) {
|
|
44
|
+
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
|
|
45
|
+
cmd: 'validate',
|
|
46
|
+
data,
|
|
47
|
+
requestId: nanoid(6),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async plan(data) {
|
|
51
|
+
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
|
|
52
|
+
cmd: 'plan',
|
|
53
|
+
data,
|
|
54
|
+
requestId: nanoid(6),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async apply(data) {
|
|
58
|
+
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
|
|
59
|
+
cmd: 'apply',
|
|
60
|
+
data,
|
|
61
|
+
requestId: nanoid(6),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async import(data) {
|
|
65
|
+
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
|
|
66
|
+
cmd: 'import',
|
|
67
|
+
data,
|
|
68
|
+
requestId: nanoid(6),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
kill() {
|
|
72
|
+
this.childProcess.kill();
|
|
73
|
+
}
|
|
74
|
+
handleIncomingRequests(cp) {
|
|
75
|
+
// Listen for incoming sudo incoming sudo requests
|
|
76
|
+
cp.on('message', async (message) => {
|
|
77
|
+
if (!ipcMessageValidator(message)) {
|
|
78
|
+
throw new Error(`Invalid message from plugin. ${JSON.stringify(message, null, 2)}`);
|
|
79
|
+
}
|
|
80
|
+
if (message.cmd === MessageCmd.COMMAND_REQUEST) {
|
|
81
|
+
const { data, requestId } = message;
|
|
82
|
+
if (!commandRequestValidator(data)) {
|
|
83
|
+
throw new Error(`Invalid sudo request from plugin. ${JSON.stringify(commandRequestValidator.errors, null, 2)}`);
|
|
84
|
+
}
|
|
85
|
+
const { command, options } = data;
|
|
86
|
+
const result = await spawnSafe(command, options);
|
|
87
|
+
cp.send({
|
|
88
|
+
cmd: MessageCmd.COMMAND_REQUEST + '_Response',
|
|
89
|
+
data: result,
|
|
90
|
+
requestId,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (message.cmd === MessageCmd.PRESS_KEY_TO_CONTINUE_REQUEST) {
|
|
94
|
+
const { data, requestId } = message;
|
|
95
|
+
if (!commandRequestValidator(data)) {
|
|
96
|
+
throw new Error(`Invalid sudo request from plugin. ${JSON.stringify(commandRequestValidator.errors, null, 2)}`);
|
|
97
|
+
}
|
|
98
|
+
cp.send({
|
|
99
|
+
cmd: MessageCmd.PRESS_KEY_TO_CONTINUE_REQUEST + '_Response',
|
|
100
|
+
data: {},
|
|
101
|
+
requestId,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (message.cmd === MessageCmd.CODIFY_CREDENTIALS_REQUEST) {
|
|
105
|
+
const { requestId } = message;
|
|
106
|
+
const testJwt = process.env.VITE_CODIFY_TEST_JWT;
|
|
107
|
+
if (!testJwt) {
|
|
108
|
+
throw new Error('Unable to parse login credentials from VITE_CODIFY_TEST_JWT env var. Please set and try again');
|
|
109
|
+
}
|
|
110
|
+
cp.send({
|
|
111
|
+
cmd: MessageCmd.CODIFY_CREDENTIALS_REQUEST + '_Response',
|
|
112
|
+
data: testJwt,
|
|
113
|
+
requestId,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=plugin-process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin-process.js","sourceRoot":"","sources":["../src/plugin-process.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,wBAAwB,EAIxB,gBAAgB,EAEhB,UAAU,EAKX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAgB,IAAI,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC;IAC1B,MAAM,EAAE,IAAI;CACb,CAAC,CAAC;AAEH,MAAM,mBAAmB,GAAG,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAC1D,MAAM,uBAAuB,GAAG,GAAG,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC;AAEtE,MAAM,OAAO,aAAa;IACxB,YAAY,CAAc;IAE1B;;;;OAIG;IACH,YAAY,UAAkB;QAC5B,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;QAC7E,CAAC;QAED,MAAM,KAAK,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,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