|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #25294 — `isCloudManaged()`, the one reader of `OS_CLOUD_MANAGED`, and its |
| 5 | + * production fuse (ruling 6341690661 on #25083, Q2 → D). Pin 9 as the ruling |
| 6 | + * reads it, one case each: a `--dev` boot with the variable runs managed; a |
| 7 | + * production boot with the variable and a bound control plane runs managed; a |
| 8 | + * production boot with the variable and no control plane refuses loudly; |
| 9 | + * without the variable nothing is managed. Then the fuse at both kernels' |
| 10 | + * `bootstrap()`, before any plugin initialises. The environment is driven |
| 11 | + * through `process.env` and restored; the memo is reset around every case. |
| 12 | + */ |
| 13 | + |
| 14 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 15 | +import { ObjectKernel } from './kernel.js'; |
| 16 | +import { LiteKernel } from './lite-kernel.js'; |
| 17 | +import { isCloudManaged, resetCloudManagedForTesting, resolveControlPlaneApiKey } from './stack-auth.js'; |
| 18 | +import type { Plugin } from './types.js'; |
| 19 | + |
| 20 | +const ENV_VARS = ['OS_CLOUD_MANAGED', 'OS_CLOUD_API_KEY', 'NODE_ENV'] as const; |
| 21 | +const saved = Object.fromEntries(ENV_VARS.map((k) => [k, process.env[k]])); |
| 22 | +beforeEach(() => { |
| 23 | + for (const k of ENV_VARS) delete process.env[k]; |
| 24 | + resetCloudManagedForTesting(); |
| 25 | +}); |
| 26 | +afterEach(() => { |
| 27 | + for (const k of ENV_VARS) { |
| 28 | + if (saved[k] === undefined) delete process.env[k]; |
| 29 | + else process.env[k] = saved[k]; |
| 30 | + } |
| 31 | + resetCloudManagedForTesting(); |
| 32 | +}); |
| 33 | + |
| 34 | +/** A production boot: `NODE_ENV` as `os serve` defaults it without `--dev`. */ |
| 35 | +const production = () => { process.env.NODE_ENV = 'production'; }; |
| 36 | + |
| 37 | +describe('isCloudManaged — pin 9, as ruling 6341690661 (Q2 → D) reads it', () => { |
| 38 | + it('a `--dev` boot with the variable runs managed, with no control plane bound', () => { |
| 39 | + process.env.NODE_ENV = 'development'; // what `os serve --dev` sets |
| 40 | + process.env.OS_CLOUD_MANAGED = '1'; |
| 41 | + expect(isCloudManaged()).toBe(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('a production boot with the variable and a bound control plane runs managed', () => { |
| 45 | + production(); |
| 46 | + process.env.OS_CLOUD_MANAGED = '1'; |
| 47 | + process.env.OS_CLOUD_API_KEY = 'svc-key'; |
| 48 | + expect(isCloudManaged()).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it('a production boot with the variable and no control plane refuses loudly, naming the variable and the binding', () => { |
| 52 | + production(); |
| 53 | + process.env.OS_CLOUD_MANAGED = '1'; |
| 54 | + expect(() => isCloudManaged()).toThrow(/^\[Kernel\] Refusing to start: OS_CLOUD_MANAGED=1 is set on a production boot with no bound control plane \(OS_CLOUD_API_KEY is unset or empty\)/); |
| 55 | + }); |
| 56 | + |
| 57 | + it('without the variable nothing is managed, on any boot', () => { |
| 58 | + production(); |
| 59 | + expect(isCloudManaged()).toBe(false); |
| 60 | + resetCloudManagedForTesting(); |
| 61 | + process.env.OS_CLOUD_API_KEY = 'svc-key'; |
| 62 | + expect(isCloudManaged()).toBe(false); |
| 63 | + resetCloudManagedForTesting(); |
| 64 | + process.env.NODE_ENV = 'development'; |
| 65 | + expect(isCloudManaged()).toBe(false); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('isCloudManaged — the edges of the switch and of "production"', () => { |
| 70 | + it('NODE_ENV=test is not production: the variable alone runs managed (the services half sets it under vitest)', () => { |
| 71 | + process.env.NODE_ENV = 'test'; |
| 72 | + process.env.OS_CLOUD_MANAGED = '1'; |
| 73 | + expect(isCloudManaged()).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('an unset NODE_ENV is production (`os serve` defaults it so), and a blank key is no binding', () => { |
| 77 | + process.env.OS_CLOUD_MANAGED = '1'; |
| 78 | + process.env.OS_CLOUD_API_KEY = ' '; |
| 79 | + expect(() => isCloudManaged()).toThrow(/no bound control plane/); |
| 80 | + }); |
| 81 | + |
| 82 | + it('an empty variable is off; any value but `1` is refused on every boot, naming the value', () => { |
| 83 | + process.env.NODE_ENV = 'development'; |
| 84 | + process.env.OS_CLOUD_MANAGED = ''; |
| 85 | + expect(isCloudManaged()).toBe(false); |
| 86 | + for (const value of ['true', '0', 'yes']) { |
| 87 | + resetCloudManagedForTesting(); |
| 88 | + process.env.OS_CLOUD_MANAGED = value; |
| 89 | + expect(() => isCloudManaged()).toThrow(`OS_CLOUD_MANAGED is set to ${JSON.stringify(value)}`); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it('memoized for the process, the refusal included, until reset', () => { |
| 94 | + process.env.NODE_ENV = 'test'; |
| 95 | + expect(isCloudManaged()).toBe(false); |
| 96 | + process.env.OS_CLOUD_MANAGED = '1'; |
| 97 | + expect(isCloudManaged()).toBe(false); |
| 98 | + resetCloudManagedForTesting(); |
| 99 | + expect(isCloudManaged()).toBe(true); |
| 100 | + |
| 101 | + resetCloudManagedForTesting(); |
| 102 | + production(); |
| 103 | + expect(() => isCloudManaged()).toThrow(/no bound control plane/); |
| 104 | + process.env.OS_CLOUD_API_KEY = 'svc-key'; |
| 105 | + expect(() => isCloudManaged()).toThrow(/no bound control plane/); |
| 106 | + resetCloudManagedForTesting(); |
| 107 | + expect(isCloudManaged()).toBe(true); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe('resolveControlPlaneApiKey — the one derivation of "a control plane is bound"', () => { |
| 112 | + it('the configured value wins, even empty; else OS_CLOUD_API_KEY; trimmed; `` when neither', () => { |
| 113 | + expect(resolveControlPlaneApiKey()).toBe(''); |
| 114 | + process.env.OS_CLOUD_API_KEY = ' env-key '; |
| 115 | + expect(resolveControlPlaneApiKey()).toBe('env-key'); |
| 116 | + expect(resolveControlPlaneApiKey(' cfg-key ')).toBe('cfg-key'); |
| 117 | + expect(resolveControlPlaneApiKey('')).toBe(''); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('the fuse at kernel boot — refused before any plugin initialises', () => { |
| 122 | + const probe = () => { |
| 123 | + const calls: string[] = []; |
| 124 | + const plugin: Plugin = { |
| 125 | + name: 'test.cloud-managed-probe', |
| 126 | + init: async () => { calls.push('init'); }, |
| 127 | + start: async () => { calls.push('start'); }, |
| 128 | + }; |
| 129 | + return { calls, plugin }; |
| 130 | + }; |
| 131 | + |
| 132 | + for (const [label, make] of [ |
| 133 | + ['ObjectKernel', () => new ObjectKernel({ logger: { level: 'error' }, gracefulShutdown: false, skipSystemValidation: true })], |
| 134 | + ['LiteKernel', () => new LiteKernel({ logger: { level: 'error' } })], |
| 135 | + ] as const) { |
| 136 | + it(`${label}: a production boot with the variable and no control plane refuses; with a binding it boots`, async () => { |
| 137 | + production(); |
| 138 | + process.env.OS_CLOUD_MANAGED = '1'; |
| 139 | + const refused = probe(); |
| 140 | + const kernel = make(); |
| 141 | + await kernel.use(refused.plugin); |
| 142 | + await expect(kernel.bootstrap()).rejects.toThrow(/no bound control plane/); |
| 143 | + expect(refused.calls).toEqual([]); |
| 144 | + |
| 145 | + resetCloudManagedForTesting(); |
| 146 | + process.env.OS_CLOUD_API_KEY = 'svc-key'; |
| 147 | + const booted = probe(); |
| 148 | + const second = make(); |
| 149 | + await second.use(booted.plugin); |
| 150 | + await second.bootstrap(); |
| 151 | + expect(booted.calls).toEqual(['init', 'start']); |
| 152 | + expect(isCloudManaged()).toBe(true); |
| 153 | + await second.shutdown(); |
| 154 | + }); |
| 155 | + } |
| 156 | +}); |
0 commit comments