@@ -2193,6 +2193,32 @@ describe('URL Inspector Semrush BP integration', () => {
21932193 expect ( response . status ) . to . equal ( 200 ) ;
21942194 expect ( body . stats . totalCitations ) . to . equal ( 77 ) ;
21952195 } ) ;
2196+
2197+ it ( 're-throws 503 config errors without falling back' , async ( ) => {
2198+ const configError = Object . assign ( new Error ( 'missing element id' ) , { status : 503 } ) ;
2199+ queryBpCitationsByUrlStub . rejects ( configError ) ;
2200+ const { context, rpcStub } = createSemrushContext ( { brandId : BRAND_ID } ) ;
2201+ rpcStub . resolves ( { data : [ { week : null , value : 10 } ] , error : null } ) ;
2202+
2203+ const handler = Handlers . createUrlInspectorStatsHandler (
2204+ async ( ) => ( { organization : { getId : ( ) => ORG_ID } } ) ,
2205+ ) ;
2206+ await expect ( handler ( context ) ) . to . be . rejectedWith ( 'missing element id' ) ;
2207+ } ) ;
2208+
2209+ it ( 'passes hostname (not full URL) as domain to queryBpCitationsByUrl' , async ( ) => {
2210+ const { context, rpcStub } = createSemrushContext ( { brandId : BRAND_ID } , { siteId : 'https://www.example.com' } ) ;
2211+ rpcStub . resolves ( { data : [ { week : null , value : 0 } ] , error : null } ) ;
2212+
2213+ const handler = Handlers . createUrlInspectorStatsHandler (
2214+ async ( ) => ( { organization : { getId : ( ) => ORG_ID } } ) ,
2215+ ) ;
2216+ await handler ( context ) ;
2217+
2218+ const callArgs = queryBpCitationsByUrlStub . firstCall . args [ 5 ] ;
2219+ expect ( callArgs . domain ) . to . equal ( 'www.example.com' ) ;
2220+ expect ( callArgs . urlFragment ) . to . equal ( 'https://www.example.com' ) ;
2221+ } ) ;
21962222 } ) ;
21972223
21982224 describe ( 'createUrlInspectorOwnedUrlsHandler — Semrush path' , ( ) => {
@@ -2276,7 +2302,54 @@ describe('URL Inspector Semrush BP integration', () => {
22762302
22772303 expect ( response . status ) . to . equal ( 200 ) ;
22782304 expect ( body . urls [ 0 ] . citations ) . to . equal ( 5 ) ;
2279- expect ( context . log . error ) . to . have . been . calledWithMatch ( / S e m r u s h B P e r r o r / ) ;
2305+ expect ( context . log . warn ) . to . have . been . calledWithMatch ( / S e m r u s h B P e r r o r / ) ;
2306+ } ) ;
2307+
2308+ it ( 'preserves successful rows when one URL Semrush call fails' , async ( ) => {
2309+ queryBpCitationsByUrlStub . onFirstCall ( ) . resolves ( {
2310+ citations : 99 , promptsCited : 3 , prompts : [ ] ,
2311+ } ) ;
2312+ queryBpCitationsByUrlStub . onSecondCall ( ) . rejects ( new Error ( 'partial fail' ) ) ;
2313+ const row1 = { ...ownedRow ( ) , url : 'https://example.com/page1' , citations : 1 } ;
2314+ const row2 = { ...ownedRow ( ) , url : 'https://example.com/page2' , citations : 2 } ;
2315+ const { context, rpcStub } = createSemrushContext ( { brandId : BRAND_ID } ) ;
2316+ rpcStub . resolves ( { data : [ row1 , row2 ] , error : null } ) ;
2317+
2318+ const handler = Handlers . createUrlInspectorOwnedUrlsHandler (
2319+ async ( ) => ( { organization : { getId : ( ) => ORG_ID } } ) ,
2320+ ) ;
2321+ const response = await handler ( context ) ;
2322+ const body = await response . json ( ) ;
2323+
2324+ expect ( response . status ) . to . equal ( 200 ) ;
2325+ expect ( body . urls [ 0 ] . citations ) . to . equal ( 99 ) ;
2326+ expect ( body . urls [ 1 ] . citations ) . to . equal ( 2 ) ;
2327+ } ) ;
2328+
2329+ it ( 're-throws 503 config errors in owned-urls' , async ( ) => {
2330+ const configError = Object . assign ( new Error ( 'missing element id' ) , { status : 503 } ) ;
2331+ queryBpCitationsByUrlStub . rejects ( configError ) ;
2332+ const { context, rpcStub } = createSemrushContext ( { brandId : BRAND_ID } ) ;
2333+ rpcStub . resolves ( { data : [ ownedRow ( ) ] , error : null } ) ;
2334+
2335+ const handler = Handlers . createUrlInspectorOwnedUrlsHandler (
2336+ async ( ) => ( { organization : { getId : ( ) => ORG_ID } } ) ,
2337+ ) ;
2338+ await expect ( handler ( context ) ) . to . be . rejectedWith ( 'missing element id' ) ;
2339+ } ) ;
2340+
2341+ it ( 'passes hostname (not full URL) as domain in per-URL query' , async ( ) => {
2342+ const { context, rpcStub } = createSemrushContext ( { brandId : BRAND_ID } ) ;
2343+ rpcStub . resolves ( { data : [ ownedRow ( ) ] , error : null } ) ;
2344+
2345+ const handler = Handlers . createUrlInspectorOwnedUrlsHandler (
2346+ async ( ) => ( { organization : { getId : ( ) => ORG_ID } } ) ,
2347+ ) ;
2348+ await handler ( context ) ;
2349+
2350+ const callArgs = queryBpCitationsByUrlStub . firstCall . args [ 5 ] ;
2351+ expect ( callArgs . domain ) . to . equal ( 'example.com' ) ;
2352+ expect ( callArgs . urlFragment ) . to . equal ( 'https://example.com/page' ) ;
22802353 } ) ;
22812354 } ) ;
22822355
@@ -2340,5 +2413,30 @@ describe('URL Inspector Semrush BP integration', () => {
23402413 expect ( body . prompts [ 0 ] . prompt ) . to . equal ( 'fallback prompt' ) ;
23412414 expect ( context . log . error ) . to . have . been . calledWithMatch ( / S e m r u s h B P e r r o r / ) ;
23422415 } ) ;
2416+
2417+ it ( 're-throws 503 config errors in url-prompts' , async ( ) => {
2418+ const configError = Object . assign ( new Error ( 'missing element id' ) , { status : 503 } ) ;
2419+ queryBpCitationsByUrlStub . rejects ( configError ) ;
2420+ const { context } = createSemrushContext ( { brandId : BRAND_ID } ) ;
2421+
2422+ const handler = Handlers . createUrlInspectorUrlPromptsHandler (
2423+ async ( ) => ( { organization : { getId : ( ) => ORG_ID } } ) ,
2424+ ) ;
2425+ const ctxWithUrl = { ...context , data : { siteId : SITE_ID , urlId : 'https://example.com/page' } } ;
2426+ await expect ( handler ( ctxWithUrl ) ) . to . be . rejectedWith ( 'missing element id' ) ;
2427+ } ) ;
2428+
2429+ it ( 'passes hostname as domain for url-prompts when urlId is a URL' , async ( ) => {
2430+ const { context } = createSemrushContext ( { brandId : BRAND_ID } ) ;
2431+
2432+ const handler = Handlers . createUrlInspectorUrlPromptsHandler (
2433+ async ( ) => ( { organization : { getId : ( ) => ORG_ID } } ) ,
2434+ ) ;
2435+ const ctxWithUrl = { ...context , data : { siteId : SITE_ID , urlId : 'https://example.com/page' } } ;
2436+ await handler ( ctxWithUrl ) ;
2437+
2438+ const callArgs = queryBpCitationsByUrlStub . firstCall . args [ 5 ] ;
2439+ expect ( callArgs . domain ) . to . equal ( 'example.com' ) ;
2440+ } ) ;
23432441 } ) ;
23442442} ) ;
0 commit comments