|
| 1 | +import express from 'express'; |
| 2 | +import type { AddressInfo } from 'node:net'; |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { registerPluginRoutes } from '../src/routes/plugins/index.js'; |
| 6 | + |
| 7 | +const servers: Array<ReturnType<express.Express['listen']>> = []; |
| 8 | + |
| 9 | +afterEach(async () => { |
| 10 | + await Promise.all( |
| 11 | + servers.splice(0).map((server) => new Promise<void>((resolve, reject) => { |
| 12 | + server.close((error) => error ? reject(error) : resolve()); |
| 13 | + })), |
| 14 | + ); |
| 15 | +}); |
| 16 | + |
| 17 | +describe('Team plugin apply retraction gate', () => { |
| 18 | + it('does not apply a Team plugin retired while registry loading is pending', async () => { |
| 19 | + const app = express(); |
| 20 | + app.use(express.json()); |
| 21 | + let bindingLive = true; |
| 22 | + let finishRegistryLoad!: (value: Record<string, never>) => void; |
| 23 | + const registryGate = new Promise<Record<string, never>>((resolve) => { |
| 24 | + finishRegistryLoad = resolve; |
| 25 | + }); |
| 26 | + let registryLoadStarted!: () => void; |
| 27 | + const registryStarted = new Promise<void>((resolve) => { |
| 28 | + registryLoadStarted = resolve; |
| 29 | + }); |
| 30 | + const applyPlugin = vi.fn(() => ({ |
| 31 | + result: { capabilitiesGranted: [], appliedPlugin: { capabilitiesGranted: [] } }, |
| 32 | + warnings: [], |
| 33 | + })); |
| 34 | + const middleware: express.RequestHandler = (_req, _res, next) => next(); |
| 35 | + |
| 36 | + registerPluginRoutes(app, { |
| 37 | + db: { |
| 38 | + prepare: () => ({ all: () => [], get: () => null, run: () => undefined }), |
| 39 | + transaction: (run: () => unknown) => () => run(), |
| 40 | + }, |
| 41 | + paths: { PROJECTS_DIR: '', PLUGIN_REGISTRY_ROOTS: [], PLUGIN_LOCKFILE_PATH: '' }, |
| 42 | + ids: { randomId: () => 'unused' }, |
| 43 | + projectStore: {}, |
| 44 | + conversations: {}, |
| 45 | + verifyWorkspaceRequestAuthority: async () => ({ |
| 46 | + ok: true, |
| 47 | + context: { workspaceId: 'ws-team' }, |
| 48 | + }), |
| 49 | + workspaceResources: { |
| 50 | + getWorkspaceResource: () => null, |
| 51 | + getWorkspaceResourceByResourceId: () => null, |
| 52 | + workspaceTeamPluginBindingAllowsRead: () => bindingLive, |
| 53 | + }, |
| 54 | + plugins: { |
| 55 | + getInstalledPlugin: () => null, |
| 56 | + getWorkspacePlugin: async () => ({ |
| 57 | + id: 'team-plugin', |
| 58 | + source: 'team:plugin:ws-team:team-plugin', |
| 59 | + }), |
| 60 | + listInstalledPlugins: () => [], |
| 61 | + applyPlugin, |
| 62 | + MissingInputError: class MissingInputError extends Error { |
| 63 | + fields: string[] = []; |
| 64 | + }, |
| 65 | + }, |
| 66 | + helpers: { |
| 67 | + requireLocalDaemonRequest: middleware, |
| 68 | + pluginUpload: { |
| 69 | + single: () => middleware, |
| 70 | + array: () => middleware, |
| 71 | + }, |
| 72 | + loadPluginRegistryView: async () => { |
| 73 | + registryLoadStarted(); |
| 74 | + return registryGate; |
| 75 | + }, |
| 76 | + buildConnectorProbe: () => ({}), |
| 77 | + connectorService: {}, |
| 78 | + sendApiError: (res: express.Response, status: number, code: string, message: string) => |
| 79 | + res.status(status).json({ error: { code, message } }), |
| 80 | + }, |
| 81 | + } as unknown as Parameters<typeof registerPluginRoutes>[1]); |
| 82 | + |
| 83 | + const server = app.listen(0, '127.0.0.1'); |
| 84 | + servers.push(server); |
| 85 | + await new Promise<void>((resolve) => server.once('listening', resolve)); |
| 86 | + const { port } = server.address() as AddressInfo; |
| 87 | + const responsePromise = fetch(`http://127.0.0.1:${port}/api/plugins/team-plugin/apply`, { |
| 88 | + method: 'POST', |
| 89 | + headers: { |
| 90 | + 'content-type': 'application/json', |
| 91 | + 'x-od-workspace-id': 'ws-team', |
| 92 | + 'x-od-workspace-type': 'team', |
| 93 | + 'x-od-workspace-member-id': 'member-team', |
| 94 | + 'x-od-workspace-role': 'member', |
| 95 | + 'x-od-workspace-lifecycle-state': 'active', |
| 96 | + 'x-od-workspace-member-status': 'active', |
| 97 | + }, |
| 98 | + body: '{}', |
| 99 | + }); |
| 100 | + await registryStarted; |
| 101 | + bindingLive = false; |
| 102 | + finishRegistryLoad({}); |
| 103 | + |
| 104 | + const response = await responsePromise; |
| 105 | + expect(response.status).toBe(404); |
| 106 | + expect(applyPlugin).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | +}); |
0 commit comments