11import { describe , expect , test } from "vitest" ;
2- import { approvalMatchesSystemRunRequest } from "./node-invoke-system-run-approval-match.js" ;
3- import { buildSystemRunApprovalEnvBinding } from "./system-run-approval-env-binding.js" ;
2+ import { evaluateSystemRunApprovalMatch } from "./node-invoke-system-run-approval-match.js" ;
3+ import {
4+ buildSystemRunApprovalBindingV1 ,
5+ buildSystemRunApprovalEnvBinding ,
6+ } from "./system-run-approval-binding.js" ;
47
5- describe ( "approvalMatchesSystemRunRequest " , ( ) => {
8+ describe ( "evaluateSystemRunApprovalMatch " , ( ) => {
69 test ( "matches legacy command text when binding fields match" , ( ) => {
7- const result = approvalMatchesSystemRunRequest ( {
10+ const result = evaluateSystemRunApprovalMatch ( {
811 cmdText : "echo SAFE" ,
912 argv : [ "echo" , "SAFE" ] ,
1013 request : {
@@ -20,11 +23,11 @@ describe("approvalMatchesSystemRunRequest", () => {
2023 sessionKey : "session-1" ,
2124 } ,
2225 } ) ;
23- expect ( result ) . toBe ( true ) ;
26+ expect ( result ) . toEqual ( { ok : true } ) ;
2427 } ) ;
2528
2629 test ( "rejects legacy command mismatch" , ( ) => {
27- const result = approvalMatchesSystemRunRequest ( {
30+ const result = evaluateSystemRunApprovalMatch ( {
2831 cmdText : "echo PWNED" ,
2932 argv : [ "echo" , "PWNED" ] ,
3033 request : {
@@ -37,47 +40,65 @@ describe("approvalMatchesSystemRunRequest", () => {
3740 sessionKey : null ,
3841 } ,
3942 } ) ;
40- expect ( result ) . toBe ( false ) ;
43+ expect ( result . ok ) . toBe ( false ) ;
44+ if ( result . ok ) {
45+ throw new Error ( "unreachable" ) ;
46+ }
47+ expect ( result . code ) . toBe ( "APPROVAL_REQUEST_MISMATCH" ) ;
4148 } ) ;
4249
43- test ( "enforces exact argv binding when commandArgv is set " , ( ) => {
44- const result = approvalMatchesSystemRunRequest ( {
50+ test ( "enforces exact argv binding in v1 object " , ( ) => {
51+ const result = evaluateSystemRunApprovalMatch ( {
4552 cmdText : "echo SAFE" ,
4653 argv : [ "echo" , "SAFE" ] ,
4754 request : {
4855 host : "node" ,
4956 command : "echo SAFE" ,
50- commandArgv : [ "echo" , "SAFE" ] ,
57+ systemRunBindingV1 : buildSystemRunApprovalBindingV1 ( {
58+ argv : [ "echo" , "SAFE" ] ,
59+ cwd : null ,
60+ agentId : null ,
61+ sessionKey : null ,
62+ } ) . binding ,
5163 } ,
5264 binding : {
5365 cwd : null ,
5466 agentId : null ,
5567 sessionKey : null ,
5668 } ,
5769 } ) ;
58- expect ( result ) . toBe ( true ) ;
70+ expect ( result ) . toEqual ( { ok : true } ) ;
5971 } ) ;
6072
61- test ( "rejects argv mismatch even when command text matches " , ( ) => {
62- const result = approvalMatchesSystemRunRequest ( {
73+ test ( "rejects argv mismatch in v1 object " , ( ) => {
74+ const result = evaluateSystemRunApprovalMatch ( {
6375 cmdText : "echo SAFE" ,
6476 argv : [ "echo" , "SAFE" ] ,
6577 request : {
6678 host : "node" ,
6779 command : "echo SAFE" ,
68- commandArgv : [ "echo SAFE" ] ,
80+ systemRunBindingV1 : buildSystemRunApprovalBindingV1 ( {
81+ argv : [ "echo SAFE" ] ,
82+ cwd : null ,
83+ agentId : null ,
84+ sessionKey : null ,
85+ } ) . binding ,
6986 } ,
7087 binding : {
7188 cwd : null ,
7289 agentId : null ,
7390 sessionKey : null ,
7491 } ,
7592 } ) ;
76- expect ( result ) . toBe ( false ) ;
93+ expect ( result . ok ) . toBe ( false ) ;
94+ if ( result . ok ) {
95+ throw new Error ( "unreachable" ) ;
96+ }
97+ expect ( result . code ) . toBe ( "APPROVAL_REQUEST_MISMATCH" ) ;
7798 } ) ;
7899
79- test ( "rejects env overrides when approval record lacks env hash " , ( ) => {
80- const result = approvalMatchesSystemRunRequest ( {
100+ test ( "rejects env overrides when approval record lacks env binding " , ( ) => {
101+ const result = evaluateSystemRunApprovalMatch ( {
81102 cmdText : "git diff" ,
82103 argv : [ "git" , "diff" ] ,
83104 request : {
@@ -92,22 +113,26 @@ describe("approvalMatchesSystemRunRequest", () => {
92113 env : { GIT_EXTERNAL_DIFF : "/tmp/pwn.sh" } ,
93114 } ,
94115 } ) ;
95- expect ( result ) . toBe ( false ) ;
116+ expect ( result . ok ) . toBe ( false ) ;
117+ if ( result . ok ) {
118+ throw new Error ( "unreachable" ) ;
119+ }
120+ expect ( result . code ) . toBe ( "APPROVAL_ENV_BINDING_MISSING" ) ;
96121 } ) ;
97122
98123 test ( "accepts matching env hash with reordered keys" , ( ) => {
99- const binding = buildSystemRunApprovalEnvBinding ( {
124+ const envBinding = buildSystemRunApprovalEnvBinding ( {
100125 SAFE_A : "1" ,
101126 SAFE_B : "2" ,
102127 } ) ;
103- const result = approvalMatchesSystemRunRequest ( {
128+ const result = evaluateSystemRunApprovalMatch ( {
104129 cmdText : "git diff" ,
105130 argv : [ "git" , "diff" ] ,
106131 request : {
107132 host : "node" ,
108133 command : "git diff" ,
109134 commandArgv : [ "git" , "diff" ] ,
110- envHash : binding . envHash ,
135+ envHash : envBinding . envHash ,
111136 } ,
112137 binding : {
113138 cwd : null ,
@@ -116,11 +141,11 @@ describe("approvalMatchesSystemRunRequest", () => {
116141 env : { SAFE_B : "2" , SAFE_A : "1" } ,
117142 } ,
118143 } ) ;
119- expect ( result ) . toBe ( true ) ;
144+ expect ( result ) . toEqual ( { ok : true } ) ;
120145 } ) ;
121146
122147 test ( "rejects non-node host requests" , ( ) => {
123- const result = approvalMatchesSystemRunRequest ( {
148+ const result = evaluateSystemRunApprovalMatch ( {
124149 cmdText : "echo SAFE" ,
125150 argv : [ "echo" , "SAFE" ] ,
126151 request : {
@@ -133,6 +158,10 @@ describe("approvalMatchesSystemRunRequest", () => {
133158 sessionKey : null ,
134159 } ,
135160 } ) ;
136- expect ( result ) . toBe ( false ) ;
161+ expect ( result . ok ) . toBe ( false ) ;
162+ if ( result . ok ) {
163+ throw new Error ( "unreachable" ) ;
164+ }
165+ expect ( result . code ) . toBe ( "APPROVAL_REQUEST_MISMATCH" ) ;
137166 } ) ;
138167} ) ;
0 commit comments