11/**
22 * @vitest -environment node
33 */
4+ import { dbChainMockFns , resetDbChainMock } from '@sim/testing'
45import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
56
67const { mockLoadRuntimeSecrets, mockPerformFullDeploy } = vi . hoisted ( ( ) => ( {
@@ -20,6 +21,7 @@ import {
2021 backfillTableWorkflowDeployments ,
2122 deployTableWorkflow ,
2223 parseTableWorkflowDeploymentBackfillArgs ,
24+ postgresTableWorkflowDeploymentStore ,
2325 prepareTableWorkflowDeploymentBackfillEnvironment ,
2426 TABLE_WORKFLOW_DEPLOYMENT_BATCH_SIZE ,
2527 type TableWorkflowDeploymentCandidate ,
@@ -32,6 +34,10 @@ const ORIGINAL_ENV = {
3234 SIM_ENV_SECRET_ID : process . env . SIM_ENV_SECRET_ID ,
3335}
3436
37+ interface MockSqlQuery {
38+ toSQL ( ) : { sql : string }
39+ }
40+
3541function restoreEnvironmentVariable ( key : keyof typeof ORIGINAL_ENV ) : void {
3642 const value = ORIGINAL_ENV [ key ]
3743 if ( value === undefined ) {
@@ -63,6 +69,7 @@ function store(
6369describe ( 'backfillTableWorkflowDeployments' , ( ) => {
6470 beforeEach ( ( ) => {
6571 vi . clearAllMocks ( )
72+ resetDbChainMock ( )
6673 } )
6774
6875 afterEach ( ( ) => {
@@ -112,6 +119,17 @@ describe('backfillTableWorkflowDeployments', () => {
112119 expect ( mockLoadRuntimeSecrets ) . not . toHaveBeenCalled ( )
113120 } )
114121
122+ it ( 'silently excludes missing and archived workflow references from integrity checks' , async ( ) => {
123+ await postgresTableWorkflowDeploymentStore . assertIntegrity ( )
124+
125+ const referenceQuery = dbChainMockFns . execute . mock . calls [ 2 ] ?. [ 0 ] as MockSqlQuery
126+ const queryText = referenceQuery . toSQL ( ) . sql
127+ expect ( queryText ) . toContain ( 'INNER JOIN workflow' )
128+ expect ( queryText ) . toContain ( 'workflow.archived_at IS NULL' )
129+ expect ( queryText ) . not . toContain ( 'LEFT JOIN workflow' )
130+ expect ( queryText ) . not . toContain ( 'workflow.id IS NULL' )
131+ } )
132+
115133 it ( 'deploys bounded keyset pages and verifies the final desired state' , async ( ) => {
116134 const listCandidates = vi
117135 . fn < TableWorkflowDeploymentStore [ 'listCandidates' ] > ( )
0 commit comments