|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as os from 'os'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Test the parseGoModPlugins function by importing the module's logic. |
| 8 | + * Since parseGoModPlugins is not exported, we test the regex pattern directly. |
| 9 | + */ |
| 10 | +suite('Plugin Discovery', () => { |
| 11 | + let tmpDir: string; |
| 12 | + |
| 13 | + setup(() => { |
| 14 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'workflow-pd-test-')); |
| 15 | + }); |
| 16 | + |
| 17 | + teardown(() => { |
| 18 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 19 | + }); |
| 20 | + |
| 21 | + test('go.mod plugin regex matches workflow-plugin-* dependencies', () => { |
| 22 | + const goMod = `module github.com/example/myapp |
| 23 | +
|
| 24 | +go 1.22 |
| 25 | +
|
| 26 | +require ( |
| 27 | + github.com/GoCodeAlone/workflow v0.3.30 |
| 28 | + github.com/GoCodeAlone/workflow-plugin-agent v0.3.1 |
| 29 | + github.com/GoCodeAlone/workflow-plugin-authz v0.2.0 |
| 30 | + github.com/GoCodeAlone/workflow-plugin-payments v0.1.0 |
| 31 | + github.com/GoCodeAlone/modular v1.12.0 |
| 32 | + github.com/some-other/module v1.0.0 |
| 33 | +)`; |
| 34 | + |
| 35 | + const plugins: string[] = []; |
| 36 | + for (const line of goMod.split('\n')) { |
| 37 | + const match = line.match(/github\.com\/GoCodeAlone\/(workflow-plugin-\S+)/); |
| 38 | + if (match) plugins.push(match[1]); |
| 39 | + } |
| 40 | + |
| 41 | + assert.deepStrictEqual(plugins, [ |
| 42 | + 'workflow-plugin-agent', |
| 43 | + 'workflow-plugin-authz', |
| 44 | + 'workflow-plugin-payments', |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + test('go.mod regex does not match non-plugin workflow modules', () => { |
| 49 | + const goMod = `require ( |
| 50 | + github.com/GoCodeAlone/workflow v0.3.30 |
| 51 | + github.com/GoCodeAlone/modular v1.12.0 |
| 52 | + github.com/GoCodeAlone/workflow-editor v0.1.0 |
| 53 | +)`; |
| 54 | + |
| 55 | + const plugins: string[] = []; |
| 56 | + for (const line of goMod.split('\n')) { |
| 57 | + const match = line.match(/github\.com\/GoCodeAlone\/(workflow-plugin-\S+)/); |
| 58 | + if (match) plugins.push(match[1]); |
| 59 | + } |
| 60 | + |
| 61 | + assert.deepStrictEqual(plugins, []); |
| 62 | + }); |
| 63 | + |
| 64 | + test('go.mod regex handles version suffixes correctly', () => { |
| 65 | + const goMod = `require github.com/GoCodeAlone/workflow-plugin-agent v0.3.1 // indirect`; |
| 66 | + |
| 67 | + const plugins: string[] = []; |
| 68 | + for (const line of goMod.split('\n')) { |
| 69 | + const match = line.match(/github\.com\/GoCodeAlone\/(workflow-plugin-\S+)/); |
| 70 | + if (match) plugins.push(match[1]); |
| 71 | + } |
| 72 | + |
| 73 | + // The regex captures "workflow-plugin-agent" and stops at whitespace |
| 74 | + assert.strictEqual(plugins.length, 1); |
| 75 | + assert.strictEqual(plugins[0], 'workflow-plugin-agent'); |
| 76 | + }); |
| 77 | + |
| 78 | + test('go.mod regex returns empty for empty content', () => { |
| 79 | + const plugins: string[] = []; |
| 80 | + for (const line of ''.split('\n')) { |
| 81 | + const match = line.match(/github\.com\/GoCodeAlone\/(workflow-plugin-\S+)/); |
| 82 | + if (match) plugins.push(match[1]); |
| 83 | + } |
| 84 | + assert.deepStrictEqual(plugins, []); |
| 85 | + }); |
| 86 | + |
| 87 | + test('cache directory creation works', () => { |
| 88 | + const cacheDir = path.join(tmpDir, 'plugin-manifests'); |
| 89 | + assert.ok(!fs.existsSync(cacheDir)); |
| 90 | + fs.mkdirSync(cacheDir, { recursive: true }); |
| 91 | + assert.ok(fs.existsSync(cacheDir)); |
| 92 | + }); |
| 93 | + |
| 94 | + test('cached manifest read/write round-trips', () => { |
| 95 | + const cacheDir = path.join(tmpDir, 'plugin-manifests'); |
| 96 | + fs.mkdirSync(cacheDir, { recursive: true }); |
| 97 | + |
| 98 | + const manifest = { |
| 99 | + name: 'workflow-plugin-agent', |
| 100 | + stepTypes: [{ name: 'step.agent_execute' }], |
| 101 | + moduleTypes: [{ name: 'agent.provider' }], |
| 102 | + }; |
| 103 | + |
| 104 | + const cacheFile = path.join(cacheDir, 'workflow-plugin-agent.json'); |
| 105 | + fs.writeFileSync(cacheFile, JSON.stringify(manifest)); |
| 106 | + |
| 107 | + const loaded = JSON.parse(fs.readFileSync(cacheFile, 'utf-8')); |
| 108 | + assert.deepStrictEqual(loaded, manifest); |
| 109 | + }); |
| 110 | +}); |
0 commit comments