This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/sandbox/workflow_module_test.go
Jeffrey Smith 42b864376c
All checks were successful
CI/CD / detect-changes (push) Successful in 4s
CI/CD / test-frontend (push) Has been skipped
CI/CD / test-runners (push) Has been skipped
CI/CD / e2e-smoke (push) Has been skipped
CI/CD / test-go-pg (push) Successful in 2m49s
CI/CD / test-sqlite (push) Successful in 3m2s
CI/CD / build-and-deploy (push) Successful in 29s
Feat v0.9.7 workflow starlark write (#81)
Co-authored-by: Jeffrey Smith <jasafpro@gmail.com>
Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
2026-04-03 18:30:10 +00:00

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
}