Skip to content

Commit 64dd815

Browse files
suryaiyer95claude
andcommitted
fix: CI — isolate data-diff-cross-dialect tests from other files
The prior integration-style test mocked the Registry module globally with `mock.module(".../registry", ...)`, which leaks across all test files in bun:test's single-process runner. That caused 14 unrelated tests in `connections.test.ts`, `telemetry-safety.test.ts`, and `dbt-first-execution.test.ts` to fail in CI. Additionally, the test relied on `mock.module("@altimateai/altimate-core")` to supply a fake `DataParitySession`. The npm-published 0.2.6 of that package does not export `DataParitySession` (sessions are only in the locally-built `altimate-core-internal` binary), and Bun's `mock.module` cannot override a package that another test file has already imported — so the integration test was structurally unreliable. Resolution: 1. **Export pure SQL-builder helpers** from `data-diff.ts` (`dateTruncExpr`, `buildPartitionWhereClause`) and unit-test them directly. No module mocking required; the test directly exercises the logic the CRITICAL/MAJOR fix changed. 2. **Move the `joindiff` + cross-warehouse guard earlier** in `runDataDiff` — before the NAPI import. Semantically identical for callers (guard still fires, same error message, `steps: 0`), but now it can be integration-tested without any NAPI mock. Preserves end-to-end wiring coverage for the guard. 3. **Rewrite `data-diff-cross-dialect.test.ts`** as pure-function unit tests for the partition WHERE logic + a real `runDataDiff` call for the joindiff guard. No more cross-file mock pollution. Functionality unchanged: - `runDataDiff` behavior for real callers is identical. The only observable difference is error-ordering: if a caller simultaneously omits NAPI and passes `joindiff + cross-warehouse`, they now get the "joindiff requires same warehouse" error instead of the NAPI-missing error. That's strictly better UX — NAPI availability is a deployment concern, `joindiff`+cross-warehouse is a user error. - `buildPartitionWhereClause` and `dateTruncExpr` are now exported but semantically unchanged — same inputs, same outputs. Test results: - 2821 altimate tests pass, 0 fail - 43 sqlserver driver tests pass, 0 fail - The 19 remaining full-suite failures (`mcp/`, `tool/project-scan`, `plan-approval-phrase`) are pre-existing on `main` and unrelated. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 3ebcec1 commit 64dd815

2 files changed

Lines changed: 194 additions & 161 deletions

File tree

‎packages/opencode/src/altimate/native/connections/data-diff.ts‎

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,10 @@ const MAX_STEPS = 200
482482

483483
/**
484484
* Build a DATE_TRUNC expression appropriate for the warehouse dialect.
485+
*
486+
* Exported for targeted unit testing; not part of the stable public API.
485487
*/
486-
function dateTruncExpr(granularity: string, column: string, dialect: string): string {
488+
export function dateTruncExpr(granularity: string, column: string, dialect: string): string {
487489
const g = granularity.toLowerCase()
488490
switch (dialect) {
489491
case "bigquery":
@@ -565,8 +567,10 @@ function buildPartitionDiscoverySQL(
565567

566568
/**
567569
* Build a WHERE clause that scopes to a single partition.
570+
*
571+
* Exported for targeted unit testing; not part of the stable public API.
568572
*/
569-
function buildPartitionWhereClause(
573+
export function buildPartitionWhereClause(
570574
partitionColumn: string,
571575
partitionValue: string,
572576
granularity: string | undefined,
@@ -845,24 +849,6 @@ export async function runDataDiff(params: DataDiffParams): Promise<DataDiffResul
845849
return runPartitionedDiff(params)
846850
}
847851

848-
// Dynamically import NAPI module (not available in test environments without the binary)
849-
let DataParitySession: new (specJson: string) => {
850-
start(): string
851-
step(responsesJson: string): string
852-
}
853-
854-
try {
855-
const core = await import("@altimateai/altimate-core")
856-
DataParitySession = (core as any).DataParitySession
857-
if (!DataParitySession) throw new Error("DataParitySession not exported from @altimateai/altimate-core")
858-
} catch (e) {
859-
return {
860-
success: false,
861-
error: `altimate-core NAPI module unavailable: ${e}`,
862-
steps: 0,
863-
}
864-
}
865-
866852
// Resolve warehouse identity (fall back to the default warehouse when the
867853
// caller omits a side). Returns the canonical warehouse name so we can detect
868854
// cross-warehouse mode even when both sides share a dialect (e.g. two
@@ -889,6 +875,42 @@ export async function runDataDiff(params: DataDiffParams): Promise<DataDiffResul
889875
const dialect1 = resolveDialect(params.source_warehouse)
890876
const dialect2 = resolveDialect(params.target_warehouse ?? params.source_warehouse)
891877

878+
// Input-validation guards — run BEFORE the NAPI import so they produce the
879+
// right error even in environments where `@altimateai/altimate-core` isn't
880+
// built locally.
881+
//
882+
// JoinDiff cannot work across warehouses: it emits one FULL OUTER JOIN task
883+
// referencing both CTE aliases, but side-aware injection only defines one
884+
// side per task — the other alias would be unresolved. Guard early so users
885+
// get a clear error instead of an obscure SQL parse failure.
886+
const crossWarehousePre = resolvedSource !== resolvedTarget
887+
if (params.algorithm === "joindiff" && crossWarehousePre) {
888+
return {
889+
success: false,
890+
steps: 0,
891+
error:
892+
"joindiff requires both tables in the same warehouse; use hashdiff or auto for cross-warehouse comparisons.",
893+
}
894+
}
895+
896+
// Dynamically import NAPI module (not available in test environments without the binary)
897+
let DataParitySession: new (specJson: string) => {
898+
start(): string
899+
step(responsesJson: string): string
900+
}
901+
902+
try {
903+
const core = await import("@altimateai/altimate-core")
904+
DataParitySession = (core as any).DataParitySession
905+
if (!DataParitySession) throw new Error("DataParitySession not exported from @altimateai/altimate-core")
906+
} catch (e) {
907+
return {
908+
success: false,
909+
error: `altimate-core NAPI module unavailable: ${e}`,
910+
steps: 0,
911+
}
912+
}
913+
892914
// Cross-warehouse mode requires side-specific CTE injection: T-SQL / Fabric
893915
// parse-bind every CTE body even when unreferenced, so sending the combined
894916
// prefix to a warehouse that lacks the other side's base table fails at parse.
@@ -897,20 +919,7 @@ export async function runDataDiff(params: DataDiffParams): Promise<DataDiffResul
897919
// other's base tables. Identity comparison after resolving the default
898920
// warehouse avoids misclassifying `source_warehouse=undefined` vs the
899921
// explicit default-warehouse name as different.
900-
const crossWarehouse = resolvedSource !== resolvedTarget
901-
902-
// Explicit JoinDiff cannot work across warehouses: it emits one FULL OUTER
903-
// JOIN task referencing both CTE aliases, but side-aware injection only
904-
// defines one side per task — the other alias would be unresolved. Guard
905-
// early so users get a clear error instead of an obscure SQL parse failure.
906-
if (params.algorithm === "joindiff" && crossWarehouse) {
907-
return {
908-
success: false,
909-
steps: 0,
910-
error:
911-
"joindiff requires both tables in the same warehouse; use hashdiff or auto for cross-warehouse comparisons.",
912-
}
913-
}
922+
const crossWarehouse = crossWarehousePre
914923

915924
// Resolve sources (plain table names vs arbitrary queries). Pass dialects so
916925
// plain-table names inside wrapped CTEs get side-native bracket/quote style.

0 commit comments

Comments
 (0)