Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion apps/sim/lib/workflows/code/corpus/editing.corpus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import {
executableView,
setOwn,
} from '@sim/workflow-code'
import { SOURCE_ENTRY_FILE } from '@sim/workflow-code/limits'
import { normalizeWorkflowBlockName } from '@sim/workflow-types/workflow'
import { registryCatalog as catalog } from '@/lib/workflows/code/catalog/registry-catalog.test-helpers'
import { DEFAULT_CORPUS_SEED } from '@/lib/workflows/code/corpus'
import { DEFAULT_CORPUS_SEED, generateReapplyCorpus } from '@/lib/workflows/code/corpus'
import {
applyCanvasOp,
CANVAS_OP_KINDS,
Expand Down Expand Up @@ -967,3 +968,78 @@ describe('renaming a block in source keeps the stored block it continues', () =>
expect(codeEditFailure(state, got, renameEdit(got, key, 'Renamed In Source'))).toBeNull()
})
})

describe('a webhook token creation minted', () => {
it('is cleared by an explicit null in source instead of coming back from the stored block', () => {
const [webhookCase] = generateReapplyCorpus()
if (!webhookCase) throw new Error('the registry has no generic_webhook block')
const state = structuredClone(webhookCase.state)
const [webhook] = Object.values(state.blocks)
const token = webhook?.subBlocks.token
if (!webhook || !token) throw new Error('the webhook fixture has no token subBlock')
token.value = 'minted-token-value'
const got = getSource(state, catalog)
const entry = got.files[SOURCE_ENTRY_FILE] ?? ''
expect(entry).toContain('token: stored()')
const applied = applySource(
state,
got,
{ ...got.files, [SOURCE_ENTRY_FILE]: entry.replace('token: stored()', 'token: null') },
catalog
)
expect(applied.materialized?.state.blocks[webhook.id]?.subBlocks.token?.value).toBeNull()
})

it('re-applies an explicit null over a cleared token as a no-op', () => {
const [webhookCase] = generateReapplyCorpus()
if (!webhookCase) throw new Error('the registry has no generic_webhook block')
const state = structuredClone(webhookCase.state)
const [webhook] = Object.values(state.blocks)
const token = webhook?.subBlocks.token
if (!webhook || !token) throw new Error('the webhook fixture has no token subBlock')
token.value = null
const got = getSource(state, catalog)
const entry = got.files[SOURCE_ENTRY_FILE] ?? ''
expect(entry).toContain('{ requireAuth: true }')
const withNull = entry.replace('{ requireAuth: true }', '{ requireAuth: true, token: null }')
const applied = applySource(
state,
got,
{ ...got.files, [SOURCE_ENTRY_FILE]: withNull },
catalog
)
expect(applied.noop).toBe(true)
})

it('keeps an explicit null on a new webhook, and re-applying it is a no-op', () => {
const [webhookCase] = generateReapplyCorpus()
if (!webhookCase) throw new Error('the registry has no generic_webhook block')
const state = structuredClone(webhookCase.state)
const got = getSource(state, catalog)
const entry = got.files[SOURCE_ENTRY_FILE] ?? ''
const added =
" const hookTwo = trigger.webhook('Hook Two', { token: null }, { triggerMode: true })\n})"
expect(entry.trimEnd().endsWith('})')).toBe(true)
const withNew = `${entry.trimEnd().slice(0, -2)}${added}\n`
const created = applySource(state, got, { ...got.files, [SOURCE_ENTRY_FILE]: withNew }, catalog)
const next = created.materialized?.state
if (!next) throw new Error('adding the webhook did not apply')
const hookTwo = Object.values(next.blocks).find((block) => block.name === 'Hook Two')
expect(hookTwo?.subBlocks.token?.value ?? null).toBeNull()

const again = getSource(next, catalog)
const againEntry = again.files[SOURCE_ENTRY_FILE] ?? ''
const reapplied = againEntry.replace(
"trigger.webhook('Hook Two', {",
"trigger.webhook('Hook Two', { token: null,"
)
expect(reapplied).toContain("trigger.webhook('Hook Two', { token: null,")
const second = applySource(
next,
again,
{ ...again.files, [SOURCE_ENTRY_FILE]: reapplied },
catalog
)
expect(second.noop).toBe(true)
})
})
17 changes: 11 additions & 6 deletions apps/sim/lib/workflows/code/corpus/fixtures.test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ export function sampleCorpus(
const CASES_PER_BLOCK_TYPE = 3
const FUZZ_RUNS = 50

/** A generated corpus case as a proof subject, in the shape the normalized tables store. */
export function caseSubject(corpusCase: CorpusCase): ProofSubject {
return {
id: corpusCase.id,
blockTypes: corpusCase.blockTypes,
state: persistedShape(corpusCase.state),
...(corpusCase.unknownTypes && { unknownTypes: true as const }),
}
}

/**
* Every workflow a corpus property runs against: the generated registry corpus (sampled unless
* `ROUNDTRIP_CORPUS_FULL=1`), the landing templates, the `@sim/testing` states, redacted production
Expand All @@ -214,12 +224,7 @@ export function corpusSubjects(): ProofSubject[] {
process.env.ROUNDTRIP_CORPUS_FULL === '1'
? generated
: sampleCorpus(generated, { perType: CASES_PER_BLOCK_TYPE })
).map((corpusCase) => ({
id: corpusCase.id,
blockTypes: corpusCase.blockTypes,
state: persistedShape(corpusCase.state),
...(corpusCase.unknownTypes && { unknownTypes: true as const }),
}))
).map(caseSubject)
return [
...corpus,
...loadLandingTemplateStates().map(fixtureSubject),
Expand Down
29 changes: 27 additions & 2 deletions apps/sim/lib/workflows/code/corpus/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ import { getBlockRegistry } from '@/blocks/registry'
const corpus = generateRegistryCorpus()
const known = corpus.filter((c) => !c.unknownTypes)

/** Field types whose stored value a `legacy-stored-value` case holds in a shape their validator refuses. */
const LEGACY_SHAPE_TYPES: ReadonlySet<string> = new Set([
'condition-input',
'router-input',
'input-format',
])

function valueProblems(corpusCase: CorpusCase): string[] {
const problems: string[] = []
for (const block of Object.values(corpusCase.state.blocks)) {
Expand All @@ -47,7 +54,10 @@ function valueProblems(corpusCase: CorpusCase): string[] {
corpusCase.storedQuirk === 'legacy-scalar-text' &&
typeof subBlock.value === 'string' &&
declarations.some((declared) => declared.type === 'switch' || declared.type === 'slider')
if (!legacyText && !results.some((result) => result.valid)) {
const legacyShape =
corpusCase.storedQuirk === 'legacy-stored-value' &&
declarations.some((declared) => LEGACY_SHAPE_TYPES.has(declared.type))
if (!legacyText && !legacyShape && !results.some((result) => result.valid)) {
problems.push(`${block.type}.${id}: ${results[0]?.error?.error}`)
}
}
Expand Down Expand Up @@ -139,11 +149,26 @@ describe('generateRegistryCorpus', () => {
'unicode-names',
'crlf-text',
'agent-tools',
'stored-shapes',
]) {
expect(kinds).toContain(kind)
}
expect(corpus.some((c) => c.id.endsWith(':advanced'))).toBe(true)
expect(corpus.some((c) => c.id === 'branches:condition:null-rows')).toBe(true)
const ids = new Set(corpus.map((c) => c.id))
const pinned = [
'branches:condition:null-rows',
'agent-tools:agent:params-repeat-operation',
'stored-shapes:condition:native-legacy-rows',
'stored-shapes:condition:empty-string-branches',
'stored-shapes:router_v2:empty-string-branches',
'stored-shapes:start_trigger:input-format-schema-object',
'stored-shapes:start_trigger:input-format-object-text',
'stored-shapes:api:native-json-object',
'stored-shapes:api:native-json-array',
'stored-shapes:response:native-json-object',
'stored-shapes:apollo:extra-code-param',
]
expect(pinned.filter((id) => !ids.has(id))).toEqual([])
const toolLists = known
.filter((c) => c.kind !== 'agent-tools')
.flatMap((c) =>
Expand Down
Loading
Loading