@@ -873,3 +873,126 @@ describe("Full E2E session simulation", () => {
873873 expect ( allSerialized ) . not . toContain ( "credit_card" )
874874 } )
875875} )
876+
877+ // ===========================================================================
878+ // altimate-core failure isolation — computeSqlFingerprint resilience
879+ // ===========================================================================
880+ describe ( "altimate-core failure isolation" , ( ) => {
881+ const core = require ( "@altimateai/altimate-core" )
882+
883+ test ( "computeSqlFingerprint returns null when getStatementTypes throws" , ( ) => {
884+ const orig = core . getStatementTypes
885+ core . getStatementTypes = ( ) => {
886+ throw new Error ( "NAPI segfault" )
887+ }
888+ try {
889+ const result = computeSqlFingerprint ( "SELECT 1" )
890+ expect ( result ) . toBeNull ( )
891+ } finally {
892+ core . getStatementTypes = orig
893+ }
894+ } )
895+
896+ test ( "computeSqlFingerprint returns null when extractMetadata throws" , ( ) => {
897+ const orig = core . extractMetadata
898+ core . extractMetadata = ( ) => {
899+ throw new Error ( "out of memory" )
900+ }
901+ try {
902+ const result = computeSqlFingerprint ( "SELECT 1" )
903+ expect ( result ) . toBeNull ( )
904+ } finally {
905+ core . extractMetadata = orig
906+ }
907+ } )
908+
909+ test ( "computeSqlFingerprint handles undefined return from getStatementTypes" , ( ) => {
910+ const orig = core . getStatementTypes
911+ core . getStatementTypes = ( ) => undefined
912+ try {
913+ const result = computeSqlFingerprint ( "SELECT 1" )
914+ expect ( result ) . not . toBeNull ( )
915+ if ( result ) {
916+ expect ( result . statement_types ) . toEqual ( [ ] )
917+ expect ( result . categories ) . toEqual ( [ ] )
918+ }
919+ } finally {
920+ core . getStatementTypes = orig
921+ }
922+ } )
923+
924+ test ( "computeSqlFingerprint handles undefined return from extractMetadata" , ( ) => {
925+ const orig = core . extractMetadata
926+ core . extractMetadata = ( ) => undefined
927+ try {
928+ const result = computeSqlFingerprint ( "SELECT 1" )
929+ expect ( result ) . not . toBeNull ( )
930+ if ( result ) {
931+ expect ( result . table_count ) . toBe ( 0 )
932+ expect ( result . function_count ) . toBe ( 0 )
933+ expect ( result . has_subqueries ) . toBe ( false )
934+ expect ( result . has_aggregation ) . toBe ( false )
935+ }
936+ } finally {
937+ core . extractMetadata = orig
938+ }
939+ } )
940+
941+ test ( "computeSqlFingerprint handles garbage data from core" , ( ) => {
942+ const origStmt = core . getStatementTypes
943+ const origMeta = core . extractMetadata
944+ core . getStatementTypes = ( ) => ( { types : "not-array" , categories : null , statements : 42 } )
945+ core . extractMetadata = ( ) => ( { tables : 42 , columns : "bad" , functions : undefined } )
946+ try {
947+ const result = computeSqlFingerprint ( "SELECT 1" )
948+ // Should not throw — defaults handle bad data
949+ expect ( result ) . not . toBeNull ( )
950+ } finally {
951+ core . getStatementTypes = origStmt
952+ core . extractMetadata = origMeta
953+ }
954+ } )
955+
956+ test ( "sql-execute fingerprint try/catch isolates failures from query results" , ( ) => {
957+ // Verify the code structure: fingerprinting runs AFTER query result is computed
958+ // and is wrapped in its own try/catch
959+ const fs = require ( "fs" )
960+ const src = fs . readFileSync (
961+ require ( "path" ) . join ( __dirname , "../../src/altimate/tools/sql-execute.ts" ) ,
962+ "utf8" ,
963+ )
964+ // Query execution happens first
965+ const execIdx = src . indexOf ( 'Dispatcher.call("sql.execute"' )
966+ const formatIdx = src . indexOf ( "formatResult(result)" )
967+ const fpCallIdx = src . indexOf ( "computeSqlFingerprint(args.query)" )
968+ const guardComment = src . indexOf ( "Fingerprinting must never break query execution" )
969+
970+ expect ( execIdx ) . toBeGreaterThan ( 0 )
971+ expect ( formatIdx ) . toBeGreaterThan ( execIdx ) // format after execute
972+ expect ( fpCallIdx ) . toBeGreaterThan ( formatIdx ) // fingerprint after format
973+ expect ( guardComment ) . toBeGreaterThan ( fpCallIdx ) // catch guard exists after fingerprint
974+ } )
975+
976+ test ( "crash-resistant SQL inputs all handled safely" , ( ) => {
977+ const inputs = [
978+ "" ,
979+ " " ,
980+ ";;;" ,
981+ "-- comment only" ,
982+ "SELECT FROM WHERE" , // incomplete
983+ "DROP TABLE users; -- injection" ,
984+ "\x00\x01\x02" , // control chars
985+ "SELECT " + "x," . repeat ( 1000 ) + "x FROM t" , // very wide
986+ ]
987+ for ( const sql of inputs ) {
988+ expect ( ( ) => computeSqlFingerprint ( sql ) ) . not . toThrow ( )
989+ }
990+ } )
991+
992+ test ( "altimate-core produces consistent results across calls" , ( ) => {
993+ const sql = "SELECT a.id, COUNT(*) FROM orders a JOIN users b ON a.uid = b.id GROUP BY a.id"
994+ const fp1 = computeSqlFingerprint ( sql )
995+ const fp2 = computeSqlFingerprint ( sql )
996+ expect ( fp1 ) . toEqual ( fp2 ) // deterministic
997+ } )
998+ } )
0 commit comments