@@ -767,77 +767,146 @@ describe('DBSQLClient telemetry paths', () => {
767767
768768 describe ( 'extractWorkspaceId' , ( ) => {
769769 it ( 'returns the numeric o= param from httpPath' , ( ) => {
770- const client = new DBSQLClient ( ) ;
771- ( client as any ) . httpPath = '/sql/1.0/warehouses/abc?o=12345678901234' ;
772- const id = ( client as any ) . extractWorkspaceId ( ) ;
770+ const id = ( DBSQLClient as any ) . extractWorkspaceId ( '/sql/1.0/warehouses/abc?o=12345678901234' ) ;
773771 expect ( id ) . to . equal ( '12345678901234' ) ;
774772 } ) ;
775773
776774 it ( 'returns undefined when no query string' , ( ) => {
777- const client = new DBSQLClient ( ) ;
778- ( client as any ) . httpPath = '/sql/1.0/warehouses/abc' ;
779- expect ( ( client as any ) . extractWorkspaceId ( ) ) . to . be . undefined ;
775+ expect ( ( DBSQLClient as any ) . extractWorkspaceId ( '/sql/1.0/warehouses/abc' ) ) . to . be . undefined ;
780776 } ) ;
781777
782778 it ( 'returns undefined when o= is not numeric' , ( ) => {
783- const client = new DBSQLClient ( ) ;
784- ( client as any ) . httpPath = '/sql/1.0/warehouses/abc?o=tenant_xyz' ;
785- expect ( ( client as any ) . extractWorkspaceId ( ) ) . to . be . undefined ;
779+ expect ( ( DBSQLClient as any ) . extractWorkspaceId ( '/sql/1.0/warehouses/abc?o=tenant_xyz' ) ) . to . be . undefined ;
786780 } ) ;
787781
788782 it ( 'handles o= as a non-first param' , ( ) => {
789- const client = new DBSQLClient ( ) ;
790- ( client as any ) . httpPath = '/sql/1.0/warehouses/abc?foo=bar&o=42&baz=qux' ;
791- expect ( ( client as any ) . extractWorkspaceId ( ) ) . to . equal ( '42' ) ;
783+ expect ( ( DBSQLClient as any ) . extractWorkspaceId ( '/sql/1.0/warehouses/abc?foo=bar&o=42&baz=qux' ) ) . to . equal ( '42' ) ;
792784 } ) ;
793785
794786 it ( 'returns undefined when httpPath is unset' , ( ) => {
795- const client = new DBSQLClient ( ) ;
796- expect ( ( client as any ) . extractWorkspaceId ( ) ) . to . be . undefined ;
787+ expect ( ( DBSQLClient as any ) . extractWorkspaceId ( undefined ) ) . to . be . undefined ;
788+ } ) ;
789+
790+ it ( 'returns the numeric workspace id from all-purpose cluster path form' , ( ) => {
791+ expect (
792+ ( DBSQLClient as any ) . extractWorkspaceId ( 'sql/protocolv1/o/99999999999999/0101-000000-aaaaaaaa' ) ,
793+ ) . to . equal ( '99999999999999' ) ;
794+ } ) ;
795+
796+ it ( 'returns the numeric workspace id from all-purpose cluster path with leading slash' , ( ) => {
797+ expect (
798+ ( DBSQLClient as any ) . extractWorkspaceId ( '/sql/protocolv1/o/12345/0101-000000-aaaaaaaa' ) ,
799+ ) . to . equal ( '12345' ) ;
800+ } ) ;
801+
802+ it ( 'returns undefined when all-purpose cluster path has non-numeric workspace segment' , ( ) => {
803+ expect (
804+ ( DBSQLClient as any ) . extractWorkspaceId ( 'sql/protocolv1/o/tenant_xyz/0101-000000-aaaaaaaa' ) ,
805+ ) . to . be . undefined ;
806+ } ) ;
807+
808+ it ( 'prefers ?o= query form over /o/ path form when both are present' , ( ) => {
809+ expect (
810+ ( DBSQLClient as any ) . extractWorkspaceId ( 'sql/protocolv1/o/111/cluster?o=222' ) ,
811+ ) . to . equal ( '222' ) ;
797812 } ) ;
798813 } ) ;
799814
800815 describe ( 'buildCustomHeaders (SPOG)' , ( ) => {
801816 it ( 'injects x-databricks-org-id from ?o= in httpPath' , ( ) => {
802817 const client = new DBSQLClient ( ) ;
803- ( client as any ) . httpPath = '/sql/1.0/warehouses/abc?o=12345678901234' ;
804- const headers = ( client as any ) . buildCustomHeaders ( { path : '/sql/1.0/warehouses/abc?o=12345678901234' } ) ;
818+ const headers = ( client as any ) . buildCustomHeaders ( '/sql/1.0/warehouses/abc?o=12345678901234' , undefined ) ;
805819 expect ( headers ) . to . deep . equal ( { 'x-databricks-org-id' : '12345678901234' } ) ;
806820 } ) ;
807821
808822 it ( 'returns undefined when no ?o= and no user-supplied customHeaders' , ( ) => {
809823 const client = new DBSQLClient ( ) ;
810- ( client as any ) . httpPath = '/sql/1.0/warehouses/abc' ;
811- const headers = ( client as any ) . buildCustomHeaders ( { path : '/sql/1.0/warehouses/abc' } ) ;
824+ const headers = ( client as any ) . buildCustomHeaders ( '/sql/1.0/warehouses/abc' , undefined ) ;
812825 expect ( headers ) . to . be . undefined ;
813826 } ) ;
814827
815828 it ( 'preserves user-supplied customHeaders alongside parsed org-id' , ( ) => {
816829 const client = new DBSQLClient ( ) ;
817- ( client as any ) . httpPath = '/sql/1.0/warehouses/abc?o=42' ;
818- const headers = ( client as any ) . buildCustomHeaders ( {
819- path : '/sql/1.0/warehouses/abc?o=42' ,
820- customHeaders : { 'x-trace-id' : 'tid-001' } ,
821- } ) ;
830+ const headers = ( client as any ) . buildCustomHeaders ( '/sql/1.0/warehouses/abc?o=42' , { 'x-trace-id' : 'tid-001' } ) ;
822831 expect ( headers ) . to . deep . equal ( { 'x-trace-id' : 'tid-001' , 'x-databricks-org-id' : '42' } ) ;
823832 } ) ;
824833
825834 it ( 'user-supplied x-databricks-org-id wins over ?o= parsed value (case-insensitive)' , ( ) => {
826835 const client = new DBSQLClient ( ) ;
827- ( client as any ) . httpPath = '/sql/1.0/warehouses/abc?o=42' ;
828- const headers = ( client as any ) . buildCustomHeaders ( {
829- path : '/sql/1.0/warehouses/abc?o=42' ,
830- customHeaders : { 'X-Databricks-Org-Id' : '999' } ,
836+ const headers = ( client as any ) . buildCustomHeaders ( '/sql/1.0/warehouses/abc?o=42' , {
837+ 'X-Databricks-Org-Id' : '999' ,
831838 } ) ;
832839 expect ( headers ) . to . deep . equal ( { 'X-Databricks-Org-Id' : '999' } ) ;
833840 } ) ;
834841
835842 it ( 'does not inject org-id when ?o= value is non-numeric' , ( ) => {
836843 const client = new DBSQLClient ( ) ;
837- ( client as any ) . httpPath = '/sql/1.0/warehouses/abc?o=tenant_xyz' ;
838- const headers = ( client as any ) . buildCustomHeaders ( { path : '/sql/1.0/warehouses/abc?o=tenant_xyz' } ) ;
844+ const headers = ( client as any ) . buildCustomHeaders ( '/sql/1.0/warehouses/abc?o=tenant_xyz' , undefined ) ;
839845 expect ( headers ) . to . be . undefined ;
840846 } ) ;
847+
848+ it ( 'injects x-databricks-org-id from all-purpose cluster path form' , ( ) => {
849+ const client = new DBSQLClient ( ) ;
850+ const headers = ( client as any ) . buildCustomHeaders (
851+ 'sql/protocolv1/o/99999999999999/0101-000000-aaaaaaaa' ,
852+ undefined ,
853+ ) ;
854+ expect ( headers ) . to . deep . equal ( { 'x-databricks-org-id' : '99999999999999' } ) ;
855+ } ) ;
856+
857+ it ( 'logs a warning when workspace ID segment is non-numeric (path form)' , ( ) => {
858+ const client = new DBSQLClient ( ) ;
859+ const logSpy = sinon . spy ( ( client as any ) . logger , 'log' ) ;
860+ try {
861+ ( client as any ) . buildCustomHeaders ( 'sql/protocolv1/o/tenant_xyz/cluster' , undefined ) ;
862+ const warnCalls = logSpy . getCalls ( ) . filter ( ( c ) => c . args [ 0 ] === LogLevel . warn ) ;
863+ expect ( warnCalls ) . to . have . lengthOf ( 1 ) ;
864+ expect ( warnCalls [ 0 ] . args [ 1 ] ) . to . match ( / n o n - n u m e r i c w o r k s p a c e I D / ) ;
865+ } finally {
866+ logSpy . restore ( ) ;
867+ }
868+ } ) ;
869+
870+ it ( 'logs a warning when ?o= is present but non-numeric' , ( ) => {
871+ const client = new DBSQLClient ( ) ;
872+ const logSpy = sinon . spy ( ( client as any ) . logger , 'log' ) ;
873+ try {
874+ ( client as any ) . buildCustomHeaders ( '/sql/1.0/warehouses/abc?o=tenant_xyz' , undefined ) ;
875+ const warnCalls = logSpy . getCalls ( ) . filter ( ( c ) => c . args [ 0 ] === LogLevel . warn ) ;
876+ expect ( warnCalls ) . to . have . lengthOf ( 1 ) ;
877+ expect ( warnCalls [ 0 ] . args [ 1 ] ) . to . match ( / n o n - n u m e r i c w o r k s p a c e I D / ) ;
878+ } finally {
879+ logSpy . restore ( ) ;
880+ }
881+ } ) ;
882+
883+ it ( 'logs a debug line when injecting org-id from httpPath' , ( ) => {
884+ const client = new DBSQLClient ( ) ;
885+ const logSpy = sinon . spy ( ( client as any ) . logger , 'log' ) ;
886+ try {
887+ ( client as any ) . buildCustomHeaders ( '/sql/1.0/warehouses/abc?o=42' , undefined ) ;
888+ const injectLog = logSpy
889+ . getCalls ( )
890+ . find ( ( c ) => c . args [ 0 ] === LogLevel . debug && / i n j e c t i n g x - d a t a b r i c k s - o r g - i d = 4 2 / . test ( String ( c . args [ 1 ] ) ) ) ;
891+ expect ( injectLog , 'expected SPOG inject debug log' ) . to . exist ;
892+ } finally {
893+ logSpy . restore ( ) ;
894+ }
895+ } ) ;
896+
897+ it ( 'logs a debug line when caller supplies x-databricks-org-id' , ( ) => {
898+ const client = new DBSQLClient ( ) ;
899+ const logSpy = sinon . spy ( ( client as any ) . logger , 'log' ) ;
900+ try {
901+ ( client as any ) . buildCustomHeaders ( '/sql/1.0/warehouses/abc?o=42' , { 'x-databricks-org-id' : '999' } ) ;
902+ const callerLog = logSpy
903+ . getCalls ( )
904+ . find ( ( c ) => c . args [ 0 ] === LogLevel . debug && / s u p p l i e d b y c a l l e r / . test ( String ( c . args [ 1 ] ) ) ) ;
905+ expect ( callerLog , 'expected SPOG caller-supplied debug log' ) . to . exist ;
906+ } finally {
907+ logSpy . restore ( ) ;
908+ }
909+ } ) ;
841910 } ) ;
842911
843912 describe ( 'telemetry refcount on reconnect' , ( ) => {
0 commit comments