Step 245: HeadlessEditorState — agent API surface without ImGui/SDL
Adds the headless agent architecture for Sprint 9 Phase 9a: - ASTUtils.h: pure AST utilities extracted from EditorUtils.h - HeadlessEditorState.h: GUI-free state with buffer management - HeadlessAgentRPCHandler.h: full RPC dispatch (20+ methods) - step245_test.cpp: 20 tests all passing Also fixes test compilation errors (NotificationSystem API changes, AgentRole permissions, DependencyPanel missing include) and adds SDL2 system library detection fix in CMakeLists.txt. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// Step 125 Integration Test: EditorState agent mutation flow
|
||||
#include "EditorUtils.h"
|
||||
#include "AgentPermissionPolicy.h"
|
||||
#include "imgui.h"
|
||||
#include <iostream>
|
||||
|
||||
@@ -32,7 +33,7 @@ int main() {
|
||||
state.bufferStates[buf->path] = std::move(buf);
|
||||
state.activeBuffer = state.bufferStates["(untitled)"].get();
|
||||
|
||||
state.agentMutationPermissions["agent_1"] = false;
|
||||
state.setAgentRole("agent_1", AgentRole::Linter); // Linter cannot mutate
|
||||
json mutReq = {
|
||||
{"jsonrpc","2.0"},
|
||||
{"id",1},
|
||||
@@ -42,7 +43,7 @@ int main() {
|
||||
json mutRes = state.processAgentRequest(mutReq, "agent_1");
|
||||
expect(mutRes.contains("error"), "mutation blocked", passed, failed);
|
||||
|
||||
state.agentMutationPermissions["agent_1"] = true;
|
||||
state.setAgentRole("agent_1", AgentRole::Refactor); // Refactor can mutate
|
||||
json mutRes2 = state.processAgentRequest(mutReq, "agent_1");
|
||||
expect(mutRes2.contains("result"), "mutation allowed", passed, failed);
|
||||
Module* astAfter = state.activeAST();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// Step 129 TDD Test: Dependency management UI writeback
|
||||
#include "DependencyPanel.h"
|
||||
#include "NotificationSystem.h"
|
||||
#include "VulnerabilityDatabase.h"
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
@@ -31,6 +33,9 @@ int main() {
|
||||
std::filesystem::remove_all(dir);
|
||||
std::filesystem::create_directories(dir);
|
||||
|
||||
NotificationSystem notifications;
|
||||
VulnerabilityDatabase vulnDb;
|
||||
|
||||
// requirements.txt writeback
|
||||
{
|
||||
std::ofstream out(dir / "requirements.txt");
|
||||
@@ -38,8 +43,7 @@ int main() {
|
||||
out.close();
|
||||
|
||||
DependencyPanelState panel;
|
||||
std::string log;
|
||||
refreshDependencies(panel, dir.string(), log);
|
||||
refreshDependencies(panel, dir.string(), notifications, vulnDb);
|
||||
expect(panel.deps.size() == 1, "requirements parsed", passed, failed);
|
||||
|
||||
DependencySpec dep;
|
||||
@@ -47,7 +51,7 @@ int main() {
|
||||
dep.version = "2.0.0";
|
||||
dep.source = "requirements.txt";
|
||||
panel.deps.push_back(dep);
|
||||
bool ok = writeDependenciesForSource("requirements.txt", dir.string(), panel.deps, log);
|
||||
bool ok = writeDependenciesForSource("requirements.txt", dir.string(), panel.deps, notifications);
|
||||
expect(ok, "requirements writeback ok", passed, failed);
|
||||
std::string content = readFile(dir / "requirements.txt");
|
||||
expect(content.find("numpy==1.26.0") != std::string::npos, "requirements keep numpy", passed, failed);
|
||||
@@ -62,8 +66,7 @@ int main() {
|
||||
out.close();
|
||||
|
||||
DependencyPanelState panel;
|
||||
std::string log;
|
||||
refreshDependencies(panel, dir.string(), log);
|
||||
refreshDependencies(panel, dir.string(), notifications, vulnDb);
|
||||
|
||||
for (auto it = panel.deps.begin(); it != panel.deps.end(); ) {
|
||||
if (it->name == "react") {
|
||||
@@ -75,7 +78,7 @@ int main() {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
bool ok = writeDependenciesForSource("package.json", dir.string(), panel.deps, log);
|
||||
bool ok = writeDependenciesForSource("package.json", dir.string(), panel.deps, notifications);
|
||||
expect(ok, "package.json writeback ok", passed, failed);
|
||||
|
||||
nlohmann::json j;
|
||||
@@ -90,10 +93,9 @@ int main() {
|
||||
|
||||
// edge case: unsupported writeback
|
||||
{
|
||||
std::string log;
|
||||
std::vector<DependencySpec> deps;
|
||||
deps.push_back({"Boost", "", "CMakeLists.txt"});
|
||||
bool ok = writeDependenciesForSource("CMakeLists.txt", dir.string(), deps, log);
|
||||
bool ok = writeDependenciesForSource("CMakeLists.txt", dir.string(), deps, notifications);
|
||||
expect(!ok, "unsupported writeback returns false", passed, failed);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Step 142 TDD Test: Emacs package browser
|
||||
#include "EmacsPackageBrowser.h"
|
||||
#include "EmacsIntegration.h"
|
||||
#include "NotificationSystem.h"
|
||||
#include <iostream>
|
||||
|
||||
static void expect(bool cond, const std::string& name, int& passed, int& failed) {
|
||||
@@ -27,9 +28,9 @@ int main() {
|
||||
|
||||
MockEmacsConnection mock;
|
||||
EmacsPackageBrowserState state;
|
||||
std::string log;
|
||||
NotificationSystem notifications;
|
||||
|
||||
refreshEmacsPackages(state, mock, log);
|
||||
refreshEmacsPackages(state, mock, notifications);
|
||||
expect(!state.packages.empty(), "packages loaded", passed, failed);
|
||||
|
||||
const EmacsPackageEntry* usePkg = findPkg(state.packages, "use-package");
|
||||
@@ -44,7 +45,7 @@ int main() {
|
||||
expect(magitPkg->status == "available", "available status set", passed, failed);
|
||||
}
|
||||
|
||||
bool loadOk = loadEmacsPackage(mock, "magit", log);
|
||||
bool loadOk = loadEmacsPackage(mock, "magit", notifications);
|
||||
expect(loadOk, "load package command", passed, failed);
|
||||
expect(mock.getLastSentCommand().find("(require 'magit)") != std::string::npos,
|
||||
"require command sent", passed, failed);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Step 143 TDD Test: Elisp function discovery and indexing
|
||||
#include "EmacsFunctionDiscovery.h"
|
||||
#include "NotificationSystem.h"
|
||||
#include <iostream>
|
||||
|
||||
static void expect(bool cond, const std::string& name, int& passed, int& failed) {
|
||||
@@ -17,21 +18,21 @@ int main() {
|
||||
int failed = 0;
|
||||
|
||||
MockEmacsConnection mock;
|
||||
std::string log;
|
||||
NotificationSystem notifications;
|
||||
std::string error;
|
||||
|
||||
auto packages = queryEmacsPackageList(mock, false, log, error);
|
||||
auto packages = queryEmacsPackageList(mock, false, notifications, error);
|
||||
expect(!packages.empty(), "loaded packages fetched", passed, failed);
|
||||
|
||||
auto funcs = queryEmacsFunctions(mock, "^use-package", log);
|
||||
auto funcs = queryEmacsFunctions(mock, "^use-package", notifications);
|
||||
expect(!funcs.empty(), "apropos returns functions", passed, failed);
|
||||
|
||||
auto doc = queryEmacsFunctionDoc(mock, "use-package", log);
|
||||
auto doc = queryEmacsFunctionDoc(mock, "use-package", notifications);
|
||||
expect(!doc.signature.empty(), "describe-function signature", passed, failed);
|
||||
expect(!doc.doc.empty(), "describe-function docstring", passed, failed);
|
||||
|
||||
EmacsFunctionIndex index;
|
||||
refreshEmacsFunctionIndex(index, mock, packages, log);
|
||||
refreshEmacsFunctionIndex(index, mock, packages, notifications);
|
||||
expect(index.functionsByPackage.count("use-package") > 0, "index contains use-package", passed, failed);
|
||||
|
||||
Module module("mod1", "test", "elisp");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Step 144 TDD Test: Emacs keybinding deep integration
|
||||
#include "EmacsKeybinding.h"
|
||||
#include "NotificationSystem.h"
|
||||
#include <iostream>
|
||||
|
||||
static void expect(bool cond, const std::string& name, int& passed, int& failed) {
|
||||
@@ -18,29 +19,29 @@ int main() {
|
||||
|
||||
MockEmacsConnection mock;
|
||||
EmacsKeybindingState state;
|
||||
std::string log;
|
||||
NotificationSystem notifications;
|
||||
|
||||
bool prefixOk = emacsHandleKeySequence(state, mock, "C-x", log);
|
||||
bool prefixOk = emacsHandleKeySequence(state, mock, "C-x", notifications);
|
||||
expect(prefixOk, "prefix handled", passed, failed);
|
||||
expect(state.prefix == "C-x", "prefix stored", passed, failed);
|
||||
|
||||
bool seqOk = emacsHandleKeySequence(state, mock, "C-s", log);
|
||||
bool seqOk = emacsHandleKeySequence(state, mock, "C-s", notifications);
|
||||
expect(seqOk, "sequence handled", passed, failed);
|
||||
expect(state.prefix.empty(), "prefix cleared", passed, failed);
|
||||
expect(state.lastCommand == "save-buffer", "command resolved", passed, failed);
|
||||
expect(mock.getLastSentCommand().find("call-interactively") != std::string::npos,
|
||||
"call-interactively sent", passed, failed);
|
||||
|
||||
bool mxOk = emacsHandleKeySequence(state, mock, "M-x", log);
|
||||
bool mxOk = emacsHandleKeySequence(state, mock, "M-x", notifications);
|
||||
expect(mxOk, "M-x handled", passed, failed);
|
||||
expect(state.minibufferActive, "minibuffer active", passed, failed);
|
||||
|
||||
std::snprintf(state.minibufferBuf, sizeof(state.minibufferBuf), "find-file");
|
||||
bool mbOk = emacsExecuteMinibuffer(state, mock, log);
|
||||
bool mbOk = emacsExecuteMinibuffer(state, mock, notifications);
|
||||
expect(mbOk, "minibuffer command executed", passed, failed);
|
||||
expect(!state.minibufferActive, "minibuffer cleared", passed, failed);
|
||||
|
||||
updateEmacsModeLine(state, mock, 2.0, log);
|
||||
updateEmacsModeLine(state, mock, 2.0, notifications);
|
||||
expect(!state.modeLine.empty(), "mode line updated", passed, failed);
|
||||
|
||||
std::cout << "\n=== Step 144 Results: " << passed << " passed, "
|
||||
|
||||
126
editor/tests/step245_test.cpp
Normal file
126
editor/tests/step245_test.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
// Step 245 TDD Test: HeadlessEditorState — agent API without ImGui/SDL
|
||||
#include "HeadlessEditorState.h"
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
static void expect(bool cond, const std::string& name,
|
||||
int& passed, int& failed) {
|
||||
if (cond) {
|
||||
std::cout << "Test " << (passed + failed + 1)
|
||||
<< " PASS: " << name << "\n";
|
||||
++passed;
|
||||
} else {
|
||||
std::cout << "Test " << (passed + failed + 1)
|
||||
<< " FAIL: " << name << "\n";
|
||||
++failed;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
|
||||
// 1. Construct headless state without crash
|
||||
HeadlessEditorState state;
|
||||
state.workspaceRoot = "/tmp/test";
|
||||
state.defaultLanguage = "python";
|
||||
expect(state.active() == nullptr, "no active buffer initially",
|
||||
passed, failed);
|
||||
|
||||
// 2. Open a buffer with Python source
|
||||
std::string src = "def hello(name):\n return 'hi ' + name\n";
|
||||
auto* buf = state.openBuffer("test.py", src, "python");
|
||||
expect(buf != nullptr, "buffer opened", passed, failed);
|
||||
expect(state.active() == buf, "buffer is active", passed, failed);
|
||||
expect(state.isStructured(), "buffer is structured", passed, failed);
|
||||
expect(state.activeAST() != nullptr, "AST parsed", passed, failed);
|
||||
|
||||
// 3. Parse source via RPC
|
||||
json parseReq = {
|
||||
{"jsonrpc", "2.0"}, {"id", 1}, {"method", "parseSource"},
|
||||
{"params", {{"source", "x = 42"}, {"language", "python"}}}
|
||||
};
|
||||
// Set role to Refactor so parseSource is permitted
|
||||
state.setAgentRole("test-agent", AgentRole::Refactor);
|
||||
json parseRes = state.processAgentRequest(parseReq, "test-agent");
|
||||
expect(parseRes.contains("result"), "parseSource returns result",
|
||||
passed, failed);
|
||||
expect(parseRes["result"].contains("ast"), "parseSource has AST",
|
||||
passed, failed);
|
||||
|
||||
// 4. Get AST via RPC
|
||||
json getReq = {
|
||||
{"jsonrpc", "2.0"}, {"id", 2}, {"method", "getAST"}
|
||||
};
|
||||
json getRes = state.processAgentRequest(getReq, "test-agent");
|
||||
expect(getRes.contains("result"), "getAST returns result",
|
||||
passed, failed);
|
||||
expect(getRes["result"].contains("ast"), "getAST has ast field",
|
||||
passed, failed);
|
||||
|
||||
// 5. Run pipeline via RPC
|
||||
json pipeReq = {
|
||||
{"jsonrpc", "2.0"}, {"id", 3}, {"method", "runPipeline"},
|
||||
{"params", {{"source", "def foo(): pass"},
|
||||
{"sourceLanguage", "python"},
|
||||
{"targetLanguage", "cpp"}}}
|
||||
};
|
||||
json pipeRes = state.processAgentRequest(pipeReq, "test-agent");
|
||||
expect(pipeRes.contains("result"), "runPipeline returns result",
|
||||
passed, failed);
|
||||
expect(pipeRes["result"].contains("generatedCode"),
|
||||
"pipeline generates code", passed, failed);
|
||||
|
||||
// 6. Mutation via RPC — rename the module
|
||||
Module* ast = state.activeAST();
|
||||
std::string rootId = ast ? ast->id : "";
|
||||
expect(!rootId.empty(), "root node has id", passed, failed);
|
||||
|
||||
json mutReq = {
|
||||
{"jsonrpc", "2.0"}, {"id", 4}, {"method", "applyMutation"},
|
||||
{"params", {{"type", "setProperty"}, {"nodeId", rootId},
|
||||
{"property", "name"}, {"value", "renamed"}}}
|
||||
};
|
||||
json mutRes = state.processAgentRequest(mutReq, "test-agent");
|
||||
expect(mutRes.contains("result"), "mutation returns result",
|
||||
passed, failed);
|
||||
expect(mutRes["result"].value("success", false),
|
||||
"mutation succeeded", passed, failed);
|
||||
|
||||
// 7. Permission check — Linter cannot mutate
|
||||
state.setAgentRole("linter-agent", AgentRole::Linter);
|
||||
json mutReq2 = {
|
||||
{"jsonrpc", "2.0"}, {"id", 5}, {"method", "applyMutation"},
|
||||
{"params", {{"type", "setProperty"}, {"nodeId", rootId},
|
||||
{"property", "name"}, {"value", "blocked"}}}
|
||||
};
|
||||
json mutRes2 = state.processAgentRequest(mutReq2, "linter-agent");
|
||||
expect(mutRes2.contains("error"), "linter mutation blocked",
|
||||
passed, failed);
|
||||
|
||||
// 8. Ping
|
||||
json pingReq = {{"jsonrpc", "2.0"}, {"id", 6}, {"method", "ping"}};
|
||||
json pingRes = state.processAgentRequest(pingReq, "test-agent");
|
||||
expect(pingRes.contains("result"), "ping returns result",
|
||||
passed, failed);
|
||||
|
||||
// 9. getSessionInfo
|
||||
json infoReq = {
|
||||
{"jsonrpc", "2.0"}, {"id", 7}, {"method", "getSessionInfo"}
|
||||
};
|
||||
json infoRes = state.processAgentRequest(infoReq, "test-agent");
|
||||
expect(infoRes.contains("result"), "getSessionInfo returns result",
|
||||
passed, failed);
|
||||
expect(infoRes["result"].value("mode", "") == "headless",
|
||||
"mode is headless", passed, failed);
|
||||
|
||||
// 10. Buffer management — close and reopen
|
||||
state.closeBuffer("test.py");
|
||||
expect(state.active() == nullptr, "buffer closed", passed, failed);
|
||||
expect(!state.isStructured(), "no structured after close",
|
||||
passed, failed);
|
||||
|
||||
std::cout << "\n=== Step 245 Results: " << passed << " passed, "
|
||||
<< failed << " failed ===\n";
|
||||
return failed == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user