|
1 | 1 | import { readFileSync } from 'node:fs' |
2 | 2 | import { join } from 'node:path' |
3 | | -import type { BlockCatalog } from '@sim/workflow-code' |
| 3 | +import { |
| 4 | + type BlockCatalog, |
| 5 | + type CatalogSubBlock, |
| 6 | + catalogBlock, |
| 7 | + catalogBuilder, |
| 8 | + compileWorkflow, |
| 9 | +} from '@sim/workflow-code' |
4 | 10 | import { catalogBuilderPaths } from '@sim/workflow-code/catalog/view' |
5 | 11 | import { describe, expect, it } from 'vitest' |
6 | 12 | import { CORE_DTS } from './core-text.generated' |
@@ -88,3 +94,88 @@ describe('lookupBuilder for a name the SDK spells differently', () => { |
88 | 94 | expect(hint('humanOutOfTheLoop')).not.toBe('Did you mean human?') |
89 | 95 | }) |
90 | 96 | }) |
| 97 | + |
| 98 | +/** The sibling fields a declaration's visibility and requirement read. */ |
| 99 | +function fieldsRead(sub: CatalogSubBlock): string[] { |
| 100 | + return [sub.condition, sub.required].flatMap((clause) => |
| 101 | + clause && typeof clause === 'object' && 'field' in clause |
| 102 | + ? [clause.field, ...(clause.and ? [clause.and.field] : [])] |
| 103 | + : [] |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Compiles `<builder>('X', {})` for every builder and returns each `W_REQUIRED_MISSING` param the |
| 109 | + * builder's params facts show optional. A field whose requirement reads another field the call may |
| 110 | + * set (`provider`, `authType`) is not counted: the type cannot know that value. |
| 111 | + */ |
| 112 | +function optionalButRefused(catalog: BlockCatalog): { refused: number; optional: string[] } { |
| 113 | + const optional: string[] = [] |
| 114 | + let refused = 0 |
| 115 | + for (const key of catalogBuilderPaths(catalog)) { |
| 116 | + const reference = describeBuilder(catalog, key) |
| 117 | + const builder = catalogBuilder(catalog, key) |
| 118 | + const block = reference && catalogBlock(catalog, reference.blockType) |
| 119 | + if (!reference || !builder || !block || reference.coreParams.length > 0) continue |
| 120 | + const pinned = new Set(['operation', ...Object.keys(builder.fixed ?? {})]) |
| 121 | + const root = key.split('.')[0] |
| 122 | + const result = compileWorkflow({ |
| 123 | + entry: { |
| 124 | + path: 'workflows/w.ts', |
| 125 | + text: `// @sim-sdk 1\nimport { ${root}, workflow } from '@sim/workflow-sdk'\n\nexport default workflow('W', {}, () => {\n const x = ${key}('X', {})\n})\n`, |
| 126 | + }, |
| 127 | + files: new Map(), |
| 128 | + catalog, |
| 129 | + sdkMajor: catalog.sdkMajor, |
| 130 | + }) |
| 131 | + for (const { code, param } of result.diagnostics) { |
| 132 | + if (code !== 'W_REQUIRED_MISSING' || !param) continue |
| 133 | + const declarations = block.subBlocks.filter( |
| 134 | + (sub) => sub.id === param || sub.canonicalParamId === param |
| 135 | + ) |
| 136 | + if (declarations.some((sub) => fieldsRead(sub).some((field) => !pinned.has(field)))) { |
| 137 | + continue |
| 138 | + } |
| 139 | + refused += 1 |
| 140 | + const { common, gate } = reference.params |
| 141 | + const omittedVariant = gate?.variants.find( |
| 142 | + (variant) => gate.default !== undefined && variant.values.includes(gate.default) |
| 143 | + ) |
| 144 | + const fact = [...common, ...(omittedVariant?.params ?? [])].find((f) => f.key === param) |
| 145 | + if (gate?.key === param ? gate.default !== undefined : fact?.required !== true) { |
| 146 | + optional.push(`${key}.${param}`) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + return { refused, optional } |
| 151 | +} |
| 152 | + |
| 153 | +/** The fixture catalog with Slack's required `listShareTarget` gate stripped of its default. */ |
| 154 | +function withBareRequiredGate(): BlockCatalog { |
| 155 | + const catalog = structuredClone(CATALOG) |
| 156 | + const subBlocks = catalog.blocks.slack_v2?.subBlocks ?? [] |
| 157 | + const index = subBlocks.findIndex((sub) => 'id' in sub && sub.id === 'listShareTarget') |
| 158 | + const gate = subBlocks[index] |
| 159 | + if (!gate || !('id' in gate) || gate.required !== true || !gate.valueDefault) { |
| 160 | + throw new Error('fixture slack_v2 has no required, defaulted listShareTarget') |
| 161 | + } |
| 162 | + const { valueDefault: _dropped, ...bare } = gate |
| 163 | + subBlocks[index] = bare |
| 164 | + return catalog |
| 165 | +} |
| 166 | + |
| 167 | +describe('required params', () => { |
| 168 | + /** |
| 169 | + * `check` refuses a call that omits a field the builder alone decides is required, so its params |
| 170 | + * type and `sim workflows source params` must not show it optional: a dropdown or gate with no |
| 171 | + * default holds nothing until source writes it. |
| 172 | + */ |
| 173 | + it.each([ |
| 174 | + ['the fixture catalog', () => CATALOG], |
| 175 | + ['a required gate with no default', withBareRequiredGate], |
| 176 | + ])('marks required every param the compiler refuses to omit, over %s', (_label, catalog) => { |
| 177 | + const { refused, optional } = optionalButRefused(catalog()) |
| 178 | + expect(refused).toBeGreaterThan(0) |
| 179 | + expect(optional).toEqual([]) |
| 180 | + }) |
| 181 | +}) |
0 commit comments