Stabilize prompts snapshot test by properly mocking the SANDBOX env var. (#1067)
Co-authored-by: Marat Boshernitsan <maratb@google.com>
This commit is contained in:
parent
6959663646
commit
6d772a30c0
|
@ -4,10 +4,8 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
import { describe, it, expect, vi } from 'vitest';
|
||||||
import { getCoreSystemPrompt } from './prompts.js'; // Adjust import path
|
import { getCoreSystemPrompt } from './prompts.js';
|
||||||
import * as process from 'node:process';
|
|
||||||
import * as os from 'node:os';
|
|
||||||
import { isGitRepository } from '../utils/gitUtils.js';
|
import { isGitRepository } from '../utils/gitUtils.js';
|
||||||
|
|
||||||
// Mock tool names if they are dynamically generated or complex
|
// Mock tool names if they are dynamically generated or complex
|
||||||
|
@ -25,34 +23,13 @@ vi.mock('../tools/shell', () => ({
|
||||||
vi.mock('../tools/write-file', () => ({
|
vi.mock('../tools/write-file', () => ({
|
||||||
WriteFileTool: { Name: 'write_file' },
|
WriteFileTool: { Name: 'write_file' },
|
||||||
}));
|
}));
|
||||||
vi.mock('node:os', () => ({
|
|
||||||
platform: vi.fn(),
|
|
||||||
}));
|
|
||||||
vi.mock('../utils/gitUtils', () => ({
|
vi.mock('../utils/gitUtils', () => ({
|
||||||
isGitRepository: vi.fn(),
|
isGitRepository: vi.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe('Core System Prompt (prompts.ts)', () => {
|
describe('Core System Prompt (prompts.ts)', () => {
|
||||||
// Store original env vars that we modify
|
|
||||||
let originalSandboxEnv: string | undefined;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
// Store original value before each test
|
|
||||||
originalSandboxEnv = process.env.SANDBOX;
|
|
||||||
vi.mocked(os.platform).mockReturnValue('darwin');
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
// Restore original value after each test
|
|
||||||
if (originalSandboxEnv === undefined) {
|
|
||||||
delete process.env.SANDBOX;
|
|
||||||
} else {
|
|
||||||
process.env.SANDBOX = originalSandboxEnv;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return the base prompt when no userMemory is provided', () => {
|
it('should return the base prompt when no userMemory is provided', () => {
|
||||||
delete process.env.SANDBOX; // Ensure default state for snapshot
|
vi.stubEnv('SANDBOX', undefined);
|
||||||
const prompt = getCoreSystemPrompt();
|
const prompt = getCoreSystemPrompt();
|
||||||
expect(prompt).not.toContain('---\n\n'); // Separator should not be present
|
expect(prompt).not.toContain('---\n\n'); // Separator should not be present
|
||||||
expect(prompt).toContain('You are an interactive CLI agent'); // Check for core content
|
expect(prompt).toContain('You are an interactive CLI agent'); // Check for core content
|
||||||
|
@ -60,7 +37,7 @@ describe('Core System Prompt (prompts.ts)', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the base prompt when userMemory is empty string', () => {
|
it('should return the base prompt when userMemory is empty string', () => {
|
||||||
delete process.env.SANDBOX;
|
vi.stubEnv('SANDBOX', undefined);
|
||||||
const prompt = getCoreSystemPrompt('');
|
const prompt = getCoreSystemPrompt('');
|
||||||
expect(prompt).not.toContain('---\n\n');
|
expect(prompt).not.toContain('---\n\n');
|
||||||
expect(prompt).toContain('You are an interactive CLI agent');
|
expect(prompt).toContain('You are an interactive CLI agent');
|
||||||
|
@ -68,7 +45,7 @@ describe('Core System Prompt (prompts.ts)', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the base prompt when userMemory is whitespace only', () => {
|
it('should return the base prompt when userMemory is whitespace only', () => {
|
||||||
delete process.env.SANDBOX;
|
vi.stubEnv('SANDBOX', undefined);
|
||||||
const prompt = getCoreSystemPrompt(' \n \t ');
|
const prompt = getCoreSystemPrompt(' \n \t ');
|
||||||
expect(prompt).not.toContain('---\n\n');
|
expect(prompt).not.toContain('---\n\n');
|
||||||
expect(prompt).toContain('You are an interactive CLI agent');
|
expect(prompt).toContain('You are an interactive CLI agent');
|
||||||
|
@ -76,7 +53,7 @@ describe('Core System Prompt (prompts.ts)', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should append userMemory with separator when provided', () => {
|
it('should append userMemory with separator when provided', () => {
|
||||||
delete process.env.SANDBOX;
|
vi.stubEnv('SANDBOX', undefined);
|
||||||
const memory = 'This is custom user memory.\nBe extra polite.';
|
const memory = 'This is custom user memory.\nBe extra polite.';
|
||||||
const expectedSuffix = `\n\n---\n\n${memory}`;
|
const expectedSuffix = `\n\n---\n\n${memory}`;
|
||||||
const prompt = getCoreSystemPrompt(memory);
|
const prompt = getCoreSystemPrompt(memory);
|
||||||
|
@ -87,7 +64,7 @@ describe('Core System Prompt (prompts.ts)', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include sandbox-specific instructions when SANDBOX env var is set', () => {
|
it('should include sandbox-specific instructions when SANDBOX env var is set', () => {
|
||||||
process.env.SANDBOX = 'true'; // Generic sandbox value
|
vi.stubEnv('SANDBOX', 'true'); // Generic sandbox value
|
||||||
const prompt = getCoreSystemPrompt();
|
const prompt = getCoreSystemPrompt();
|
||||||
expect(prompt).toContain('# Sandbox');
|
expect(prompt).toContain('# Sandbox');
|
||||||
expect(prompt).not.toContain('# MacOS Seatbelt');
|
expect(prompt).not.toContain('# MacOS Seatbelt');
|
||||||
|
@ -96,7 +73,7 @@ describe('Core System Prompt (prompts.ts)', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include seatbelt-specific instructions when SANDBOX env var is "sandbox-exec"', () => {
|
it('should include seatbelt-specific instructions when SANDBOX env var is "sandbox-exec"', () => {
|
||||||
process.env.SANDBOX = 'sandbox-exec';
|
vi.stubEnv('SANDBOX', 'sandbox-exec');
|
||||||
const prompt = getCoreSystemPrompt();
|
const prompt = getCoreSystemPrompt();
|
||||||
expect(prompt).toContain('# MacOS Seatbelt');
|
expect(prompt).toContain('# MacOS Seatbelt');
|
||||||
expect(prompt).not.toContain('# Sandbox');
|
expect(prompt).not.toContain('# Sandbox');
|
||||||
|
@ -105,7 +82,7 @@ describe('Core System Prompt (prompts.ts)', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include non-sandbox instructions when SANDBOX env var is not set', () => {
|
it('should include non-sandbox instructions when SANDBOX env var is not set', () => {
|
||||||
delete process.env.SANDBOX; // Ensure it's not set
|
vi.stubEnv('SANDBOX', undefined); // Ensure it's not set
|
||||||
const prompt = getCoreSystemPrompt();
|
const prompt = getCoreSystemPrompt();
|
||||||
expect(prompt).toContain('# Outside of Sandbox');
|
expect(prompt).toContain('# Outside of Sandbox');
|
||||||
expect(prompt).not.toContain('# Sandbox');
|
expect(prompt).not.toContain('# Sandbox');
|
||||||
|
@ -114,6 +91,7 @@ describe('Core System Prompt (prompts.ts)', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include git instructions when in a git repo', () => {
|
it('should include git instructions when in a git repo', () => {
|
||||||
|
vi.stubEnv('SANDBOX', undefined);
|
||||||
vi.mocked(isGitRepository).mockReturnValue(true);
|
vi.mocked(isGitRepository).mockReturnValue(true);
|
||||||
const prompt = getCoreSystemPrompt();
|
const prompt = getCoreSystemPrompt();
|
||||||
expect(prompt).toContain('# Git Repository');
|
expect(prompt).toContain('# Git Repository');
|
||||||
|
@ -121,6 +99,7 @@ describe('Core System Prompt (prompts.ts)', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not include git instructions when not in a git repo', () => {
|
it('should not include git instructions when not in a git repo', () => {
|
||||||
|
vi.stubEnv('SANDBOX', undefined);
|
||||||
vi.mocked(isGitRepository).mockReturnValue(false);
|
vi.mocked(isGitRepository).mockReturnValue(false);
|
||||||
const prompt = getCoreSystemPrompt();
|
const prompt = getCoreSystemPrompt();
|
||||||
expect(prompt).not.toContain('# Git Repository');
|
expect(prompt).not.toContain('# Git Repository');
|
||||||
|
|
Loading…
Reference in New Issue