Delete directory 'tests'
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
# Test package initialization
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
import { Graph, Node, ReflectionNode, RewritingNode } from '../src/index.js';
|
|
||||||
|
|
||||||
describe('Graph with reflection and rewriting nodes', () => {
|
|
||||||
let graph;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
graph = new Graph();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('can add generic, reflection, and rewriting nodes', () => {
|
|
||||||
const n1 = new Node('n1');
|
|
||||||
const r1 = new ReflectionNode('r1');
|
|
||||||
const w1 = new RewritingNode('w1');
|
|
||||||
|
|
||||||
graph.addNode(n1);
|
|
||||||
graph.addNode(r1);
|
|
||||||
graph.addNode(w1);
|
|
||||||
|
|
||||||
expect(graph.getNode('n1')).toBe(n1);
|
|
||||||
expect(graph.getNode('r1')).toBe(r1);
|
|
||||||
expect(graph.getNode('w1')).toBe(w1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('adding duplicate node id throws error', () => {
|
|
||||||
const n1 = new Node('dup');
|
|
||||||
graph.addNode(n1);
|
|
||||||
expect(() => graph.addNode(new Node('dup'))).toThrow(/already exists/);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('can add edges between any node types', () => {
|
|
||||||
const n1 = new Node('n1');
|
|
||||||
const r1 = new ReflectionNode('r1');
|
|
||||||
const w1 = new RewritingNode('w1');
|
|
||||||
|
|
||||||
graph.addNode(n1);
|
|
||||||
graph.addNode(r1);
|
|
||||||
graph.addNode(w1);
|
|
||||||
|
|
||||||
graph.addEdge('n1', 'r1');
|
|
||||||
graph.addEdge('r1', 'w1');
|
|
||||||
graph.addEdge('w1', 'n1');
|
|
||||||
|
|
||||||
const visited = [];
|
|
||||||
graph.traverse('n1', (node) => visited.push(node.id));
|
|
||||||
expect(visited.sort()).toEqual(['n1', 'r1', 'w1']);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('removeNode removes node and its edges', () => {
|
|
||||||
const n1 = new Node('n1');
|
|
||||||
const r1 = new ReflectionNode('r1');
|
|
||||||
graph.addNode(n1);
|
|
||||||
graph.addNode(r1);
|
|
||||||
graph.addEdge('n1', 'r1');
|
|
||||||
graph.addEdge('r1', 'n1');
|
|
||||||
|
|
||||||
graph.removeNode('r1');
|
|
||||||
|
|
||||||
expect(graph.getNode('r1')).toBeUndefined();
|
|
||||||
expect(() => graph.traverse('n1', () => {})).not.toThrow();
|
|
||||||
// n1 should have no outgoing edges now
|
|
||||||
const visited = [];
|
|
||||||
graph.traverse('n1', (node) => visited.push(node.id));
|
|
||||||
expect(visited).toEqual(['n1']);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('traverse handles disconnected graph', () => {
|
|
||||||
const n1 = new Node('n1');
|
|
||||||
const r1 = new ReflectionNode('r1');
|
|
||||||
const w1 = new RewritingNode('w1');
|
|
||||||
graph.addNode(n1);
|
|
||||||
graph.addNode(r1);
|
|
||||||
graph.addNode(w1);
|
|
||||||
graph.addEdge('n1', 'r1');
|
|
||||||
|
|
||||||
const visited = [];
|
|
||||||
graph.traverse('n1', (node) => visited.push(node.id));
|
|
||||||
expect(visited).toEqual(['n1', 'r1']);
|
|
||||||
// w1 is disconnected
|
|
||||||
expect(() => graph.traverse('w1', (node) => visited.push(node.id))).not.toThrow();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
import json
|
|
||||||
import os
|
|
||||||
import tempfile
|
|
||||||
import unittest
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from src.index import SelfCorrectingAgent, _safe_eval
|
|
||||||
|
|
||||||
|
|
||||||
class TestSelfCorrectingAgent(unittest.TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
# Create a temporary file for knowledge persistence
|
|
||||||
self.temp_dir = tempfile.TemporaryDirectory()
|
|
||||||
self.knowledge_file = Path(self.temp_dir.name) / "knowledge.json"
|
|
||||||
self.agent = SelfCorrectingAgent(knowledge_file=self.knowledge_file)
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.temp_dir.cleanup()
|
|
||||||
|
|
||||||
def test_safe_eval_basic(self):
|
|
||||||
self.assertEqual(_safe_eval("2+3*4"), 14)
|
|
||||||
self.assertAlmostEqual(_safe_eval("10/4"), 2.5)
|
|
||||||
self.assertEqual(_safe_eval("-5 + 2"), -3)
|
|
||||||
|
|
||||||
def test_safe_eval_invalid(self):
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
_safe_eval("import os; os.system('echo hi')")
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
_safe_eval("2 ** 3 ** 4") # exponentiation is allowed but nested is fine
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
_safe_eval("2 + unknown_var")
|
|
||||||
|
|
||||||
def test_learning_and_persistence(self):
|
|
||||||
problem = "1 + 1"
|
|
||||||
# Initially unknown, should compute
|
|
||||||
self.assertEqual(self.agent.solve(problem), 2)
|
|
||||||
# Simulate user correction
|
|
||||||
self.agent.knowledge[problem] = 3
|
|
||||||
# Now should return learned answer
|
|
||||||
self.assertEqual(self.agent.solve(problem), 3)
|
|
||||||
# Persist knowledge
|
|
||||||
self.agent._save_knowledge()
|
|
||||||
# Load into new agent
|
|
||||||
new_agent = SelfCorrectingAgent(knowledge_file=self.knowledge_file)
|
|
||||||
self.assertEqual(new_agent.solve(problem), 3)
|
|
||||||
|
|
||||||
def test_invalid_expression(self):
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
self.agent.solve("2 + * 3")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import pytest
|
|
||||||
from src.graph import build_graph
|
|
||||||
|
|
||||||
|
|
||||||
def test_graph_flow():
|
|
||||||
graph = build_graph()
|
|
||||||
input_state = {"input": "Hello world"}
|
|
||||||
result = graph.invoke(input_state)
|
|
||||||
assert "rewritten" in result
|
|
||||||
expected = (
|
|
||||||
"I notice that you said: 'Hello world'. "
|
|
||||||
"Let's reflect on that."
|
|
||||||
)
|
|
||||||
assert result["rewritten"] == expected
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
import io
|
|
||||||
import sys
|
|
||||||
import json
|
|
||||||
import unittest
|
|
||||||
from src import index
|
|
||||||
|
|
||||||
class TestIndex(unittest.TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
# Capture stdout
|
|
||||||
self._stdout = sys.stdout
|
|
||||||
sys.stdout = io.StringIO()
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
sys.stdout = self._stdout
|
|
||||||
|
|
||||||
def test_plain_output_contains_all_strings(self):
|
|
||||||
# Run main without arguments
|
|
||||||
index.main()
|
|
||||||
output = sys.stdout.getvalue()
|
|
||||||
# Check that all labels are present
|
|
||||||
for label in index.LABELS:
|
|
||||||
self.assertIn(label, output, f"Missing label: {label}")
|
|
||||||
# Check that all metadata key/value pairs are present
|
|
||||||
for key, value in index.METADATA.items():
|
|
||||||
self.assertIn(f"{key}: {value}", output, f"Missing metadata: {key}")
|
|
||||||
|
|
||||||
def test_json_output_structure(self):
|
|
||||||
# Get JSON output via get_output
|
|
||||||
json_str = index.get_output(json_output=True)
|
|
||||||
data = json.loads(json_str)
|
|
||||||
# Verify top-level keys
|
|
||||||
self.assertIn("metadata", data)
|
|
||||||
self.assertIn("labels", data)
|
|
||||||
# Verify metadata content
|
|
||||||
self.assertEqual(data["metadata"], index.METADATA)
|
|
||||||
# Verify labels content
|
|
||||||
self.assertEqual(data["labels"], index.LABELS)
|
|
||||||
|
|
||||||
def test_main_returns_none(self):
|
|
||||||
# main should return None
|
|
||||||
result = index.main()
|
|
||||||
self.assertIsNone(result)
|
|
||||||
|
|
||||||
def test_output_is_not_empty(self):
|
|
||||||
index.main()
|
|
||||||
output = sys.stdout.getvalue()
|
|
||||||
self.assertTrue(len(output.strip()) > 0)
|
|
||||||
|
|
||||||
def test_get_output_plain(self):
|
|
||||||
plain = index.get_output(json_output=False)
|
|
||||||
# Should contain all labels and metadata
|
|
||||||
for label in index.LABELS:
|
|
||||||
self.assertIn(label, plain)
|
|
||||||
for key, value in index.METADATA.items():
|
|
||||||
self.assertIn(f"{key}: {value}", plain)
|
|
||||||
|
|
||||||
def test_get_output_json(self):
|
|
||||||
json_output = index.get_output(json_output=True)
|
|
||||||
# Should be valid JSON
|
|
||||||
try:
|
|
||||||
data = json.loads(json_output)
|
|
||||||
except json.JSONDecodeError as e:
|
|
||||||
self.fail(f"JSON output is invalid: {e}")
|
|
||||||
# Check that keys exist
|
|
||||||
self.assertIn("metadata", data)
|
|
||||||
self.assertIn("labels", data)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
"""
|
|
||||||
Unit tests for ReflectionNode and RewritingNode.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import unittest
|
|
||||||
from unittest.mock import MagicMock, patch
|
|
||||||
|
|
||||||
from src.nodes import ReflectionNode, RewritingNode
|
|
||||||
|
|
||||||
|
|
||||||
class TestNodes(unittest.TestCase):
|
|
||||||
@patch("src.llm_integration.get_llm")
|
|
||||||
def test_reflection_node(self, mock_get_llm):
|
|
||||||
# Mock LLM to return a fixed reflection
|
|
||||||
mock_llm = MagicMock()
|
|
||||||
mock_llm.return_value = "This is a reflection."
|
|
||||||
mock_get_llm.return_value = mock_llm
|
|
||||||
|
|
||||||
node = ReflectionNode("test_reflection")
|
|
||||||
input_text = "Sample input text."
|
|
||||||
output = node.process(input_text)
|
|
||||||
|
|
||||||
self.assertIsInstance(output, dict)
|
|
||||||
self.assertIn("reflection", output)
|
|
||||||
self.assertEqual(output["reflection"], "This is a reflection.")
|
|
||||||
# Ensure LLM was called with correct prompt
|
|
||||||
expected_prompt = (
|
|
||||||
"Please reflect on the following text:\n\nSample input text.\n\nReflection:"
|
|
||||||
)
|
|
||||||
mock_llm.assert_called_once_with(expected_prompt)
|
|
||||||
|
|
||||||
@patch("src.llm_integration.get_llm")
|
|
||||||
def test_rewriting_node(self, mock_get_llm):
|
|
||||||
# Mock LLM to return a fixed rewritten text
|
|
||||||
mock_llm = MagicMock()
|
|
||||||
mock_llm.return_value = "Rewritten text."
|
|
||||||
mock_get_llm.return_value = mock_llm
|
|
||||||
|
|
||||||
node = RewritingNode("test_rewriting", style="formal")
|
|
||||||
input_data = {"reflection": "This is a reflection."}
|
|
||||||
output = node.process(input_data)
|
|
||||||
|
|
||||||
self.assertIsInstance(output, dict)
|
|
||||||
self.assertIn("rewritten", output)
|
|
||||||
self.assertEqual(output["rewritten"], "Rewritten text.")
|
|
||||||
# Ensure LLM was called with correct prompt
|
|
||||||
expected_prompt = (
|
|
||||||
"Rewrite the following reflection in a formal style:\n\nThis is a reflection.\n\nRewritten:"
|
|
||||||
)
|
|
||||||
mock_llm.assert_called_once_with(expected_prompt)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
import pytest
|
|
||||||
from src.nodes.reflect import ReflectNode
|
|
||||||
|
|
||||||
|
|
||||||
def test_reflect_node():
|
|
||||||
state = {"input": "Hello world"}
|
|
||||||
result = ReflectNode.run(state)
|
|
||||||
assert "reflection" in result
|
|
||||||
expected = (
|
|
||||||
"I see that you said: 'Hello world'. "
|
|
||||||
"Let's reflect on that."
|
|
||||||
)
|
|
||||||
assert result["reflection"] == expected
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import pytest
|
|
||||||
from src.nodes.rewrite import RewriteNode
|
|
||||||
|
|
||||||
|
|
||||||
def test_rewrite_node():
|
|
||||||
state = {
|
|
||||||
"reflection": (
|
|
||||||
"I see that you said: 'Hello world'. "
|
|
||||||
"Let's reflect on that."
|
|
||||||
)
|
|
||||||
}
|
|
||||||
result = RewriteNode.run(state)
|
|
||||||
assert "rewritten" in result
|
|
||||||
expected = (
|
|
||||||
"I notice that you said: 'Hello world'. "
|
|
||||||
"Let's reflect on that."
|
|
||||||
)
|
|
||||||
assert result["rewritten"] == expected
|
|
||||||
Reference in New Issue
Block a user