tftsr-devops_investigation/tests/unit/settingsStore.test.ts
Shaun Arman 093495a653
Some checks failed
Test / rust-fmt-check (pull_request) Failing after 0s
Test / rust-clippy (pull_request) Failing after 1s
Test / rust-tests (pull_request) Failing after 0s
Test / frontend-typecheck (pull_request) Failing after 16s
Test / frontend-tests (pull_request) Failing after 18s
PR Review Automation / review (pull_request) Failing after 4m13s
feat: full copy from apollo_nxt-trcaa with complete sanitization
Complete backport of all features from apollo_nxt-trcaa repository:
- Three-tier shell execution safety system (Tier 1: auto, Tier 2: approve, Tier 3: deny)
- Ollama function calling with tool use support
- AI provider tool calling auto-detection
- kubectl binary bundling and management
- kubeconfig upload and context management
- Shell approval modal with real-time UI
- MCP protocol HTTP transport with custom headers
- Enhanced security audit logging
- Comprehensive test coverage (275+ tests)
- Updated CI/CD workflows for Gitea Actions
- Complete documentation (ADRs, wiki, release notes)

Sanitization applied to all files:
- Removed all MSI, Motorola, VNXT, Vesta references
- Replaced internal infrastructure references with TFTSR equivalents
- Updated all URLs and API endpoints
- Sanitized commit history references in documentation

Technical changes:
- New modules: shell/classifier, shell/executor, shell/kubectl, shell/kubeconfig
- Enhanced AI providers: ollama.rs, openai.rs with function calling
- New Tauri commands: shell execution, kubeconfig management, tool calling detection
- Database migrations: shell_execution_audit table
- Frontend: ShellApprovalModal, ShellExecution, KubeconfigManager pages
- CI/CD: kubectl bundling, multi-platform builds, Gitea Actions integration

Version: 1.0.8

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-06-05 14:12:43 -05:00

109 lines
3.9 KiB
TypeScript

import { describe, it, expect, beforeEach } from "vitest";
import { useSettingsStore } from "@/stores/settingsStore";
import type { ProviderConfig } from "@/lib/tauriCommands";
const mockProvider: ProviderConfig = {
name: "openai",
api_url: "https://api.openai.com/v1",
api_key: "sk-test-key",
model: "gpt-4o",
};
const DEFAULT_PII_PATTERNS = ["email", "ip_address", "phone", "ssn", "credit_card", "hostname", "password", "api_key"];
describe("Settings Store", () => {
beforeEach(() => {
localStorage.clear();
useSettingsStore.setState({
theme: "dark",
ai_providers: [],
active_provider: undefined,
default_provider: "ollama",
default_model: "llama3.2:3b",
ollama_url: "http://localhost:11434",
pii_enabled_patterns: Object.fromEntries(DEFAULT_PII_PATTERNS.map((id) => [id, true])),
});
});
it("adds a provider", () => {
useSettingsStore.getState().addProvider(mockProvider);
expect(useSettingsStore.getState().ai_providers).toHaveLength(1);
expect(useSettingsStore.getState().ai_providers[0].name).toBe("openai");
});
it("removes a provider", () => {
useSettingsStore.getState().addProvider(mockProvider);
useSettingsStore.getState().removeProvider(0);
expect(useSettingsStore.getState().ai_providers).toHaveLength(0);
});
it("updates a provider", () => {
useSettingsStore.getState().addProvider(mockProvider);
useSettingsStore.getState().updateProvider(0, { ...mockProvider, model: "gpt-4o-mini" });
expect(useSettingsStore.getState().ai_providers[0].model).toBe("gpt-4o-mini");
});
it("toggles theme", () => {
useSettingsStore.getState().setTheme("light");
expect(useSettingsStore.getState().theme).toBe("light");
});
it("does not persist API keys to localStorage", () => {
useSettingsStore.getState().addProvider(mockProvider);
const raw = localStorage.getItem("trcaa-settings");
expect(raw).toBeTruthy();
expect(raw).not.toContain("sk-test-key");
});
});
describe("Settings Store — PII patterns", () => {
beforeEach(() => {
localStorage.clear();
useSettingsStore.setState({
theme: "dark",
ai_providers: [],
active_provider: undefined,
default_provider: "ollama",
default_model: "llama3.2:3b",
ollama_url: "http://localhost:11434",
pii_enabled_patterns: Object.fromEntries(DEFAULT_PII_PATTERNS.map((id) => [id, true])),
});
});
it("initializes all 8 PII patterns as enabled by default", () => {
const patterns = useSettingsStore.getState().pii_enabled_patterns;
for (const id of DEFAULT_PII_PATTERNS) {
expect(patterns[id]).toBe(true);
}
});
it("setPiiPattern disables a single pattern", () => {
useSettingsStore.getState().setPiiPattern("email", false);
expect(useSettingsStore.getState().pii_enabled_patterns["email"]).toBe(false);
});
it("setPiiPattern does not affect other patterns", () => {
useSettingsStore.getState().setPiiPattern("email", false);
for (const id of DEFAULT_PII_PATTERNS.filter((id) => id !== "email")) {
expect(useSettingsStore.getState().pii_enabled_patterns[id]).toBe(true);
}
});
it("setPiiPattern re-enables a disabled pattern", () => {
useSettingsStore.getState().setPiiPattern("ssn", false);
useSettingsStore.getState().setPiiPattern("ssn", true);
expect(useSettingsStore.getState().pii_enabled_patterns["ssn"]).toBe(true);
});
it("pii_enabled_patterns is persisted to localStorage", () => {
useSettingsStore.getState().setPiiPattern("api_key", false);
const raw = localStorage.getItem("trcaa-settings");
expect(raw).toBeTruthy();
// Zustand persist wraps state in { state: {...}, version: ... }
const parsed = JSON.parse(raw!);
const stored = parsed.state ?? parsed;
expect(stored.pii_enabled_patterns.api_key).toBe(false);
expect(stored.pii_enabled_patterns.email).toBe(true);
});
});