feat: Phase 1 RPC protocol infrastructure for daemon architecture (bd-111)

Implemented Unix socket RPC foundation to enable daemon-based concurrent access:

New files:
- internal/rpc/protocol.go: Request/Response types with 13 operations
- internal/rpc/server.go: Unix socket server with storage adapter
- internal/rpc/client.go: Client with auto-detection and typed methods
- internal/rpc/rpc_test.go: Integration tests

Features:
- JSON-based protocol over Unix sockets
- Adapter pattern for context/actor propagation to storage API
- Ping/health checks for daemon detection
- All core operations: create, update, close, list, show, ready, stats, deps, labels
- Graceful socket cleanup and signal handling
- Concurrent request support

Tests: 49.3% coverage, all passing

Related issues:
- bd-110: Daemon architecture epic
- bd-111: Phase 1 (completed)
- bd-112: Phase 2 (client auto-detection)
- bd-113: Phase 3 (daemon command)
- bd-114: Phase 4 (atomic operations)

Amp-Thread-ID: https://ampcode.com/threads/T-796c62e6-93b6-41c7-9cb5-8acc4a35ba9a
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-16 22:49:19 -07:00
parent b87ef26b22
commit 5c0fac6e17
8 changed files with 1030 additions and 736 deletions

View File

@@ -2,146 +2,169 @@ package rpc
import (
"encoding/json"
"errors"
"testing"
)
func TestNewRequest(t *testing.T) {
args := map[string]string{
"title": "Test issue",
"priority": "1",
func TestRequestSerialization(t *testing.T) {
createArgs := CreateArgs{
Title: "Test Issue",
Description: "Test description",
IssueType: "task",
Priority: 2,
}
req, err := NewRequest(OpCreate, args)
argsJSON, err := json.Marshal(createArgs)
if err != nil {
t.Fatalf("NewRequest failed: %v", err)
t.Fatalf("Failed to marshal args: %v", err)
}
if req.Operation != OpCreate {
t.Errorf("Expected operation %s, got %s", OpCreate, req.Operation)
}
var unmarshaledArgs map[string]string
if err := req.UnmarshalArgs(&unmarshaledArgs); err != nil {
t.Fatalf("UnmarshalArgs failed: %v", err)
}
if unmarshaledArgs["title"] != args["title"] {
t.Errorf("Expected title %s, got %s", args["title"], unmarshaledArgs["title"])
}
}
func TestNewSuccessResponse(t *testing.T) {
data := map[string]interface{}{
"id": "bd-123",
"status": "open",
}
resp, err := NewSuccessResponse(data)
if err != nil {
t.Fatalf("NewSuccessResponse failed: %v", err)
}
if !resp.Success {
t.Error("Expected success=true")
}
if resp.Error != "" {
t.Errorf("Expected empty error, got %s", resp.Error)
}
var unmarshaledData map[string]interface{}
if err := resp.UnmarshalData(&unmarshaledData); err != nil {
t.Fatalf("UnmarshalData failed: %v", err)
}
if unmarshaledData["id"] != data["id"] {
t.Errorf("Expected id %s, got %s", data["id"], unmarshaledData["id"])
}
}
func TestNewErrorResponse(t *testing.T) {
testErr := errors.New("test error")
resp := NewErrorResponse(testErr)
if resp.Success {
t.Error("Expected success=false")
}
if resp.Error != testErr.Error() {
t.Errorf("Expected error %s, got %s", testErr.Error(), resp.Error)
}
if len(resp.Data) != 0 {
t.Errorf("Expected empty data, got %v", resp.Data)
}
}
func TestRequestResponseJSON(t *testing.T) {
req := &Request{
Operation: OpList,
Args: json.RawMessage(`{"status":"open"}`),
req := Request{
Operation: OpCreate,
Args: argsJSON,
}
reqJSON, err := json.Marshal(req)
if err != nil {
t.Fatalf("Marshal request failed: %v", err)
t.Fatalf("Failed to marshal request: %v", err)
}
var unmarshaledReq Request
if err := json.Unmarshal(reqJSON, &unmarshaledReq); err != nil {
t.Fatalf("Unmarshal request failed: %v", err)
var decodedReq Request
if err := json.Unmarshal(reqJSON, &decodedReq); err != nil {
t.Fatalf("Failed to unmarshal request: %v", err)
}
if unmarshaledReq.Operation != req.Operation {
t.Errorf("Expected operation %s, got %s", req.Operation, unmarshaledReq.Operation)
if decodedReq.Operation != OpCreate {
t.Errorf("Expected operation %s, got %s", OpCreate, decodedReq.Operation)
}
resp := &Response{
var decodedArgs CreateArgs
if err := json.Unmarshal(decodedReq.Args, &decodedArgs); err != nil {
t.Fatalf("Failed to unmarshal args: %v", err)
}
if decodedArgs.Title != createArgs.Title {
t.Errorf("Expected title %s, got %s", createArgs.Title, decodedArgs.Title)
}
if decodedArgs.Priority != createArgs.Priority {
t.Errorf("Expected priority %d, got %d", createArgs.Priority, decodedArgs.Priority)
}
}
func TestResponseSerialization(t *testing.T) {
resp := Response{
Success: true,
Data: json.RawMessage(`{"count":5}`),
Data: json.RawMessage(`{"id":"bd-1","title":"Test"}`),
}
respJSON, err := json.Marshal(resp)
if err != nil {
t.Fatalf("Marshal response failed: %v", err)
t.Fatalf("Failed to marshal response: %v", err)
}
var unmarshaledResp Response
if err := json.Unmarshal(respJSON, &unmarshaledResp); err != nil {
t.Fatalf("Unmarshal response failed: %v", err)
var decodedResp Response
if err := json.Unmarshal(respJSON, &decodedResp); err != nil {
t.Fatalf("Failed to unmarshal response: %v", err)
}
if unmarshaledResp.Success != resp.Success {
t.Errorf("Expected success %v, got %v", resp.Success, unmarshaledResp.Success)
if decodedResp.Success != resp.Success {
t.Errorf("Expected success %v, got %v", resp.Success, decodedResp.Success)
}
if string(decodedResp.Data) != string(resp.Data) {
t.Errorf("Expected data %s, got %s", string(resp.Data), string(decodedResp.Data))
}
}
func TestBatchRequest(t *testing.T) {
req1, _ := NewRequest(OpCreate, map[string]string{"title": "Issue 1"})
req2, _ := NewRequest(OpCreate, map[string]string{"title": "Issue 2"})
batch := &BatchRequest{
Operations: []Request{*req1, *req2},
Atomic: true,
func TestErrorResponse(t *testing.T) {
resp := Response{
Success: false,
Error: "something went wrong",
}
batchJSON, err := json.Marshal(batch)
respJSON, err := json.Marshal(resp)
if err != nil {
t.Fatalf("Marshal batch failed: %v", err)
t.Fatalf("Failed to marshal response: %v", err)
}
var unmarshaledBatch BatchRequest
if err := json.Unmarshal(batchJSON, &unmarshaledBatch); err != nil {
t.Fatalf("Unmarshal batch failed: %v", err)
var decodedResp Response
if err := json.Unmarshal(respJSON, &decodedResp); err != nil {
t.Fatalf("Failed to unmarshal response: %v", err)
}
if len(unmarshaledBatch.Operations) != 2 {
t.Errorf("Expected 2 operations, got %d", len(unmarshaledBatch.Operations))
if decodedResp.Success {
t.Errorf("Expected success false, got true")
}
if !unmarshaledBatch.Atomic {
t.Error("Expected atomic=true")
if decodedResp.Error != resp.Error {
t.Errorf("Expected error %s, got %s", resp.Error, decodedResp.Error)
}
}
func TestAllOperations(t *testing.T) {
operations := []string{
OpPing,
OpCreate,
OpUpdate,
OpClose,
OpList,
OpShow,
OpReady,
OpStats,
OpDepAdd,
OpDepRemove,
OpDepTree,
OpLabelAdd,
OpLabelRemove,
}
for _, op := range operations {
req := Request{
Operation: op,
Args: json.RawMessage(`{}`),
}
reqJSON, err := json.Marshal(req)
if err != nil {
t.Errorf("Failed to marshal request for op %s: %v", op, err)
continue
}
var decodedReq Request
if err := json.Unmarshal(reqJSON, &decodedReq); err != nil {
t.Errorf("Failed to unmarshal request for op %s: %v", op, err)
continue
}
if decodedReq.Operation != op {
t.Errorf("Expected operation %s, got %s", op, decodedReq.Operation)
}
}
}
func TestUpdateArgsWithNilValues(t *testing.T) {
title := "New Title"
args := UpdateArgs{
ID: "bd-1",
Title: &title,
}
argsJSON, err := json.Marshal(args)
if err != nil {
t.Fatalf("Failed to marshal args: %v", err)
}
var decodedArgs UpdateArgs
if err := json.Unmarshal(argsJSON, &decodedArgs); err != nil {
t.Fatalf("Failed to unmarshal args: %v", err)
}
if decodedArgs.Title == nil {
t.Errorf("Expected title to be non-nil")
} else if *decodedArgs.Title != title {
t.Errorf("Expected title %s, got %s", title, *decodedArgs.Title)
}
if decodedArgs.Status != nil {
t.Errorf("Expected status to be nil, got %v", *decodedArgs.Status)
}
}