@@ -2,15 +2,76 @@ import assert from 'node:assert/strict';
22import { test } from 'vitest' ;
33import {
44 AppError ,
5+ defaultHintForCode ,
56 discloseDispatch ,
67 discloseDispatchAfterSteps ,
78 normalizeError ,
9+ PUBLISHED_ERROR_REASONS ,
10+ sessionAppRequiredDetails ,
11+ sessionOrDeviceSelectorRequiredDetails ,
812 throwDaemonError ,
913 readElementMatchCandidateRefs ,
1014 readErrorCandidateViews ,
1115 summarizeCommandAttemptFailures ,
1216} from './errors.ts' ;
1317
18+ test ( 'the published reasons are the values consumers branch on' , ( ) => {
19+ assert . deepEqual ( PUBLISHED_ERROR_REASONS , {
20+ sessionAppRequired : 'session_app_required' ,
21+ sessionOrDeviceSelectorRequired : 'session_or_device_selector_required' ,
22+ } ) ;
23+ } ) ;
24+
25+ for ( const [ label , details , code , message ] of [
26+ [
27+ 'sessionAppRequired' ,
28+ sessionAppRequiredDetails ( ) ,
29+ 'INVALID_ARGS' ,
30+ 'permission setting requires an active app in session' ,
31+ ] ,
32+ [
33+ 'sessionOrDeviceSelectorRequired' ,
34+ sessionOrDeviceSelectorRequiredDetails ( ) ,
35+ 'INVALID_ARGS' ,
36+ 'clipboard requires an active session or an explicit device selector (e.g. --platform ios).' ,
37+ ] ,
38+ ] as const ) {
39+ test ( `${ label } refusal carries its reason and dispatched:no through normalize and the wire` , ( ) => {
40+ const normalized = normalizeError ( new AppError ( code , message , details ) ) ;
41+ assert . equal ( normalized . message , message ) ;
42+ assert . equal ( normalized . details ?. reason , details . reason ) ;
43+ assert . equal ( normalized . details ?. dispatched , 'no' ) ;
44+ assert . throws (
45+ ( ) => throwDaemonError ( normalized ) ,
46+ ( error : unknown ) =>
47+ error instanceof AppError &&
48+ error . details ?. reason === details . reason &&
49+ error . details ?. dispatched === 'no' ,
50+ ) ;
51+ } ) ;
52+
53+ test ( `${ label } reason survives a second normalization of the wire shape` , ( ) => {
54+ const once = normalizeError ( new AppError ( code , message , details ) ) ;
55+ const twice = normalizeError ( new AppError ( once . code , once . message , once . details ) ) ;
56+ assert . deepEqual ( twice . details , once . details ) ;
57+ } ) ;
58+ }
59+
60+ test ( 'a published refusal keeps the INVALID_ARGS default hint — the reason carries the extra fact' , ( ) => {
61+ for ( const details of [ sessionAppRequiredDetails ( ) , sessionOrDeviceSelectorRequiredDetails ( ) ] ) {
62+ const normalized = normalizeError ( new AppError ( 'INVALID_ARGS' , 'refused' , details ) ) ;
63+ assert . equal ( normalized . hint , defaultHintForCode ( 'INVALID_ARGS' ) ) ;
64+ }
65+ } ) ;
66+
67+ test ( 'a sibling INVALID_ARGS refusal without a reason cannot activate a reason-driven consumer' , ( ) => {
68+ const unrelated = normalizeError (
69+ new AppError ( 'INVALID_ARGS' , 'clipboard requires a subcommand: read or write' ) ,
70+ ) ;
71+ assert . equal ( unrelated . details ?. reason , undefined ) ;
72+ assert . equal ( 'dispatched' in ( unrelated . details ?? { } ) , false ) ;
73+ } ) ;
74+
1475test ( 'normalizeError retains redacted nested command errors through repeated normalization' , ( ) => {
1576 const stderr = `Launch request denied\n${ 'Context line\n' . repeat ( 50 ) } Underlying reason: token=private-value device locked` ;
1677 const expected = stderr . replace ( 'private-value' , '[REDACTED]' ) ;
0 commit comments