@@ -8,7 +8,11 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
88import * as fs from 'node:fs' ;
99import * as path from 'node:path' ;
1010import * as os from 'node:os' ;
11- import { resolveWorkspacePolicyState } from './policy.js' ;
11+ import {
12+ resolveWorkspacePolicyState ,
13+ autoAcceptWorkspacePolicies ,
14+ setAutoAcceptWorkspacePolicies ,
15+ } from './policy.js' ;
1216import { writeToStderr } from '@google/gemini-cli-core' ;
1317
1418// Mock debugLogger to avoid noise in test output
@@ -68,24 +72,18 @@ describe('resolveWorkspacePolicyState', () => {
6872 fs . mkdirSync ( policiesDir , { recursive : true } ) ;
6973 fs . writeFileSync ( path . join ( policiesDir , 'policy.toml' ) , 'rules = []' ) ;
7074
71- // First call to establish integrity (interactive accept)
75+ // First call to establish integrity (interactive auto- accept)
7276 const firstResult = await resolveWorkspacePolicyState ( {
7377 cwd : workspaceDir ,
7478 trustedFolder : true ,
7579 interactive : true ,
7680 } ) ;
77- expect ( firstResult . policyUpdateConfirmationRequest ) . toBeDefined ( ) ;
78-
79- // Establish integrity manually as if accepted
80- const { PolicyIntegrityManager } = await import ( '@google/gemini-cli-core' ) ;
81- const integrityManager = new PolicyIntegrityManager ( ) ;
82- await integrityManager . acceptIntegrity (
83- 'workspace' ,
84- workspaceDir ,
85- firstResult . policyUpdateConfirmationRequest ! . newHash ,
86- ) ;
81+ expect ( firstResult . workspacePoliciesDir ) . toBe ( policiesDir ) ;
82+ expect ( firstResult . policyUpdateConfirmationRequest ) . toBeUndefined ( ) ;
83+ expect ( writeToStderr ) . not . toHaveBeenCalled ( ) ;
8784
8885 // Second call should match
86+
8987 const result = await resolveWorkspacePolicyState ( {
9088 cwd : workspaceDir ,
9189 trustedFolder : true ,
@@ -107,26 +105,33 @@ describe('resolveWorkspacePolicyState', () => {
107105 expect ( result . policyUpdateConfirmationRequest ) . toBeUndefined ( ) ;
108106 } ) ;
109107
110- it ( 'should return confirmation request if changed in interactive mode' , async ( ) => {
111- fs . mkdirSync ( policiesDir , { recursive : true } ) ;
112- fs . writeFileSync ( path . join ( policiesDir , 'policy.toml' ) , 'rules = []' ) ;
108+ it ( 'should return confirmation request if changed in interactive mode when AUTO_ACCEPT is false ' , async ( ) => {
109+ const originalValue = autoAcceptWorkspacePolicies ;
110+ setAutoAcceptWorkspacePolicies ( false ) ;
113111
114- const result = await resolveWorkspacePolicyState ( {
115- cwd : workspaceDir ,
116- trustedFolder : true ,
117- interactive : true ,
118- } ) ;
112+ try {
113+ fs . mkdirSync ( policiesDir , { recursive : true } ) ;
114+ fs . writeFileSync ( path . join ( policiesDir , 'policy.toml' ) , 'rules = []' ) ;
119115
120- expect ( result . workspacePoliciesDir ) . toBeUndefined ( ) ;
121- expect ( result . policyUpdateConfirmationRequest ) . toEqual ( {
122- scope : 'workspace' ,
123- identifier : workspaceDir ,
124- policyDir : policiesDir ,
125- newHash : expect . any ( String ) ,
126- } ) ;
116+ const result = await resolveWorkspacePolicyState ( {
117+ cwd : workspaceDir ,
118+ trustedFolder : true ,
119+ interactive : true ,
120+ } ) ;
121+
122+ expect ( result . workspacePoliciesDir ) . toBeUndefined ( ) ;
123+ expect ( result . policyUpdateConfirmationRequest ) . toEqual ( {
124+ scope : 'workspace' ,
125+ identifier : workspaceDir ,
126+ policyDir : policiesDir ,
127+ newHash : expect . any ( String ) ,
128+ } ) ;
129+ } finally {
130+ setAutoAcceptWorkspacePolicies ( originalValue ) ;
131+ }
127132 } ) ;
128133
129- it ( 'should warn and auto-accept if changed in non-interactive mode' , async ( ) => {
134+ it ( 'should warn and auto-accept if changed in non-interactive mode when AUTO_ACCEPT is true ' , async ( ) => {
130135 fs . mkdirSync ( policiesDir , { recursive : true } ) ;
131136 fs . writeFileSync ( path . join ( policiesDir , 'policy.toml' ) , 'rules = []' ) ;
132137
@@ -143,6 +148,30 @@ describe('resolveWorkspacePolicyState', () => {
143148 ) ;
144149 } ) ;
145150
151+ it ( 'should warn and auto-accept if changed in non-interactive mode when AUTO_ACCEPT is false' , async ( ) => {
152+ const originalValue = autoAcceptWorkspacePolicies ;
153+ setAutoAcceptWorkspacePolicies ( false ) ;
154+
155+ try {
156+ fs . mkdirSync ( policiesDir , { recursive : true } ) ;
157+ fs . writeFileSync ( path . join ( policiesDir , 'policy.toml' ) , 'rules = []' ) ;
158+
159+ const result = await resolveWorkspacePolicyState ( {
160+ cwd : workspaceDir ,
161+ trustedFolder : true ,
162+ interactive : false ,
163+ } ) ;
164+
165+ expect ( result . workspacePoliciesDir ) . toBe ( policiesDir ) ;
166+ expect ( result . policyUpdateConfirmationRequest ) . toBeUndefined ( ) ;
167+ expect ( writeToStderr ) . toHaveBeenCalledWith (
168+ expect . stringContaining ( 'Automatically accepting and loading' ) ,
169+ ) ;
170+ } finally {
171+ setAutoAcceptWorkspacePolicies ( originalValue ) ;
172+ }
173+ } ) ;
174+
146175 it ( 'should not return workspace policies if cwd is the home directory' , async ( ) => {
147176 const policiesDir = path . join ( tempDir , '.gemini' , 'policies' ) ;
148177 fs . mkdirSync ( policiesDir , { recursive : true } ) ;
0 commit comments