|
10 | 10 | */ |
11 | 11 |
|
12 | 12 | import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 13 | +import { CLOUD_BINDING_SERVICE, type ICloudBindingService } from '@objectstack/spec/contracts'; |
13 | 14 | import { CloudConnectionPlugin, createCloudConnectionPlugin } from './cloud-connection-plugin.js'; |
14 | 15 | import { bindCatalogFromTables } from './__tests__/security-catalog.testkit.js'; |
15 | 16 |
|
@@ -268,3 +269,64 @@ describe('single-environment mode (host auth, fixed env id)', () => { |
268 | 269 | expect(res.payload.data.environmentId).toBe('env-from-env-var'); |
269 | 270 | }); |
270 | 271 | }); |
| 272 | + |
| 273 | +/** |
| 274 | + * [#30762] One reading of the control-plane URL. The routes read it through |
| 275 | + * the `cloud-binding` service the plugin registers, the same answer the push |
| 276 | + * relay sender reads, so the two cannot disagree on an option. An explicit |
| 277 | + * empty option is "cloud not configured" for both, and does not fall through |
| 278 | + * to `OS_CLOUD_URL` or the default. |
| 279 | + */ |
| 280 | +describe('CloudConnectionPlugin — the routes and the binding read the control-plane URL once', () => { |
| 281 | + const ENV = 'env-123'; |
| 282 | + const services = { |
| 283 | + 'env-registry': { resolveByHostname: async () => ({ environmentId: ENV }) }, |
| 284 | + 'kernel-manager': { getOrCreate: async () => ({ getServiceAsync: async () => sessionAuth('user-1') }) }, |
| 285 | + }; |
| 286 | + |
| 287 | + afterEach(() => { vi.unstubAllEnvs(); }); |
| 288 | + |
| 289 | + /** Boot the plugin, drive `GET /installed` once, and answer where it went and what the binding says. */ |
| 290 | + async function readBoth(controlPlaneUrl: string | undefined) { |
| 291 | + const rawApp = makeRawApp(); |
| 292 | + const { ctx, fireKernelReady } = makeCtx({ rawApp, services }); |
| 293 | + const registered = new Map<string, unknown>(); |
| 294 | + const fetchSpy = vi.fn(async (_url: string) => new Response(JSON.stringify({ success: true, data: { packages: [], total: 0 } }), { status: 200 })); |
| 295 | + vi.stubGlobal('fetch', fetchSpy); |
| 296 | + const plugin = new CloudConnectionPlugin({ controlPlaneApiKey: 'svc-key', ...(controlPlaneUrl === undefined ? {} : { controlPlaneUrl }) }); |
| 297 | + await plugin.init({ ...ctx, registerService: (name: string, svc: unknown) => registered.set(name, svc) } as any); |
| 298 | + await plugin.start(ctx as any); |
| 299 | + await fireKernelReady(); |
| 300 | + const res = await rawApp.routes.get('GET /api/v1/cloud-connection/installed')!( |
| 301 | + makeC('https://t1.example.com/api/v1/cloud-connection/installed'), |
| 302 | + ); |
| 303 | + const binding = registered.get(CLOUD_BINDING_SERVICE) as ICloudBindingService; |
| 304 | + const called = fetchSpy.mock.calls.map(([url]) => String(url)); |
| 305 | + return { res, binding: binding.controlPlaneUrl(), called }; |
| 306 | + } |
| 307 | + |
| 308 | + it.each(['', ' '])('an explicit empty option %j is "cloud not configured" for both, over OS_CLOUD_URL', async (option) => { |
| 309 | + vi.stubEnv('OS_CLOUD_URL', 'https://env.example.com'); |
| 310 | + const { res, binding, called } = await readBoth(option); |
| 311 | + expect(binding).toBe(''); |
| 312 | + expect(called).toEqual([]); |
| 313 | + expect(res.payload).toEqual({ success: true, data: { packages: [], total: 0, connected: false } }); |
| 314 | + }); |
| 315 | + |
| 316 | + it('a declined OS_CLOUD_URL under an absent option is "cloud not configured" for both', async () => { |
| 317 | + vi.stubEnv('OS_CLOUD_URL', 'off'); |
| 318 | + const { binding, called } = await readBoth(undefined); |
| 319 | + expect(binding).toBe(''); |
| 320 | + expect(called).toEqual([]); |
| 321 | + }); |
| 322 | + |
| 323 | + it.each([ |
| 324 | + ['the configured URL', 'https://cp.example.com/', 'https://env.example.com', 'https://cp.example.com'], |
| 325 | + ['OS_CLOUD_URL under an absent option', undefined, 'https://env.example.com/', 'https://env.example.com'], |
| 326 | + ])('%s: the routes call the URL the binding answers', async (_label, option, env, expected) => { |
| 327 | + vi.stubEnv('OS_CLOUD_URL', env); |
| 328 | + const { binding, called } = await readBoth(option); |
| 329 | + expect(binding).toBe(expected); |
| 330 | + expect(called).toEqual([`${expected}/api/v1/cloud/environments/${ENV}/packages`]); |
| 331 | + }); |
| 332 | +}); |
0 commit comments