From dcacfd6eab249d2242be9810567c716d58fffb5a Mon Sep 17 00:00:00 2001 From: MohamedAfrash Date: Tue, 13 Oct 2026 16:44:26 +0100 Subject: [PATCH] test: reproduce Antigravity opt-out and Stop workspace regressions RED: enabled-to-declined reinstalls retain executable hooks for both targets, including legacy bridges; Stop diagnostics inspect launch cwd instead of payload workspace. Add current/legacy/re-enable/fresh consent controls and disposable workspace diagnostics. --- tests/lib/antigravity-hook-consent.test.js | 102 ++++++++++++++++++ tests/scripts/antigravity-hook-policy.test.js | 35 +++++- 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 tests/lib/antigravity-hook-consent.test.js diff --git a/tests/lib/antigravity-hook-consent.test.js b/tests/lib/antigravity-hook-consent.test.js new file mode 100644 index 000000000..97570ac5d --- /dev/null +++ b/tests/lib/antigravity-hook-consent.test.js @@ -0,0 +1,102 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const { test } = require('node:test'); +const { createManifestInstallPlan } = require('../../scripts/lib/install/plan'); +const { applyInstallPlan } = require('../../scripts/lib/install/apply'); +const { withHookConsent } = require('../../scripts/lib/install/hook-consent'); + +for (const target of ['antigravity', 'antigravity-home']) { + for (const legacy of [false, true]) { + test(`${target} revokes ${legacy ? 'legacy' : 'current'} hooks and can re-enable them`, () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-hook-consent-')); + try { + const options = { moduleIds: ['hooks-runtime'], target, homeDir: root, projectRoot: root }; + const enabled = withHookConsent(createManifestInstallPlan(options), 'enabled'); + applyInstallPlan(enabled); + const hooksPath = path.join(enabled.targetRoot, 'hooks.json'); + const setupPath = path.join(enabled.targetRoot, 'ecc', 'setup.json'); + const bridgePath = path.join(enabled.targetRoot, 'scripts', 'hooks', 'antigravity-hook-bridge.js'); + const marker = path.join(root, 'hook-ran'); + // Simulate an already installed bridge that predates managed flag support. + if (legacy) { + fs.rmSync(setupPath); + fs.writeFileSync(bridgePath, `require('node:fs').writeFileSync(${JSON.stringify(marker)}, 'ran');`); + } + const config = JSON.parse(fs.readFileSync(hooksPath, 'utf8')); + const userHook = { Stop: [{ type: 'command', command: 'user-selected-hook' }] }; + fs.writeFileSync(hooksPath, JSON.stringify({ ...config, 'user-hook': userHook })); + applyInstallPlan(withHookConsent(createManifestInstallPlan(options), 'declined')); + const declined = JSON.parse(fs.readFileSync(hooksPath, 'utf8')); + assert.deepEqual(declined['user-hook'], userHook); + for (const handler of declined['ecc-guard']?.PostToolUse || []) { + for (const hook of handler.hooks || []) { + const result = spawnSync(hook.command, { + shell: true, cwd: root, encoding: 'utf8', + input: JSON.stringify({ toolCall: { name: 'write_to_file', args: { TargetFile: path.join(root, 'file.js') } } }), + }); + assert.equal(result.status, 0, result.stderr); + } + } + assert.equal(fs.existsSync(marker), false, 'Declined legacy hook must not execute'); + assert.equal(Object.hasOwn(declined, 'ecc-guard'), false); + assert.equal(JSON.parse(fs.readFileSync(setupPath, 'utf8')).hooks.enabled, false); + applyInstallPlan(withHookConsent(createManifestInstallPlan(options), 'declined')); + assert.deepEqual(JSON.parse(fs.readFileSync(hooksPath, 'utf8'))['user-hook'], userHook); + applyInstallPlan(withHookConsent(createManifestInstallPlan(options), 'enabled')); + assert.ok(JSON.parse(fs.readFileSync(hooksPath, 'utf8'))['ecc-guard']); + assert.equal(JSON.parse(fs.readFileSync(setupPath, 'utf8')).hooks.enabled, true); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + } + + for (const unsafe of ['modified-hooks', 'symlinked-setup']) { + test(`${target} refuses ${unsafe} before recording declined consent`, () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-unsafe-revocation-')); + try { + const options = { moduleIds: ['hooks-runtime'], target, homeDir: root, projectRoot: root }; + const enabled = withHookConsent(createManifestInstallPlan(options), 'enabled'); + applyInstallPlan(enabled); + const hooksPath = path.join(enabled.targetRoot, 'hooks.json'); + const setupPath = path.join(enabled.targetRoot, 'ecc', 'setup.json'); + const original = fs.readFileSync(hooksPath, 'utf8'); + if (unsafe === 'modified-hooks') { + const hooks = JSON.parse(original); + fs.writeFileSync(hooksPath, JSON.stringify({ + ...hooks, 'ecc-guard': { ...hooks['ecc-guard'], Stop: [{ command: 'user-customized-hook' }] }, + })); + } else { + fs.rmSync(setupPath); + fs.symlinkSync(path.join(root, 'outside.json'), setupPath); + } + const before = fs.readFileSync(hooksPath, 'utf8'); + assert.throws(() => applyInstallPlan(withHookConsent(createManifestInstallPlan(options), 'declined')), + /modified Antigravity hooks|symlink|outside the install root/i); + assert.equal(fs.readFileSync(hooksPath, 'utf8'), before); + assert.equal(fs.existsSync(path.join(root, 'outside.json')), false); + assert.equal(JSON.parse(fs.readFileSync(enabled.installStatePath, 'utf8')).request.hookConsent, 'enabled'); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + } + + test(`${target} fresh declined installation creates no hook runtime`, () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-fresh-declined-')); + try { + const plan = withHookConsent(createManifestInstallPlan({ moduleIds: ['hooks-runtime'], target, homeDir: root, projectRoot: root }), 'declined'); + applyInstallPlan(plan); + assert.equal(fs.existsSync(path.join(plan.targetRoot, 'hooks.json')), false); + assert.equal(fs.existsSync(path.join(plan.targetRoot, 'ecc', 'setup.json')), false); + assert.equal(fs.existsSync(path.join(plan.targetRoot, 'scripts', 'hooks')), false); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); +} diff --git a/tests/scripts/antigravity-hook-policy.test.js b/tests/scripts/antigravity-hook-policy.test.js index 0bd564ac9..0d791fdf1 100644 --- a/tests/scripts/antigravity-hook-policy.test.js +++ b/tests/scripts/antigravity-hook-policy.test.js @@ -4,7 +4,7 @@ const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); -const { spawnSync } = require('node:child_process'); +const { spawnSync, execFileSync } = require('node:child_process'); const { test } = require('node:test'); const { buildAntigravityHooksConfig } = require('../../scripts/lib/antigravity-hooks'); @@ -109,3 +109,36 @@ test('managed profile is used unless an explicit environment choice overrides it fs.rmSync(root, { recursive: true, force: true }); } }); + +test('Stop diagnostics inspect the payload workspace rather than the launch directory', () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-agy-stop-cwd-')); + try { + const workspace = path.join(root, 'workspace'); + const unrelated = path.join(root, 'unrelated'); + const env = { + ...process.env, ECC_HOOK_PROFILE: 'standard', ECC_HOOKS_ENABLED: 'true', + ECC_DISABLED_HOOKS: '', ECC_DRY_RUN: '0', ECC_HOOK_CONFIG: '', + GIT_CONFIG_GLOBAL: path.join(root, 'no-global-config'), GIT_CONFIG_NOSYSTEM: '1', + }; + for (const [dir, name] of [[workspace, 'intended.js'], [unrelated, 'unrelated.js']]) { + fs.mkdirSync(dir); + execFileSync('git', ['init', '--quiet', dir], { env }); + fs.writeFileSync(path.join(dir, name), 'const value = 1;\n'); + execFileSync('git', ['-C', dir, 'add', name], { env }); + fs.appendFileSync(path.join(dir, name), 'console.log(value);\n'); + } + for (const cwd of [root, unrelated]) { + const child = spawnSync(process.execPath, [bridge, '--mode', 'stop', '--hook', 'stop:check-console-log'], { + cwd, env, encoding: 'utf8', timeout: 15000, + input: JSON.stringify({ workspacePaths: [workspace], fullyIdle: true }), + }); + assert.equal(child.status, 0, child.stderr); + const response = JSON.parse(child.stdout); + assert.equal(response.decision, 'continue'); + assert.match(response.reason, /intended\.js/); + assert.doesNotMatch(response.reason, /unrelated\.js/); + } + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +});