|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// `objectstack serve` hands the deployment's product name to AuthPlugin, from |
| 4 | +// the SAME resolver the email capability's template context uses. |
| 5 | +// |
| 6 | +// The name reaches two auth surfaces through `AuthManager.getAppName()`: the |
| 7 | +// `{{appName}}` of every auth email, and the issuer and label prefix of the |
| 8 | +// otpauth URI a TOTP enrollment hands to an authenticator app. AuthPlugin used |
| 9 | +// to be constructed with no `appName`, so under `serve` `getAppName()` answered |
| 10 | +// 'ObjectStack' whatever `OS_APP_NAME` said. Auth emails hid it: their own |
| 11 | +// `appName` outranks the email service's template context, which DID carry |
| 12 | +// `OS_APP_NAME`, so an operator who set it saw the env var honoured in every |
| 13 | +// other mail and the framework's name in the auth ones. |
| 14 | +// |
| 15 | +// Two halves, both needed. The resolver half proves the chain is the email |
| 16 | +// chain, rung for rung, so the two consumers cannot disagree about the name. |
| 17 | +// The construction half scans `serve.ts` (comments masked, as its siblings |
| 18 | +// `serve-email-config-parity.contract.test.ts` and |
| 19 | +// `serve-verify-security-parity.contract.test.ts` do), because nothing else |
| 20 | +// sees the AuthPlugin literal: the plugin is imported through a variable |
| 21 | +// specifier, so the type checker cannot, and only a booted CLI could. |
| 22 | + |
| 23 | +import { describe, it, expect } from 'vitest'; |
| 24 | +import { readFileSync } from 'node:fs'; |
| 25 | +import path from 'node:path'; |
| 26 | +import { fileURLToPath } from 'node:url'; |
| 27 | +import { resolveDeploymentAppName, resolveEmailCapabilityArg } from './serve.js'; |
| 28 | +import { maskComments } from '../../../../scripts/js-comment-mask.mjs'; |
| 29 | + |
| 30 | +const SERVE_SOURCE = maskComments( |
| 31 | + readFileSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), 'serve.ts'), 'utf8'), |
| 32 | +); |
| 33 | + |
| 34 | +/** The text between the parenthesis or brace opening at `open` and its match. */ |
| 35 | +function balanced(source: string, open: number): string { |
| 36 | + const pairs: Record<string, string> = { '(': ')', '{': '}' }; |
| 37 | + const close = pairs[source[open]]; |
| 38 | + if (!close) throw new Error(`no bracket at offset ${open}`); |
| 39 | + let depth = 0; |
| 40 | + for (let i = open; i < source.length; i++) { |
| 41 | + if (source[i] === source[open]) depth++; |
| 42 | + else if (source[i] === close && --depth === 0) return source.slice(open + 1, i); |
| 43 | + } |
| 44 | + throw new Error(`unbalanced bracket opened at offset ${open}`); |
| 45 | +} |
| 46 | + |
| 47 | +/** Every `<callee>(` call's argument text, declarations excluded. */ |
| 48 | +function callArguments(source: string, callee: string): string[] { |
| 49 | + const out: string[] = []; |
| 50 | + const needle = `${callee}(`; |
| 51 | + for (let i = source.indexOf(needle); i !== -1; i = source.indexOf(needle, i + 1)) { |
| 52 | + if (/function\s+$/.test(source.slice(Math.max(0, i - 20), i))) continue; |
| 53 | + out.push(balanced(source, i + callee.length)); |
| 54 | + } |
| 55 | + return out; |
| 56 | +} |
| 57 | + |
| 58 | +/** Argument text compared as code: whitespace and a trailing comma dropped. */ |
| 59 | +const normalise = (args: string) => args.replace(/\s+/g, '').replace(/,$/, ''); |
| 60 | + |
| 61 | +const emailAppName = (cfgEmail: Record<string, any>, env: NodeJS.ProcessEnv, configAppName?: string) => |
| 62 | + (resolveEmailCapabilityArg(cfgEmail, env, configAppName).options |
| 63 | + .defaultTemplateContext as Record<string, unknown>).appName; |
| 64 | + |
| 65 | +describe('resolveDeploymentAppName is the email chain, rung for rung', () => { |
| 66 | + const RUNGS: Array<[string, Record<string, any>, NodeJS.ProcessEnv, string | undefined, string]> = [ |
| 67 | + ['OS_APP_NAME', { appName: 'Key', defaultTemplateContext: { appName: 'Context' } }, { OS_APP_NAME: 'Env' }, 'Top', 'Env'], |
| 68 | + ['config.email.appName', { appName: 'Key', defaultTemplateContext: { appName: 'Context' } }, {}, 'Top', 'Key'], |
| 69 | + ['defaultTemplateContext.appName', { defaultTemplateContext: { appName: 'Context' } }, {}, 'Top', 'Context'], |
| 70 | + ['top-level config.appName', {}, {}, 'Top', 'Top'], |
| 71 | + ["'ObjectStack'", {}, {}, undefined, 'ObjectStack'], |
| 72 | + ]; |
| 73 | + |
| 74 | + for (const [rung, cfgEmail, env, top, expected] of RUNGS) { |
| 75 | + it(`${rung} decides both consumers`, () => { |
| 76 | + expect(resolveDeploymentAppName(cfgEmail, env, top)).toBe(expected); |
| 77 | + expect(emailAppName(cfgEmail, env, top)).toBe(expected); |
| 78 | + }); |
| 79 | + } |
| 80 | +}); |
| 81 | + |
| 82 | +describe('serve constructs AuthPlugin with the deployment app name', () => { |
| 83 | + it('passes appName from resolveDeploymentAppName, with the arguments the email capability gets', () => { |
| 84 | + const constructions = callArguments(SERVE_SOURCE, 'new AuthPlugin'); |
| 85 | + // Absence must be loud: a scan that finds no construction proves nothing. |
| 86 | + expect(constructions.length, 'serve.ts must construct AuthPlugin exactly once').toBe(1); |
| 87 | + |
| 88 | + const literal = constructions[0]; |
| 89 | + const passed = callArguments(literal, 'resolveDeploymentAppName'); |
| 90 | + expect(passed.length, 'AuthPlugin must receive appName from resolveDeploymentAppName').toBe(1); |
| 91 | + expect(literal).toMatch(/\bappName:\s*resolveDeploymentAppName\(/); |
| 92 | + |
| 93 | + const emailCalls = callArguments(SERVE_SOURCE, 'resolveEmailCapabilityArg'); |
| 94 | + expect(emailCalls.length, 'serve.ts must resolve the email capability exactly once').toBe(1); |
| 95 | + expect(normalise(passed[0])).toBe(normalise(emailCalls[0])); |
| 96 | + }); |
| 97 | +}); |
0 commit comments