All checks were successful
CI/CD / detect-changes (pull_request) Successful in 3s
CI/CD / test-frontend (pull_request) Has been skipped
CI/CD / test-runners (pull_request) Has been skipped
CI/CD / e2e-smoke (pull_request) Has been skipped
CI/CD / test-go-pg (pull_request) Successful in 2m44s
CI/CD / test-sqlite (pull_request) Successful in 3m4s
CI/CD / build-and-deploy (pull_request) Successful in 1m23s
Add workflow.start(), workflow.advance(), workflow.cancel(), and workflow.submit_signoff() to the Starlark workflow module. Extract WorkflowEngine interface in sandbox package to break the circular import between workflow and sandbox packages. - WorkflowEngine interface (Start/Advance/Cancel/SubmitSignoff) - instanceToDict/signoffToDict/dictToJSON shared helpers - Runner.SetWorkflowEngine setter + main.go wiring - 6 new unit tests with mock engine Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
263 lines
7.1 KiB
Go
263 lines
7.1 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"go.starlark.net/starlark"
|
|
|
|
"armature/models"
|
|
"armature/store"
|
|
)
|
|
|
|
// ── Mock Engine ──────────────────────
|
|
|
|
type mockWorkflowEngine struct {
|
|
startResult *models.WorkflowInstance
|
|
advanceResult *models.WorkflowInstance
|
|
signoffResult *models.WorkflowSignoff
|
|
|
|
lastMethod string
|
|
lastWorkflowID string
|
|
lastInstanceID string
|
|
lastUserID string
|
|
lastData json.RawMessage
|
|
lastDecision string
|
|
lastComment string
|
|
}
|
|
|
|
func (m *mockWorkflowEngine) Start(_ context.Context, workflowID string, data json.RawMessage, userID string) (*models.WorkflowInstance, error) {
|
|
m.lastMethod = "Start"
|
|
m.lastWorkflowID = workflowID
|
|
m.lastData = data
|
|
m.lastUserID = userID
|
|
return m.startResult, nil
|
|
}
|
|
|
|
func (m *mockWorkflowEngine) Advance(_ context.Context, instanceID string, data json.RawMessage, userID string) (*models.WorkflowInstance, error) {
|
|
m.lastMethod = "Advance"
|
|
m.lastInstanceID = instanceID
|
|
m.lastData = data
|
|
m.lastUserID = userID
|
|
return m.advanceResult, nil
|
|
}
|
|
|
|
func (m *mockWorkflowEngine) Cancel(_ context.Context, instanceID string, userID string) error {
|
|
m.lastMethod = "Cancel"
|
|
m.lastInstanceID = instanceID
|
|
m.lastUserID = userID
|
|
return nil
|
|
}
|
|
|
|
func (m *mockWorkflowEngine) SubmitSignoff(_ context.Context, instanceID, userID, decision, comment string) (*models.WorkflowSignoff, error) {
|
|
m.lastMethod = "SubmitSignoff"
|
|
m.lastInstanceID = instanceID
|
|
m.lastUserID = userID
|
|
m.lastDecision = decision
|
|
m.lastComment = comment
|
|
return m.signoffResult, nil
|
|
}
|
|
|
|
// ── Helpers ──────────────────────────
|
|
|
|
func testInstance() *models.WorkflowInstance {
|
|
return &models.WorkflowInstance{
|
|
ID: "inst-1",
|
|
WorkflowID: "wf-1",
|
|
WorkflowVersion: 2,
|
|
CurrentStage: "review",
|
|
Status: "active",
|
|
StartedBy: "user-1",
|
|
StageData: json.RawMessage(`{"key":"val"}`),
|
|
}
|
|
}
|
|
|
|
func testSignoff() *models.WorkflowSignoff {
|
|
return &models.WorkflowSignoff{
|
|
ID: "sig-1",
|
|
InstanceID: "inst-1",
|
|
Stage: "review",
|
|
UserID: "user-1",
|
|
Decision: "approve",
|
|
Comment: "LGTM",
|
|
CreatedAt: time.Date(2026, 4, 3, 12, 0, 0, 0, time.UTC),
|
|
}
|
|
}
|
|
|
|
func runWorkflowScript(t *testing.T, engine WorkflowEngine, rc *RunContext, script string) (starlark.StringDict, error) {
|
|
t.Helper()
|
|
mod := BuildWorkflowModule(context.Background(), store.Stores{}, engine, rc)
|
|
predeclared := starlark.StringDict{"workflow": mod}
|
|
globals, err := starlark.ExecFile(&starlark.Thread{Name: "test"}, "test.star", script, predeclared)
|
|
return globals, err
|
|
}
|
|
|
|
// ── Tests ────────────────────────────
|
|
|
|
func TestWorkflowStart(t *testing.T) {
|
|
mock := &mockWorkflowEngine{startResult: testInstance()}
|
|
rc := &RunContext{UserID: "user-1"}
|
|
|
|
globals, err := runWorkflowScript(t, mock, rc, `
|
|
result = workflow.start("wf-1", data={"key": "val"})
|
|
`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if mock.lastMethod != "Start" {
|
|
t.Fatalf("expected Start, got %s", mock.lastMethod)
|
|
}
|
|
if mock.lastWorkflowID != "wf-1" {
|
|
t.Fatalf("expected wf-1, got %s", mock.lastWorkflowID)
|
|
}
|
|
if mock.lastUserID != "user-1" {
|
|
t.Fatalf("expected user-1, got %s", mock.lastUserID)
|
|
}
|
|
|
|
// Verify data was marshaled
|
|
var d map[string]interface{}
|
|
if err := json.Unmarshal(mock.lastData, &d); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if d["key"] != "val" {
|
|
t.Fatalf("expected data key=val, got %v", d)
|
|
}
|
|
|
|
// Verify returned dict
|
|
result := globals["result"].(*starlark.Dict)
|
|
id, _, _ := result.Get(starlark.String("id"))
|
|
if id.(starlark.String).GoString() != "inst-1" {
|
|
t.Fatalf("expected id=inst-1, got %v", id)
|
|
}
|
|
status, _, _ := result.Get(starlark.String("status"))
|
|
if status.(starlark.String).GoString() != "active" {
|
|
t.Fatalf("expected status=active, got %v", status)
|
|
}
|
|
}
|
|
|
|
func TestWorkflowAdvance(t *testing.T) {
|
|
inst := testInstance()
|
|
inst.CurrentStage = "complete"
|
|
mock := &mockWorkflowEngine{advanceResult: inst}
|
|
rc := &RunContext{UserID: "user-1"}
|
|
|
|
globals, err := runWorkflowScript(t, mock, rc, `
|
|
result = workflow.advance("inst-1", data={"step": 2})
|
|
`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if mock.lastMethod != "Advance" {
|
|
t.Fatalf("expected Advance, got %s", mock.lastMethod)
|
|
}
|
|
if mock.lastInstanceID != "inst-1" {
|
|
t.Fatalf("expected inst-1, got %s", mock.lastInstanceID)
|
|
}
|
|
|
|
result := globals["result"].(*starlark.Dict)
|
|
stage, _, _ := result.Get(starlark.String("current_stage"))
|
|
if stage.(starlark.String).GoString() != "complete" {
|
|
t.Fatalf("expected current_stage=complete, got %v", stage)
|
|
}
|
|
}
|
|
|
|
func TestWorkflowCancel(t *testing.T) {
|
|
mock := &mockWorkflowEngine{}
|
|
rc := &RunContext{UserID: "user-1"}
|
|
|
|
globals, err := runWorkflowScript(t, mock, rc, `
|
|
result = workflow.cancel("inst-1")
|
|
`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if mock.lastMethod != "Cancel" {
|
|
t.Fatalf("expected Cancel, got %s", mock.lastMethod)
|
|
}
|
|
if mock.lastInstanceID != "inst-1" {
|
|
t.Fatalf("expected inst-1, got %s", mock.lastInstanceID)
|
|
}
|
|
if mock.lastUserID != "user-1" {
|
|
t.Fatalf("expected user-1, got %s", mock.lastUserID)
|
|
}
|
|
|
|
if globals["result"] != starlark.None {
|
|
t.Fatalf("expected None, got %v", globals["result"])
|
|
}
|
|
}
|
|
|
|
func TestWorkflowSubmitSignoff(t *testing.T) {
|
|
mock := &mockWorkflowEngine{signoffResult: testSignoff()}
|
|
rc := &RunContext{UserID: "user-1"}
|
|
|
|
globals, err := runWorkflowScript(t, mock, rc, `
|
|
result = workflow.submit_signoff("inst-1", "approve", comment="LGTM")
|
|
`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if mock.lastMethod != "SubmitSignoff" {
|
|
t.Fatalf("expected SubmitSignoff, got %s", mock.lastMethod)
|
|
}
|
|
if mock.lastDecision != "approve" {
|
|
t.Fatalf("expected approve, got %s", mock.lastDecision)
|
|
}
|
|
if mock.lastComment != "LGTM" {
|
|
t.Fatalf("expected LGTM, got %s", mock.lastComment)
|
|
}
|
|
|
|
result := globals["result"].(*starlark.Dict)
|
|
decision, _, _ := result.Get(starlark.String("decision"))
|
|
if decision.(starlark.String).GoString() != "approve" {
|
|
t.Fatalf("expected decision=approve, got %v", decision)
|
|
}
|
|
comment, _, _ := result.Get(starlark.String("comment"))
|
|
if comment.(starlark.String).GoString() != "LGTM" {
|
|
t.Fatalf("expected comment=LGTM, got %v", comment)
|
|
}
|
|
}
|
|
|
|
func TestWorkflowWrite_NoUser(t *testing.T) {
|
|
mock := &mockWorkflowEngine{startResult: testInstance()}
|
|
|
|
_, err := runWorkflowScript(t, mock, nil, `
|
|
result = workflow.start("wf-1")
|
|
`)
|
|
if err == nil {
|
|
t.Fatal("expected error for nil RunContext")
|
|
}
|
|
if got := err.Error(); !contains(got, "authenticated user context") {
|
|
t.Fatalf("unexpected error: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestWorkflowWrite_NoEngine(t *testing.T) {
|
|
rc := &RunContext{UserID: "user-1"}
|
|
|
|
_, err := runWorkflowScript(t, nil, rc, `
|
|
result = workflow.start("wf-1")
|
|
`)
|
|
if err == nil {
|
|
t.Fatal("expected error for nil engine")
|
|
}
|
|
if got := err.Error(); !contains(got, "engine not available") {
|
|
t.Fatalf("unexpected error: %s", got)
|
|
}
|
|
}
|
|
|
|
// contains is a test helper — strings.Contains without importing strings.
|
|
func contains(s, substr string) bool {
|
|
for i := 0; i <= len(s)-len(substr); i++ {
|
|
if s[i:i+len(substr)] == substr {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|