66// confidence 1), and with no schema at all the tool reported TableNotFound although its
77// description promises existence checks are skipped.
88import { afterAll , beforeAll , describe , expect , test } from "bun:test"
9- import { foldIdentifierCase , normalizeSchemaContext } from "../../src/altimate/native/schema-resolver"
10- import { isExistenceError } from "../../src/altimate/native/altimate-core"
9+
10+ // Both source modules import `@altimateai/altimate-core` statically, so they are loaded
11+ // lazily: a static import here would fail the whole file where the napi binding cannot
12+ // load, and the skip guard below would never run. (bot review)
13+ const hasCore = ( ( ) => {
14+ try {
15+ require . resolve ( "@altimateai/altimate-core" )
16+ return true
17+ } catch {
18+ return false
19+ }
20+ } ) ( )
21+ const describeIf = hasCore ? describe : describe . skip
1122
1223const SQL = `select customer_region, sum(net_revenue) as net_revenue
1324from TPCH_ANALYTICS.PUBLIC_REPORTING.RPT_MONTHLY_SALES_BY_REGION
@@ -24,7 +35,17 @@ const UPPER = {
2435 } ,
2536}
2637
27- describe ( "foldIdentifierCase" , ( ) => {
38+ describeIf ( "foldIdentifierCase" , ( ) => {
39+ let foldIdentifierCase : typeof import ( "../../src/altimate/native/schema-resolver" ) . foldIdentifierCase
40+ let foldQuotedIdentifierCase : typeof import ( "../../src/altimate/native/schema-resolver" ) . foldQuotedIdentifierCase
41+ let normalizeSchemaContext : typeof import ( "../../src/altimate/native/schema-resolver" ) . normalizeSchemaContext
42+ let schemaProvided : typeof import ( "../../src/altimate/native/schema-resolver" ) . schemaProvided
43+ beforeAll ( async ( ) => {
44+ ; ( { foldIdentifierCase, foldQuotedIdentifierCase, normalizeSchemaContext, schemaProvided } = await import (
45+ "../../src/altimate/native/schema-resolver"
46+ ) )
47+ } )
48+
2849 test ( "an all-uppercase name (created unquoted) folds to the engine's lowercase form" , ( ) => {
2950 expect ( foldIdentifierCase ( "ORDER_MONTH" ) ) . toBe ( "order_month" )
3051 expect ( foldIdentifierCase ( "TPCH_ANALYTICS.PUBLIC_REPORTING.RPT_MONTHLY_SALES_BY_REGION" ) ) . toBe (
@@ -50,9 +71,52 @@ describe("foldIdentifierCase", () => {
5071 const fromFlat = JSON . parse ( normalizeSchemaContext ( { ORDERS : { ORDER_ID : "NUMBER" , "Mixed_Col" : "VARCHAR" } } ) )
5172 expect ( fromFlat . tables . orders . columns . map ( ( c : any ) => c . name ) ) . toEqual ( [ "order_id" , "Mixed_Col" ] )
5273 } )
74+
75+ test ( "a fold that would collide with an entry the schema already has keeps the name as written" , ( ) => {
76+ // Metadata with both `ORDERS` and `orders`: distinct warehouse objects, and folding one
77+ // onto the other would drop its columns. Both survive, the uppercase one unfolded.
78+ const tables = JSON . parse (
79+ normalizeSchemaContext ( { tables : { ORDERS : { columns : [ { name : "A" , type : "INT" } ] } , orders : { columns : [ { name : "b" , type : "INT" } ] } } } ) ,
80+ ) . tables
81+ expect ( Object . keys ( tables ) . sort ( ) ) . toEqual ( [ "ORDERS" , "orders" ] )
82+ expect ( tables . ORDERS . columns [ 0 ] . name ) . toBe ( "a" )
83+ // Same rule inside a table: `FOO` and `foo` columns both stay.
84+ const cols = JSON . parse (
85+ normalizeSchemaContext ( { t : { columns : [ { name : "FOO" , type : "INT" } , { name : "foo" , type : "INT" } , { name : "BAR" , type : "INT" } ] } } ) ,
86+ ) . tables . t . columns . map ( ( c : any ) => c . name )
87+ expect ( cols ) . toEqual ( [ "FOO" , "foo" , "bar" ] )
88+ } )
89+
90+ test ( "a table named __PROTO__ is an entry in the folded schema, not a prototype write" , ( ) => {
91+ const def = JSON . parse ( normalizeSchemaContext ( { __PROTO__ : { ID : "INT" } } ) )
92+ expect ( Object . keys ( def . tables ) ) . toEqual ( [ "__proto__" ] )
93+ expect ( def . tables . __proto__ . columns ) . toEqual ( [ { name : "id" , type : "INT" } ] )
94+ } )
95+
96+ test ( "foldQuotedIdentifierCase lowercases quoted all-uppercase identifiers and nothing else" , ( ) => {
97+ const sql = `select "ORDER_MONTH", "Mixed_Col", "ORDER", 'lit "KEEP"', x -- "NOPE"\n, /* "NOT" */ "A1$" from "ORDERS" where n = 'it''s "OK"'`
98+ expect ( foldQuotedIdentifierCase ( sql ) ) . toBe (
99+ `select "order_month", "Mixed_Col", "order", 'lit "KEEP"', x -- "NOPE"\n, /* "NOT" */ "a1$" from "orders" where n = 'it''s "OK"'` ,
100+ )
101+ expect ( foldQuotedIdentifierCase ( sql ) ) . toHaveLength ( sql . length )
102+ } )
103+
104+ test ( "schemaProvided counts a schema_path, and a schema_context only when it normalises to a table" , ( ) => {
105+ expect ( schemaProvided ( "/some/schema.json" ) ) . toBe ( true )
106+ expect ( schemaProvided ( undefined , UPPER ) ) . toBe ( true )
107+ expect ( schemaProvided ( undefined , { } ) ) . toBe ( false )
108+ expect ( schemaProvided ( undefined , { tables : { } } ) ) . toBe ( false )
109+ expect ( schemaProvided ( undefined , { users : { } } ) ) . toBe ( false )
110+ expect ( schemaProvided ( "" , undefined ) ) . toBe ( false )
111+ } )
53112} )
54113
55- describe ( "isExistenceError" , ( ) => {
114+ describeIf ( "isExistenceError" , ( ) => {
115+ let isExistenceError : typeof import ( "../../src/altimate/native/altimate-core" ) . isExistenceError
116+ beforeAll ( async ( ) => {
117+ ; ( { isExistenceError } = await import ( "../../src/altimate/native/altimate-core" ) )
118+ } )
119+
56120 test ( "recognises the engine's table/column-not-found by code or kind, and nothing else" , ( ) => {
57121 expect ( isExistenceError ( { code : "E001" , kind : { type : "TableNotFound" } } ) ) . toBe ( true )
58122 expect ( isExistenceError ( { code : "E002" , kind : { type : "ColumnNotFound" } } ) ) . toBe ( true )
@@ -63,19 +127,20 @@ describe("isExistenceError", () => {
63127 } )
64128} )
65129
66- const hasCore = await import ( "@altimateai/altimate-core" ) . then ( ( ) => true ) . catch ( ( ) => false )
67- const describeIf = hasCore ? describe : describe . skip
68-
69130describeIf ( "altimate_core.validate through the dispatcher (#1333)" , ( ) => {
70131 let D : any
132+ const telemetry = process . env . ALTIMATE_TELEMETRY_DISABLED
71133 beforeAll ( async ( ) => {
72134 process . env . ALTIMATE_TELEMETRY_DISABLED = "true"
73135 D = await import ( "../../src/altimate/native/dispatcher" )
74136 const core = await import ( "../../src/altimate/native/altimate-core" )
75137 core . registerAll ( )
76138 } )
77139 afterAll ( ( ) => {
78- delete process . env . ALTIMATE_TELEMETRY_DISABLED
140+ if ( telemetry === undefined ) delete process . env . ALTIMATE_TELEMETRY_DISABLED
141+ else process . env . ALTIMATE_TELEMETRY_DISABLED = telemetry
142+ // The handlers went into the process-global dispatcher; leave it as found.
143+ D . reset ( )
79144 } )
80145
81146 test ( "a lowercase Snowflake query validates against UPPERCASE metadata" , async ( ) => {
@@ -98,6 +163,28 @@ describeIf("altimate_core.validate through the dispatcher (#1333)", () => {
98163 expect ( r . data . errors . some ( ( e : any ) => e . kind ?. type === "ColumnNotFound" ) ) . toBe ( true )
99164 } )
100165
166+ test ( "a quoted all-uppercase reference — dbt quote_columns style — validates against uppercase metadata" , async ( ) => {
167+ const quoted = `select "CUSTOMER_REGION", sum("NET_REVENUE") as net_revenue
168+ from "TPCH_ANALYTICS"."PUBLIC_REPORTING"."RPT_MONTHLY_SALES_BY_REGION" where "ORDER_MONTH" >= '1997-01-01' group by 1`
169+ const r = await D . call ( "altimate_core.validate" , { sql : quoted , schema_context : UPPER } )
170+ expect ( r . data . errors ) . toEqual ( [ ] )
171+ expect ( r . data . valid ) . toBe ( true )
172+ // A quoted mixed-case name is the exact-match case and still has to be exact.
173+ const mixed = await D . call ( "altimate_core.validate" , {
174+ sql : `select "Customer_Region" from TPCH_ANALYTICS.PUBLIC_REPORTING.RPT_MONTHLY_SALES_BY_REGION` ,
175+ schema_context : UPPER ,
176+ } )
177+ expect ( mixed . data . valid ) . toBe ( false )
178+ } )
179+
180+ test ( "a schema_context with no tables is treated as no schema" , async ( ) => {
181+ for ( const schema_context of [ { tables : { } } , { users : { } } ] ) {
182+ const r = await D . call ( "altimate_core.validate" , { sql : SQL , schema_context } )
183+ expect ( r . data . errors ) . toEqual ( [ ] )
184+ expect ( r . data . valid ) . toBe ( true )
185+ }
186+ } )
187+
101188 test ( "with no schema, existence findings are dropped and a correct query is valid" , async ( ) => {
102189 const r = await D . call ( "altimate_core.validate" , { sql : SQL , schema_path : "" , schema_context : { } } )
103190 expect ( r . data . valid ) . toBe ( true )
0 commit comments