Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(test): r4-h2 resolve upload authorities structurally
Josuéac9af8ad708a
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex d6782a6cb..e0697b7ef 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.488",+ "version": "0.1.489","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.488",+ "version": "0.1.489","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/image-policy": "file:../packages/image-policy",diff --git a/control-api/package.json b/control-api/package.jsonindex 895f64295..ed0cabcc0 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.488",+ "version": "0.1.489","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/test/helpers/staticSourceAuthority.ts b/control-api/test/helpers/staticSourceAuthority.tsnew file mode 100644index 000000000..70b5a7d93--- /dev/null+++ b/control-api/test/helpers/staticSourceAuthority.ts@@ -0,0 +1,162 @@+import ts from 'typescript'++export type BoundedEnvBytes = { fallback: number; ceiling: number }++function parseTypeScript(source: string, label: string): ts.SourceFile {+ const file = ts.createSourceFile(label, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)+ const diagnostics = (file as ts.SourceFile & { parseDiagnostics: readonly ts.Diagnostic[] })+ .parseDiagnostics+ if (diagnostics.length > 0) throw new Error(`${label} contains malformed TypeScript`)+ return file+}++function evaluateStaticByteExpression(+ expression: ts.Expression,+ sourceFile: ts.SourceFile,+ label: string+): number {+ if (ts.isParenthesizedExpression(expression)) {+ return evaluateStaticByteExpression(expression.expression, sourceFile, label)+ }+ if (ts.isNumericLiteral(expression)) {+ const literal = expression.getText(sourceFile)+ if (!/^(?:0|[1-9][0-9]*(?:_[0-9]+)*)$/.test(literal)) {+ throw new Error(`${label} must use a canonical decimal integer literal`)+ }+ const value = Number(literal.replaceAll('_', ''))+ if (!Number.isSafeInteger(value)) throw new Error(`${label} must be a safe integer`)+ return value+ }+ if (+ ts.isBinaryExpression(expression) &&+ expression.operatorToken.kind === ts.SyntaxKind.AsteriskToken+ ) {+ const left = evaluateStaticByteExpression(expression.left, sourceFile, label)+ const right = evaluateStaticByteExpression(expression.right, sourceFile, label)+ const value = left * right+ if (!Number.isSafeInteger(value)) throw new Error(`${label} must be a safe integer`)+ return value+ }+ throw new Error(`${label} must use only decimal integers and multiplication`)+}++function hasModifier(node: ts.Node, kind: ts.SyntaxKind): boolean {+ return (+ ts.canHaveModifiers(node) &&+ (ts.getModifiers(node)?.some(modifier => modifier.kind === kind) ?? false)+ )+}++function directExportedConstInitializer(+ sourceFile: ts.SourceFile,+ symbol: string,+ label: string+): ts.Expression {+ const declarations: ts.VariableDeclaration[] = []+ let indirectExport = false++ const visit = (node: ts.Node): void => {+ if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === symbol) {+ declarations.push(node)+ }+ if (ts.isExportDeclaration(node)) {+ if (!node.exportClause || ts.isNamespaceExport(node.exportClause)) {+ indirectExport = true+ } else {+ for (const element of node.exportClause.elements) {+ if (element.name.text === symbol) indirectExport = true+ }+ }+ }+ ts.forEachChild(node, visit)+ }+ visit(sourceFile)++ if (indirectExport || declarations.length !== 1) {+ throw new Error(`${symbol} must have exactly one unambiguous direct export in ${label}`)+ }+ const declaration = declarations[0]+ const declarationList = declaration.parent+ const statement = declarationList.parent+ if (+ !ts.isVariableDeclarationList(declarationList) ||+ (declarationList.flags & ts.NodeFlags.Const) === 0 ||+ !ts.isVariableStatement(statement) ||+ !hasModifier(statement, ts.SyntaxKind.ExportKeyword) ||+ !declaration.initializer+ ) {+ throw new Error(`${symbol} must be a direct exported const initializer in ${label}`)+ }+ return declaration.initializer+}++export function staticExportedBytesFromSource(+ source: string,+ symbol: string,+ label = symbol+): number {+ const sourceFile = parseTypeScript(source, label)+ const initializer = directExportedConstInitializer(sourceFile, symbol, label)+ return evaluateStaticByteExpression(initializer, sourceFile, `${label}:${symbol}`)+}++function staticStringExpression(expression: ts.Expression): string | null {+ if (ts.isStringLiteral(expression)) return expression.text+ if (ts.isParenthesizedExpression(expression)) return staticStringExpression(expression.expression)+ return null+}++function propertyName(name: ts.PropertyName): string | null {+ if (ts.isIdentifier(name) || ts.isStringLiteral(name)) return name.text+ if (ts.isComputedPropertyName(name)) return staticStringExpression(name.expression)+ return null+}++export function boundedEnvBytesFromSource(+ source: string,+ objectSymbol: string,+ property: string,+ envName: string,+ label = envName+): BoundedEnvBytes {+ const sourceFile = parseTypeScript(source, label)+ const initializer = directExportedConstInitializer(sourceFile, objectSymbol, label)+ if (!ts.isObjectLiteralExpression(initializer)) {+ throw new Error(`${objectSymbol} must be initialized with an object literal in ${label}`)+ }++ const authorities: ts.ObjectLiteralElementLike[] = []+ for (const member of initializer.properties) {+ if (ts.isSpreadAssignment(member)) {+ throw new Error(`${objectSymbol} must not contain spread authority in ${label}`)+ }+ if (ts.isComputedPropertyName(member.name) && propertyName(member.name) === null) {+ throw new Error(`${objectSymbol} contains an unresolved computed authority in ${label}`)+ }+ if (propertyName(member.name) === property) authorities.push(member)+ }++ if (authorities.length !== 1) {+ throw new Error(`${property} must have exactly one active authority in ${label}`)+ }+ const authority = authorities[0]+ if (!ts.isPropertyAssignment(authority)) {+ throw new Error(`${property} must use an explicit property assignment in ${label}`)+ }+ const call = authority.initializer+ if (+ !ts.isCallExpression(call) ||+ !ts.isIdentifier(call.expression) ||+ call.expression.text !== 'boundedIntegerFromEnv' ||+ call.arguments.length !== 3 ||+ !ts.isStringLiteral(call.arguments[0]) ||+ call.arguments[0].text !== envName+ ) {+ throw new Error(`${property} must use the expected boundedIntegerFromEnv call in ${label}`)+ }+ const [, fallback, ceiling] = call.arguments+ return {+ fallback: evaluateStaticByteExpression(fallback, sourceFile, `${label}:${envName}:fallback`),+ ceiling: evaluateStaticByteExpression(ceiling, sourceFile, `${label}:${envName}:ceiling`),+ }+}diff --git a/control-api/test/staticSourceAuthority.test.ts b/control-api/test/staticSourceAuthority.test.tsnew file mode 100644index 000000000..041211e10--- /dev/null+++ b/control-api/test/staticSourceAuthority.test.ts@@ -0,0 +1,92 @@+import { describe, expect, it } from 'vitest'+import {+ boundedEnvBytesFromSource,+ staticExportedBytesFromSource,+} from './helpers/staticSourceAuthority.js'++const SYMBOL = 'GFS_UPLOAD_V2_MAX_PART_BYTES'+const OBJECT = 'config'+const PROPERTY = 'gfsUploadMaxPartBytes'+const ENV = 'CONTROL_API_GFS_UPLOAD_MAX_PART_BYTES'+const BOUNDED_CALL = `boundedIntegerFromEnv('${ENV}', 16 * 1024 * 1024, 16 * 1024 * 1024)`++function bounded(source: string) {+ return boundedEnvBytesFromSource(source, OBJECT, PROPERTY, ENV)+}++describe('bounded static source authority', () => {+ it.each([+ [`export const ${SYMBOL} = 16 * 1024 * 1024`, 16 * 1024 * 1024],+ [`export const ${SYMBOL} = 16 * 1_024 * 1_024`, 16 * 1024 * 1024],+ [`export const ${SYMBOL} = (16 * 1024) * 1024`, 16 * 1024 * 1024],+ ])('accepts a unique direct export', (source, expected) => {+ expect(staticExportedBytesFromSource(source, SYMBOL)).toBe(expected)+ })++ it.each([+ `const ${SYMBOL} = 16 * 1024 * 1024`,+ `const local = 16 * 1024 * 1024; export { local as ${SYMBOL} }`,+ `const ${SYMBOL} = 16 * 1024 * 1024; export { ${SYMBOL} }`,+ `export const ${SYMBOL} = 16 * 1024 * 1024; function shadow() { const ${SYMBOL} = 1 }`,+ `export const ${SYMBOL} = 16 * 1024 * 1024 + 1`,+ `export const ${SYMBOL} = 16 * 1024 * 1024 / 2`,+ `export const ${SYMBOL} = 16 * 1024 * OTHER`,+ `// export const ${SYMBOL} = 16 * 1024 * 1024`,+ `/* export const ${SYMBOL} = 16 * 1024 * 1024 */`,+ `export const ${SYMBOL} = 16 * 1024 * 1024 nonsense`,+ ])('rejects ambiguous or unsupported exported authority: %s', source => {+ expect(() => staticExportedBytesFromSource(source, SYMBOL)).toThrow()+ })++ it('ignores a commented safe export before the active authority', () => {+ const source = [+ `// export const ${SYMBOL} = 16 * 1024 * 1024`,+ `export const ${SYMBOL} = 17 * 1024 * 1024`,+ ].join('\n')+ expect(staticExportedBytesFromSource(source, SYMBOL)).toBe(17 * 1024 * 1024)+ })++ it.each([+ `export const ${OBJECT} = { ${PROPERTY}: ${BOUNDED_CALL} }`,+ `export const ${OBJECT} = { '${PROPERTY}': ${BOUNDED_CALL} }`,+ `export const ${OBJECT} = { ['${PROPERTY}']: ${BOUNDED_CALL} }`,+ `export const ${OBJECT} = { [('${PROPERTY}')]: ${BOUNDED_CALL} }`,+ ])('accepts one explicit object authority: %s', source => {+ expect(bounded(source)).toEqual({+ fallback: 16 * 1024 * 1024,+ ceiling: 16 * 1024 * 1024,+ })+ })++ it.each([+ `const ${PROPERTY} = ${BOUNDED_CALL}; export const ${OBJECT} = { ${PROPERTY} }`,+ `export const ${OBJECT} = { ...other, ${PROPERTY}: ${BOUNDED_CALL} }`,+ `export const ${OBJECT} = { [authorityName]: ${BOUNDED_CALL} }`,+ `export const ${OBJECT} = { ${PROPERTY}: ${BOUNDED_CALL}, ['${PROPERTY}']: ${BOUNDED_CALL} }`,+ `export const ${OBJECT} = { ${PROPERTY}: boundedIntegerFromEnv('OTHER', 1, 1) }`,+ `export const ${OBJECT} = { ${PROPERTY}: boundedIntegerFromEnv('${ENV}', 1) }`,+ ])('rejects unresolved, duplicate, shorthand, or malformed object authority: %s', source => {+ expect(() => bounded(source)).toThrow()+ })++ it('ignores comments and unrelated properties', () => {+ const source = `export const ${OBJECT} = {+ // ${PROPERTY}: boundedIntegerFromEnv('OTHER', 1, 1),+ ${PROPERTY}Extra: boundedIntegerFromEnv('OTHER', 1, 1),+ ['unrelated']: boundedIntegerFromEnv('OTHER', 1, 1),+ ${PROPERTY}: ${BOUNDED_CALL},+ }`+ expect(bounded(source)).toEqual({+ fallback: 16 * 1024 * 1024,+ ceiling: 16 * 1024 * 1024,+ })+ })++ it('rejects a commented authority without a live assignment', () => {+ expect(() =>+ bounded(`export const ${OBJECT} = {+ // ${PROPERTY}: ${BOUNDED_CALL},+ }`)+ ).toThrow(/exactly one active authority/)+ })+})