@@ -5,6 +5,7 @@ import { assert, it } from "@effect/vitest";
55import { ProviderInstanceId , type ProviderAuthState } from "@t3tools/contracts" ;
66import { AcpRegistrySettings } from "../settings.ts" ;
77import * as Deferred from "effect/Deferred" ;
8+ import * as Cause from "effect/Cause" ;
89import * as Effect from "effect/Effect" ;
910import * as Option from "effect/Option" ;
1011import * as Schema from "effect/Schema" ;
@@ -13,7 +14,7 @@ import type * as AcpSchema from "effect-acp/compat";
1314import { AcpRequestError } from "effect-acp/errors" ;
1415
1516import * as PtyAdapter from "@t3tools/shared/PtyAdapter" ;
16- import { makeAcpRegistryAuth } from "./auth.ts" ;
17+ import { formatFailureDetail , makeAcpRegistryAuth } from "./auth.ts" ;
1718import * as AcpRegistrySupport from "./AcpRegistrySupport.ts" ;
1819import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime" ;
1920
@@ -61,6 +62,7 @@ const makeHarness = (
6162 failVerification = false ,
6263 settings = decodeSettings ( { agentId : "test-agent" } ) ,
6364 providerInstanceId = instanceId ,
65+ authenticateError ?: AcpRequestError ,
6466) =>
6567 Effect . gen ( function * ( ) {
6668 const verify = yield * Deferred . make < void > ( ) ;
@@ -111,6 +113,9 @@ const makeHarness = (
111113 authenticate : ( methodId ) =>
112114 Effect . gen ( function * ( ) {
113115 authenticated . push ( methodId ) ;
116+ if ( authenticateError !== undefined ) {
117+ return yield * authenticateError ;
118+ }
114119 if ( elicitation )
115120 yield * elicitation (
116121 {
@@ -342,8 +347,137 @@ it.effect("does not report authentication when post-login verification fails", (
342347 h . exit ( 0 ) ;
343348 yield * h . phase ( "verifying" ) ;
344349 yield * Deferred . succeed ( h . verify , undefined ) ;
345- yield * h . phase ( "failed" ) ;
350+ const failed = yield * h . phase ( "failed" ) ;
346351 assert . deepEqual ( h . changed , [ ] ) ;
347352 assert . isTrue ( h . killed ( ) ) ;
353+ assert . strictEqual (
354+ failed . message ,
355+ "The provider could not create a session after terminal sign-in. Authentication required" ,
356+ ) ;
357+ } ) . pipe ( Effect . scoped , Effect . provide ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) ) ,
358+ ) ;
359+
360+ it . effect ( "surfaces the agent error message when authenticate fails with AcpRequestError" , ( ) =>
361+ Effect . gen ( function * ( ) {
362+ const error = new AcpRequestError ( {
363+ method : "authenticate" ,
364+ code : - 32000 ,
365+ errorMessage : "Connect to the VPN, then sign in again." ,
366+ } ) ;
367+ const h = yield * makeHarness (
368+ browserMethod ,
369+ false ,
370+ decodeSettings ( { agentId : "test-agent" } ) ,
371+ instanceId ,
372+ error ,
373+ ) ;
374+ yield * h . controller . start ( "owner" , Effect . void , "browser" ) ;
375+ const failed = yield * h . phase ( "failed" ) ;
376+ assert . strictEqual (
377+ failed . message ,
378+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again." ,
379+ ) ;
380+ assert . deepEqual ( h . changed , [ ] ) ;
348381 } ) . pipe ( Effect . scoped , Effect . provide ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) ) ,
349382) ;
383+
384+ it . effect (
385+ "surfaces the agent error message when post-sign-in verification fails for agent auth" ,
386+ ( ) =>
387+ Effect . gen ( function * ( ) {
388+ const h = yield * makeHarness ( browserMethod , true ) ;
389+ const flow = yield * h . controller . start ( "owner" , Effect . void , "browser" ) ;
390+ yield * h . phase ( "waiting" ) ;
391+ yield * h . controller . respond ! ( "owner" , {
392+ instanceId,
393+ flowId : flow . flowId ! ,
394+ interactionId : "consent" ,
395+ response : { type : "browser" , action : "accept" } ,
396+ } ) ;
397+ yield * h . phase ( "verifying" ) ;
398+ yield * Deferred . succeed ( h . verify , undefined ) ;
399+ const failed = yield * h . phase ( "failed" ) ;
400+ assert . strictEqual (
401+ failed . message ,
402+ "The provider could not create a session after sign-in. Authentication required" ,
403+ ) ;
404+ assert . deepEqual ( h . changed , [ ] ) ;
405+ } ) . pipe ( Effect . scoped , Effect . provide ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) ) ,
406+ ) ;
407+
408+ it ( "formatFailureDetail handles all cause types and formatting edge cases cleanly" , ( ) => {
409+ const baseWithPeriod = "The ACP agent could not complete sign-in." ;
410+ const baseWithoutPeriod = "The ACP agent could not complete sign-in" ;
411+
412+ // 1. undefined or null cause
413+ assert . strictEqual ( formatFailureDetail ( baseWithPeriod , undefined ) , baseWithPeriod ) ;
414+ assert . strictEqual ( formatFailureDetail ( baseWithPeriod , null ) , baseWithPeriod ) ;
415+
416+ // 2. plain Error object, string, number, or object defect
417+ assert . strictEqual (
418+ formatFailureDetail ( baseWithPeriod , new Error ( "Something broke" ) ) ,
419+ baseWithPeriod ,
420+ ) ;
421+ assert . strictEqual ( formatFailureDetail ( baseWithPeriod , "string error" ) , baseWithPeriod ) ;
422+ assert . strictEqual ( formatFailureDetail ( baseWithPeriod , { foo : "bar" } ) , baseWithPeriod ) ;
423+
424+ // 3. AcpRequestError with empty or whitespace-only errorMessage
425+ const emptyError = new AcpRequestError ( {
426+ method : "authenticate" ,
427+ code : - 32000 ,
428+ errorMessage : "" ,
429+ } ) ;
430+ const whitespaceError = new AcpRequestError ( {
431+ method : "authenticate" ,
432+ code : - 32000 ,
433+ errorMessage : " \t\n " ,
434+ } ) ;
435+ assert . strictEqual ( formatFailureDetail ( baseWithPeriod , emptyError ) , baseWithPeriod ) ;
436+ assert . strictEqual ( formatFailureDetail ( baseWithPeriod , whitespaceError ) , baseWithPeriod ) ;
437+
438+ // 4. AcpRequestError with valid message: ensure no double periods, clean separation
439+ const validError = new AcpRequestError ( {
440+ method : "authenticate" ,
441+ code : - 32000 ,
442+ errorMessage : "Connect to the VPN, then sign in again." ,
443+ } ) ;
444+ assert . strictEqual (
445+ formatFailureDetail ( baseWithPeriod , validError ) ,
446+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again." ,
447+ ) ;
448+ assert . strictEqual (
449+ formatFailureDetail ( baseWithoutPeriod , validError ) ,
450+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again." ,
451+ ) ;
452+
453+ // 5. AcpRequestError with trimmed message and message without ending period
454+ const unpunctError = new AcpRequestError ( {
455+ method : "authenticate" ,
456+ code : - 32000 ,
457+ errorMessage : " Invalid credentials " ,
458+ } ) ;
459+ assert . strictEqual (
460+ formatFailureDetail ( baseWithPeriod , unpunctError ) ,
461+ "The ACP agent could not complete sign-in. Invalid credentials" ,
462+ ) ;
463+
464+ // 6. Nested Cause (e.g. Cause.fail or Cause.die wrapping AcpRequestError)
465+ assert . strictEqual (
466+ formatFailureDetail ( baseWithPeriod , Cause . fail ( validError ) ) ,
467+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again." ,
468+ ) ;
469+ assert . strictEqual (
470+ formatFailureDetail ( baseWithPeriod , Cause . die ( validError ) ) ,
471+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again." ,
472+ ) ;
473+
474+ // 7. Extremely long error message: truncates to 500 characters with ellipsis
475+ const longText = "A" . repeat ( 600 ) ;
476+ const longError = new AcpRequestError ( {
477+ method : "authenticate" ,
478+ code : - 32000 ,
479+ errorMessage : longText ,
480+ } ) ;
481+ const expectedLong = `The ACP agent could not complete sign-in. ${ "A" . repeat ( 500 ) } …` ;
482+ assert . strictEqual ( formatFailureDetail ( baseWithPeriod , longError ) , expectedLong ) ;
483+ } ) ;
0 commit comments