From ce5fc3e2263486380eacaa2f8d46d3df937e9152 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 12 Oct 2026 01:17:02 +1100 Subject: [PATCH] test(postgres): remove database-backed tests and the testcontainers harness --- go.mod | 22 -- go.sum | 59 ----- .../app/controlplane/channel_settings_test.go | 73 ------ .../app/controlplane/chat_settings_test.go | 143 ----------- internal/app/controlplane/org_models_test.go | 102 -------- .../app/controlplane/workflow_org_test.go | 209 ---------------- .../postgres/binding_dispatch_test.go | 226 ----------------- .../postgres/capture_list_org_test.go | 59 ----- .../postgres/capture_prune_org_test.go | 74 ------ .../postgres/dispatch_org_test.go | 164 ------------ .../infrastructure/postgres/eda_org_test.go | 106 -------- .../postgres/org_sys_migration_test.go | 234 ------------------ internal/infrastructure/postgres/orgs_test.go | 75 ------ .../postgres/pgstore/pgstore.go | 61 ----- .../infrastructure/postgres/pgtest/pgtest.go | 85 ------- .../postgres/presence_checks_test.go | 34 --- .../infrastructure/postgres/resume_test.go | 93 ------- .../postgres/review_queue_test.go | 139 ----------- .../postgres/session_org_test.go | 62 ----- .../postgres/source_org_test.go | 33 --- .../infrastructure/postgres/task_org_test.go | 83 ------- .../postgres/task_origin_test.go | 54 ---- .../infrastructure/postgres/usage_org_test.go | 45 ---- .../postgres/workflow_call_test.go | 122 --------- 24 files changed, 2357 deletions(-) delete mode 100644 internal/app/controlplane/channel_settings_test.go delete mode 100644 internal/app/controlplane/chat_settings_test.go delete mode 100644 internal/app/controlplane/org_models_test.go delete mode 100644 internal/app/controlplane/workflow_org_test.go delete mode 100644 internal/infrastructure/postgres/binding_dispatch_test.go delete mode 100644 internal/infrastructure/postgres/capture_list_org_test.go delete mode 100644 internal/infrastructure/postgres/capture_prune_org_test.go delete mode 100644 internal/infrastructure/postgres/dispatch_org_test.go delete mode 100644 internal/infrastructure/postgres/eda_org_test.go delete mode 100644 internal/infrastructure/postgres/org_sys_migration_test.go delete mode 100644 internal/infrastructure/postgres/orgs_test.go delete mode 100644 internal/infrastructure/postgres/pgstore/pgstore.go delete mode 100644 internal/infrastructure/postgres/pgtest/pgtest.go delete mode 100644 internal/infrastructure/postgres/presence_checks_test.go delete mode 100644 internal/infrastructure/postgres/resume_test.go delete mode 100644 internal/infrastructure/postgres/review_queue_test.go delete mode 100644 internal/infrastructure/postgres/session_org_test.go delete mode 100644 internal/infrastructure/postgres/source_org_test.go delete mode 100644 internal/infrastructure/postgres/task_org_test.go delete mode 100644 internal/infrastructure/postgres/task_origin_test.go delete mode 100644 internal/infrastructure/postgres/usage_org_test.go delete mode 100644 internal/infrastructure/postgres/workflow_call_test.go diff --git a/go.mod b/go.mod index 7d7f1652c..e28556137 100644 --- a/go.mod +++ b/go.mod @@ -4,25 +4,20 @@ go 1.27.2 require ( cel.dev/expr v0.25.3 // indirect - dario.cat/mergo v1.0.2 // indirect - github.com/Azure/go-ansiterm v0.0.0-20261008225312-e8a9660d7aea // indirect github.com/Microsoft/go-winio v0.6.3 // indirect github.com/ProtonMail/go-crypto v1.5.2 // indirect github.com/antithesishq/antithesis-sdk-go v0.8.0 // indirect github.com/antlr4-go/antlr/v4 v4.13.1 // indirect - github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cloudflare/circl v1.6.5 // indirect github.com/containerd/errdefs v1.0.0 // indirect github.com/containerd/errdefs/pkg v0.3.0 // indirect github.com/containerd/log v0.2.0 // indirect github.com/containerd/platforms v1.0.0-rc.5 // indirect - github.com/cpuguy83/dockercfg v0.3.2 // indirect github.com/distribution/reference v0.6.0 // indirect github.com/docker/go-connections v0.8.2 // indirect github.com/docker/go-units v0.5.0 // indirect github.com/dustin/go-humanize v1.1.0 // indirect - github.com/ebitengine/purego v0.11.1 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/fatih/color v1.19.0 // indirect github.com/felixge/httpsnoop v1.1.0 // indirect @@ -31,7 +26,6 @@ require ( github.com/go-json-experiment/json v0.0.0-20260820222146-c27c302e5fc3 // indirect github.com/go-logr/logr v1.4.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect - github.com/go-ole/go-ole v1.3.0 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/go-tpm v0.9.8 // indirect github.com/hashicorp/yamux v0.1.2 // indirect @@ -40,37 +34,23 @@ require ( github.com/jackc/puddle/v2 v2.2.3 // indirect github.com/kevinburke/ssh_config v1.6.0 // indirect github.com/klauspost/compress v1.20.1 // indirect - github.com/lufia/plan9stats v0.0.0-20260802145828-341c2f0c90b5 // indirect - github.com/magiconair/properties v1.18.12 // indirect github.com/mattn/go-colorable v0.1.16 // indirect github.com/mattn/go-isatty v0.0.24 // indirect github.com/mfridman/interpolate v0.0.2 // indirect github.com/minio/highwayhash v1.0.4 // indirect github.com/moby/docker-image-spec v1.3.1 // indirect - github.com/moby/go-archive v0.3.3 // indirect - github.com/moby/patternmatcher v0.6.1 // indirect - github.com/moby/sys/sequential v0.7.0 // indirect - github.com/moby/sys/user v0.4.1 // indirect - github.com/moby/sys/userns v0.2.1 // indirect - github.com/moby/term v0.5.2 // indirect github.com/nats-io/jwt/v2 v2.8.2 // indirect github.com/nats-io/nkeys v0.4.16 // indirect github.com/nats-io/nuid v1.0.1 // indirect github.com/oklog/run v1.2.0 // indirect github.com/pjbgf/sha1cd v0.7.0 // indirect github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect - github.com/power-devops/perfstat v0.0.0-20260916203055-22a1a467d9f0 // indirect github.com/segmentio/asm v1.2.1 // indirect github.com/segmentio/encoding v0.5.4 // indirect github.com/sergi/go-diff v1.4.0 // indirect github.com/sethvargo/go-retry v0.5.0 // indirect - github.com/shirou/gopsutil/v4 v4.26.9 // indirect github.com/sirupsen/logrus v1.10.2 // indirect - github.com/stretchr/testify v1.12.1 // indirect - github.com/tklauser/go-sysconf v0.4.0 // indirect - github.com/tklauser/numcpus v0.12.0 // indirect github.com/yosida95/uritemplate/v3 v3.0.2 // indirect - github.com/yusufpapurcu/wmi v1.2.4 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.72.0 // indirect go.opentelemetry.io/otel v1.47.0 // indirect @@ -113,8 +93,6 @@ require ( github.com/opencontainers/image-spec v1.1.1 github.com/pressly/goose/v3 v3.28.0 github.com/samcharles93/ai-sdk v0.1.43 - github.com/testcontainers/testcontainers-go v0.44.0 - github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 go.lsp.dev/jsonrpc2 v1.0.1 go.lsp.dev/protocol v1.0.1 go.lsp.dev/uri v1.0.1 diff --git a/go.sum b/go.sum index b00d83896..02ced88b3 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,6 @@ cel.dev/cel-go v0.32.0 h1:irvpFKr5EuGPyxeME03ERh0rii1TX+BDAnB9eL3IvNk= cel.dev/cel-go v0.32.0/go.mod h1:DnVip7tpJSsgZymwfT+m1tnEVy3ivAjSMXPx12YrMkU= cel.dev/expr v0.25.3 h1:A2jO8jwOugrrovveCWfj0KEZOfqiLgAcwjpHPhzIGw0= cel.dev/expr v0.25.3/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4= -dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8= -dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA= -github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk= -github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8= -github.com/Azure/go-ansiterm v0.0.0-20261008225312-e8a9660d7aea h1:NdVLskj4PaItpD133shAdyHicndDbcDGpManoqR032g= -github.com/Azure/go-ansiterm v0.0.0-20261008225312-e8a9660d7aea/go.mod h1:Ue4ymb+lBljb9NjkybMTsOYOSjH/cJTim9nrhvRJAUU= github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/Microsoft/go-winio v0.6.3 h1:M/52tsoyTfwaxajLnXZm7PqkbMGRH8iSWP+8fcg9MQ4= @@ -26,8 +20,6 @@ github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/ github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU5DoEw9xY/FUi1c= github.com/cedar-policy/cedar-go v1.8.0 h1:9gcU7EHXwHC2RMdpph68yTAkdB3behTTssC+kt4GoS8= github.com/cedar-policy/cedar-go v1.8.0/go.mod h1:h5+3CVW1oI5LXVskJG+my9TFCYI5yjh/+Ul3EJie6MI= -github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= -github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cloudflare/circl v1.6.5 h1:O64F26HEqNhznd/hrC5KZXVKYuKM2rx4deZDTc4ihQA= @@ -42,10 +34,6 @@ github.com/containerd/platforms v1.0.0-rc.5 h1:vXd569rDrz8LeMXzAnBsy6LADV5YtsD8o github.com/containerd/platforms v1.0.0-rc.5/go.mod h1:lKlMXyLybmBedS/JJm11uDofzI8L2v0J2ZbYvNsbq1A= github.com/coreos/go-oidc/v3 v3.21.0 h1:wZo4Q9Pum8dYEj0eMUPrqR+kvuGkeUplbLpNCkBqoWM= github.com/coreos/go-oidc/v3 v3.21.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4= -github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA= -github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc= -github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= -github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= @@ -58,8 +46,6 @@ github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.8 h1:ay2XEj0yW3uzsNwN3KXS/c4nlneH github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.8/go.mod h1:4G6VgFAp9eaQkwy7DbZbOfWRD/Mjvc/TnEtQBRRMEsA= github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= -github.com/ebitengine/purego v0.11.1 h1:2zpWRSQNVKN4eKsKO9eM1ILDgWfYMY9GwqRmK6XeQ/0= -github.com/ebitengine/purego v0.11.1/go.mod h1:DCHPP08djqhNSoTfImcnHYQRZmd0qhakvrozqaEYhGQ= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= @@ -86,9 +72,6 @@ github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8= github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= -github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= -github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-telegram/bot v1.27.0 h1:PG9yBDzM8O26oUerE9SZwWlHon4RpH8WcNslZ7CEZmE= github.com/go-telegram/bot v1.27.0/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM= github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY= @@ -130,12 +113,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= -github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/lufia/plan9stats v0.0.0-20260802145828-341c2f0c90b5 h1:eveIIGn4BGM3qknO74omf6HYr30/exH+eVUTuAgwjZ0= -github.com/lufia/plan9stats v0.0.0-20260802145828-341c2f0c90b5/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg= -github.com/magiconair/properties v1.18.12 h1:sT9zQpvTB3B4gzrX0tmZNTEaGyg8Zw55MFYRE32Mr9I= -github.com/magiconair/properties v1.18.12/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.16 h1:+2YjeAe7xP1OQbZW1jERPWWCDLlNqFNCvjiqGiAt84A= @@ -144,34 +121,16 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= -github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI= -github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o= github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= github.com/minio/highwayhash v1.0.4 h1:asJizugGgchQod2ja9NJlGOWq4s7KsAWr5XUc9Clgl4= github.com/minio/highwayhash v1.0.4/go.mod h1:GGYsuwP/fPD6Y9hMiXuapVvlIUEhFhMTh0rxU3ik1LQ= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= -github.com/moby/go-archive v0.3.3 h1:OxxR9paxsluYi+zDUEXTTaIxtkK3viymW+Ka7vRhhME= -github.com/moby/go-archive v0.3.3/go.mod h1:Npdv43fFqlhZW7Xo8fbm3ZMYFvAGNviUPqX21VERbcE= github.com/moby/moby/api v1.56.1 h1:PpWkvVPB7Fr/No8w+TfyJ/I6rWZ2YPZhT3JorIg+06c= github.com/moby/moby/api v1.56.1/go.mod h1:sZ+THbVWkjOmBPPfbnzdD/G1LuIexWhqlSHHPTDQ1Uk= github.com/moby/moby/client v0.6.2 h1:ekSPlMccrZGcw6Scb8T9D39qE0mB+XupU/yS0zNw9N4= github.com/moby/moby/client v0.6.2/go.mod h1:DGcnO7qmPJrqobx/ggQgQ/AUk+2wgVdQv7uxB0/mEsc= -github.com/moby/patternmatcher v0.6.1 h1:qlhtafmr6kgMIJjKJMDmMWq7WLkKIo23hsrpR3x084U= -github.com/moby/patternmatcher v0.6.1/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= -github.com/moby/sys/mount v0.3.5 h1:eS3fsZTjHaBihwjp4/+5Z3jxqLXYsbwxqpVSfFv3M00= -github.com/moby/sys/mount v0.3.5/go.mod h1:WUQDO+/uCiCIkIztx8SrwIDVn2dtMFRBebRhpDFT71M= -github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg= -github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4= -github.com/moby/sys/sequential v0.7.0 h1:ASQNGNROJSuOO6LL6bPHbKvuZu6NU8P4ldPWk31zj/8= -github.com/moby/sys/sequential v0.7.0/go.mod h1:NfSTAp6V3fw4tmkD62PEcOKeZKquXT8VKCkf7aVR79o= -github.com/moby/sys/user v0.4.1 h1:RgjRlaDKi/Xmyrz4t8lyzXT6v2ooFeO/7xtchmhVWE0= -github.com/moby/sys/user v0.4.1/go.mod h1:E9QsW5WRe1kUAf7kW8hXKwu1uhsZEAdPLYHYSDudF4Y= -github.com/moby/sys/userns v0.2.1 h1:4OvdM7BcPkASbuouHsbW3aeMJSFlYDldBRnXVZhaRk8= -github.com/moby/sys/userns v0.2.1/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28= -github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ= -github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc= github.com/modelcontextprotocol/go-sdk v1.8.0 h1:KIvahhYqwtbeniWVPs3TcXEA7b8jEtwfBpOTAI+Urx4= github.com/modelcontextprotocol/go-sdk v1.8.0/go.mod h1:dL7u98E/zjJTGzEq+j30jQ8K2k1mb6LeAH4inEcSGts= github.com/nats-io/jwt/v2 v2.8.2 h1:XXRgB60MSTnqsRwejQurVDs/hcv2dkt+86GjI+I/bMc= @@ -197,8 +156,6 @@ github.com/pjbgf/sha1cd v0.7.0/go.mod h1:pKR5Li+qTCo+ebqITZfwPlJGoCFCvSO00qxjbNt github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/power-devops/perfstat v0.0.0-20260916203055-22a1a467d9f0 h1:XA01Vk/wv9YikCi1V51yRzIHPMT5of9+cMpZoZvDn/M= -github.com/power-devops/perfstat v0.0.0-20260916203055-22a1a467d9f0/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/pressly/goose/v3 v3.28.0 h1:D2M+iL31GmpZxSHOhX8mqyqAT3CXnokUmm0eKoSP+Vc= github.com/pressly/goose/v3 v3.28.0/go.mod h1:v26MOuB8bL3kzzrt3Vqhb3R0PRVsl8hFQKdrht/L6Rk= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= @@ -215,31 +172,17 @@ github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw= github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sethvargo/go-retry v0.5.0 h1:JYfFKmVJ8ER06GReN1efzc5cm52BrVsFVzmcHiKJwfY= github.com/sethvargo/go-retry v0.5.0/go.mod h1:tvsjdKG6xfiCx4LSiUZ06kcv38xvdVQwv8R6/VnnVWg= -github.com/shirou/gopsutil/v4 v4.26.9 h1:CaBo/hBFqvlJoLvAQEKdHdf5GD97/MN2ACfyv63+2Ig= -github.com/shirou/gopsutil/v4 v4.26.9/go.mod h1:nKH+8wX2zxr/mDbsR+AAy6Qd2z4ZOQ7wfQaEp+yrYmg= github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBBo= github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4= -github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= -github.com/testcontainers/testcontainers-go v0.44.0 h1:/Fwh6HY1mIikhnm9e7HwoxGycx0lzRAE0f5VQpjFxzI= -github.com/testcontainers/testcontainers-go v0.44.0/go.mod h1:IcnwQrYTO86xHXu5bvMaBH7ATlbS3Qn1M1QWW3c66rE= -github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 h1:8fdv/9y3JMxjQ+ULAcOG8RtgeNu5t9XF9LolSXDuTwM= -github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0/go.mod h1:CFr2LncGYokw+OKjXcr8ARCKG1SaC2UEnGxFBovE86g= -github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU= -github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI= -github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4= -github.com/tklauser/numcpus v0.12.0/go.mod h1:ABHeXzJnr/qqwguhClkZKT1/8VABcYrsyUiUGobwWJg= github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4= -github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= -github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= go.lsp.dev/jsonrpc2 v1.0.1 h1:JZAchpm6RcOuwEOnZ7kF3kA1EpEjdJZ/6rMSfOF3+F4= go.lsp.dev/jsonrpc2 v1.0.1/go.mod h1:WN+TesqrPumBmUdsuWyn9DHq3zl4i85L/qG0NLdj8qg= go.lsp.dev/protocol v1.0.1 h1:kLWUIk1v/deXxo1D0VO4avjZnfeuZ82mouJgJd/NbdU= @@ -278,10 +221,8 @@ golang.org/x/oauth2 v0.37.0 h1:JUlcxA8oAtauLfiH8FX2/FkAWHAdi0QtGCGc+hofE98= golang.org/x/oauth2 v0.37.0/go.mod h1:IxwZNxUULJmpBFf9K/9NTMSIfZZuvuTy1gGxhigP/58= golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= -golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/app/controlplane/channel_settings_test.go b/internal/app/controlplane/channel_settings_test.go deleted file mode 100644 index b340e8298..000000000 --- a/internal/app/controlplane/channel_settings_test.go +++ /dev/null @@ -1,73 +0,0 @@ -package controlplane_test - -import ( - "encoding/json" - "testing" - - "github.com/samcharles93/archie-core/internal/app/controlplane" - pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps" -) - -func TestStoredChannelSettingsCanBeEdited(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - if err := db.BootstrapIdentities(ctx, nil); err != nil { - t.Fatal(err) - } - if err := db.UpgradeDefaultOrg(ctx); err != nil { - t.Fatal(err) - } - steps, err := workflowsteps.NewManager() - if err != nil { - t.Fatal(err) - } - server, err := controlplane.NewServer(db, steps) - if err != nil { - t.Fatal(err) - } - client := workflowRPC(t, server, db).ControlPlane() - stored, err := db.PutResource(ctx, storecontract.ResourceWrite{ - OrgID: storecontract.DefaultOrgID, Kind: controlplane.ChannelSettingsKind, - Value: []byte(`{"email":{"listen_addr":""},"webhook":{},"webhook_addr":"","telegram":{"allowed_user_ids":[42],"token_ref":{"engine":"bws","key":"telegram"}},"workspace":"/workspace","max_steps":17,"rate_limit":{"window":"30s","max_requests":5}}`), - Actor: "operator", Source: "test", RequestID: "old-settings", - }) - if err != nil { - t.Fatal(err) - } - got, err := client.Query(ctx, &pb.QueryRequest{Kind: controlplane.ChannelSettingsKind}) - if err != nil { - t.Fatal(err) - } - var settings controlplanerpc.ChannelSettings - if err := json.Unmarshal(got.Resource.ValueJson, &settings); err != nil { - t.Fatal(err) - } - var document map[string]json.RawMessage - if err := json.Unmarshal(got.Resource.ValueJson, &document); err != nil { - t.Fatal(err) - } - for _, retired := range []string{"email", "webhook", "webhook_addr"} { - if _, exists := document[retired]; exists { - t.Fatalf("served retired %s field in editable settings", retired) - } - } - if settings.Telegram.Token.Key != "telegram" || len(settings.Telegram.AllowedUserIDs) != 1 || settings.Telegram.AllowedUserIDs[0] != 42 || settings.Workspace != "/workspace" || settings.MaxSteps != 17 || settings.RateLimit.MaxRequests != 5 { - t.Fatalf("lost supported settings: %+v", settings) - } - settings.Telegram.AllowedUserIDs = []int64{42, 43} - value, err := json.Marshal(settings) - if err != nil { - t.Fatal(err) - } - changed, err := client.Command(ctx, &pb.CommandRequest{Kind: controlplane.ChannelSettingsKind, Command: "replace", ValueJson: value, ExpectedVersion: got.Resource.Version, Actor: "operator", Source: "test", RequestId: "edit"}) - if err != nil { - t.Fatalf("saving edited channel settings: %v", err) - } - if changed.Resource.Version != stored.Version+1 { - t.Fatalf("saved version %d", changed.Resource.Version) - } -} diff --git a/internal/app/controlplane/chat_settings_test.go b/internal/app/controlplane/chat_settings_test.go deleted file mode 100644 index 787ab25e0..000000000 --- a/internal/app/controlplane/chat_settings_test.go +++ /dev/null @@ -1,143 +0,0 @@ -package controlplane_test - -import ( - "context" - "net" - "strings" - "testing" - - "google.golang.org/grpc" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/credentials/insecure" - "google.golang.org/grpc/status" - - "github.com/samcharles93/archie-core/internal/app/controlplane" - "github.com/samcharles93/archie-core/internal/config" - "github.com/samcharles93/archie-core/internal/domain/access" - "github.com/samcharles93/archie-core/internal/domain/identity" - "github.com/samcharles93/archie-core/internal/domain/messaging" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/gateway" - "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" - "github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity" - "github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps" -) - -func TestWebChatSettingsActor(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - if err := db.BootstrapIdentities(ctx, nil); err != nil { - t.Fatal(err) - } - if err := db.UpgradeDefaultOrg(ctx); err != nil { - t.Fatal(err) - } - steps, err := workflowsteps.NewManager() - if err != nil { - t.Fatal(err) - } - cp, err := controlplane.NewServer(db, steps) - if err != nil { - t.Fatal(err) - } - if _, _, err := cp.ImportConfig(ctx, config.Config{}); err != nil { - t.Fatal(err) - } - state := workflowRPC(t, cp, db) - router := gateway.NewRouter(state, nil, "web") - router.Settings = messaging.NewSettingsCommand(controlplanerpc.NewSettingsClient(state.ControlPlane())).WithIdentities(state) - listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - callers := rpcidentity.Callers{Principals: state} - srv := grpc.NewServer(grpc.ChainUnaryInterceptor(gatewayrpc.UnaryServerInterceptor("gateway-secret"), callers.Unary()), grpc.ChainStreamInterceptor(gatewayrpc.StreamServerInterceptor("gateway-secret"), callers.Stream())) - gatewayrpc.RegisterServer(srv, &gateway.LocalChatAdapter{Router: router}, nil, gatewayrpc.Catalog{}) - go func() { _ = srv.Serve(listener) }() - t.Cleanup(srv.Stop) - person, err := db.PrincipalFor(ctx, identity.OperatorID()) - if err != nil { - t.Fatal(err) - } - tests := []struct { - name, token, sender string - person, write, stream bool - code codes.Code - }{ - {name: "streamed signed-in person", stream: true, token: "gateway-secret", person: true, write: true}, - {name: "streamed wrong service credential", stream: true, token: "wrong", person: true, code: codes.Unauthenticated}, - {name: "signed-in person", token: "gateway-secret", person: true, write: true}, - {name: "message cannot spoof actor", token: "gateway-secret", sender: string(identity.SystemID), person: true, write: true}, - {name: "message alone cannot name a person", token: "gateway-secret", sender: string(identity.OperatorID())}, - {name: "wrong service credential cannot forward a principal", token: "wrong", person: true, code: codes.Unauthenticated}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - conn, err := grpc.NewClient(listener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithUnaryInterceptor(gatewayrpc.UnaryClientTokenInterceptor(tt.token)), grpc.WithStreamInterceptor(gatewayrpc.StreamClientTokenInterceptor(tt.token))) - if err != nil { - t.Fatal(err) - } - defer conn.Close() - requestCtx := ctx - if tt.person { - requestCtx = access.WithPrincipal(ctx, person) - } - before, err := db.Resource(ctx, string(org.DefaultOrgID), controlplane.ReviewSettingsKind) - if err != nil { - t.Fatal(err) - } - in := messaging.Inbound{Platform: "web", Message: messaging.Message{SenderID: tt.sender, Text: "/settings set review-settings precision_gate=true"}} - client := gatewayrpc.NewClient(conn) - reply, err := settingsReply(t, client, requestCtx, in, tt.stream, tt.code) - if status.Code(err) != tt.code { - t.Fatalf("route error = %v, want %s", err, tt.code) - } - after, err := db.Resource(ctx, string(org.DefaultOrgID), controlplane.ReviewSettingsKind) - if err != nil { - t.Fatal(err) - } - if !tt.write { - if after.Version != before.Version { - t.Fatal("unauthenticated command changed settings") - } - return - } - history, err := db.ResourceHistory(ctx, string(org.DefaultOrgID), controlplane.ReviewSettingsKind, 1) - if err != nil { - t.Fatal(err) - } - if len(history) != 1 { - t.Fatal("settings audit missing") - } - if !strings.Contains(reply.Text, "Updated review-settings") || after.Version != before.Version+1 || history[0].Actor != string(person.IdentityID) { - t.Fatalf("reply %q, version %d -> %d, actor %q", reply.Text, before.Version, after.Version, history[0].Actor) - } - }) - } -} - -func settingsReply(t *testing.T, client *gatewayrpc.Client, ctx context.Context, in messaging.Inbound, stream bool, want codes.Code) (messaging.ChatReply, error) { - t.Helper() - if !stream { - return client.Route(ctx, in) - } - events, err := client.Stream(ctx, in) - if err != nil { - return messaging.ChatReply{}, err - } - var reply messaging.ChatReply - for event := range events { - if event.Kind == "error" { - if want == codes.OK || !strings.Contains(event.Text, "code = "+want.String()) { - t.Fatalf("stream: %s", event.Text) - } - return messaging.ChatReply{}, status.Error(want, event.Text) - } - if event.Kind == "done" { - reply.Text = event.Text - } - } - return reply, nil -} diff --git a/internal/app/controlplane/org_models_test.go b/internal/app/controlplane/org_models_test.go deleted file mode 100644 index 4251536af..000000000 --- a/internal/app/controlplane/org_models_test.go +++ /dev/null @@ -1,102 +0,0 @@ -package controlplane_test - -import ( - "context" - "maps" - "slices" - "strings" - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/app/controlplane" - pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" - "github.com/samcharles93/archie-core/internal/domain/identity" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps" -) - -// An org reaches only the models the instance admin allows it and the -// providers it was allowed to add; an org with no policy reaches nothing. -func TestOrgModelPolicy(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - if err := db.BootstrapIdentities(ctx, nil); err != nil { - t.Fatal(err) - } - if err := db.UpgradeDefaultOrg(ctx); err != nil { - t.Fatal(err) - } - steps, err := workflowsteps.NewManager() - if err != nil { - t.Fatal(err) - } - server, err := controlplane.NewServer(db, steps) - if err != nil { - t.Fatal(err) - } - owner, err := db.Create(ctx, identity.Identity{ID: "owner", Kind: identity.KindUser, DisplayName: "Owner", Lifecycle: identity.LifecycleActive}, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: "owner", At: time.Now()}) - if err != nil { - t.Fatal(err) - } - for _, id := range []org.OrgID{"acme", "newco"} { - if _, err := db.CreateOrg(ctx, org.Org{ID: id, Name: string(id)}, owner.ID); err != nil { - t.Fatal(err) - } - } - sys := org.WithOrg(ctx, org.DefaultOrgID) - acme := org.WithOrg(ctx, "acme") - put := func(ctx context.Context, kind, value string) error { - _, err := server.Command(ctx, &pb.CommandRequest{Kind: kind, Command: "replace", ValueJson: []byte(value), Actor: "test", Source: "test", RequestId: kind + value}) - return err - } - for _, w := range [][2]string{ - {controlplane.ProviderSettingsKind, `{"openai":{"class":"openai","api_key_ref":{}},"deepseek":{"class":"deepseek","api_key_ref":{}}}`}, - {controlplane.ModelAliasesKind, `{"default":"openai/gpt-6-luna","cheap":"deepseek/chat"}`}, - {controlplane.OrgModelPolicyKind, `{"acme":{"allowed":["openai/*"],"own_providers":true}}`}, - } { - if err := put(sys, w[0], w[1]); err != nil { - t.Fatalf("seed %s: %v", w[0], err) - } - } - - for _, tt := range []struct { - name, kind, value string - ok bool - }{ - {"an alias to a model the org may not use", controlplane.ModelAliasesKind, `{"fast":"deepseek/chat"}`, false}, - {"an org provider named like an instance one", controlplane.ProviderSettingsKind, `{"openai":{"class":"openai","api_key_ref":{}}}`, false}, - {"an org provider pointing at a secret", controlplane.ProviderSettingsKind, `{"mine":{"class":"openai","api_key_ref":{"engine":"env","key":"X"}}}`, false}, - {"an org writing the instance policy", controlplane.OrgModelPolicyKind, `{"acme":{"allowed":["deepseek/*"]}}`, false}, - {"an org provider of its own", controlplane.ProviderSettingsKind, `{"mine":{"class":"openai","api_key_ref":{}}}`, true}, - {"aliases to allowed and own models", controlplane.ModelAliasesKind, `{"fast":"openai/gpt-6-mini","own":"mine/m1"}`, true}, - } { - t.Run(tt.name, func(t *testing.T) { - if err := put(acme, tt.kind, tt.value); (err == nil) != tt.ok { - t.Fatalf("err = %v, want ok %v", err, tt.ok) - } - }) - } - - for _, tt := range []struct { - org string - aliases, prov []string - }{ - {"acme", []string{"default", "fast", "own"}, []string{"mine"}}, - {"newco", nil, nil}, - {string(org.DefaultOrgID), []string{"cheap", "default"}, nil}, - } { - t.Run("resolve "+tt.org, func(t *testing.T) { - models, err := server.ModelsFor(ctx, tt.org) - if err != nil { - t.Fatal(err) - } - if got := slices.Sorted(maps.Keys(models.Aliases)); strings.Join(got, ",") != strings.Join(tt.aliases, ",") { - t.Fatalf("aliases = %v, want %v", got, tt.aliases) - } - if got := slices.Sorted(maps.Keys(models.Providers)); strings.Join(got, ",") != strings.Join(tt.prov, ",") { - t.Fatalf("providers = %v, want %v", got, tt.prov) - } - }) - } -} diff --git a/internal/app/controlplane/workflow_org_test.go b/internal/app/controlplane/workflow_org_test.go deleted file mode 100644 index eb12884be..000000000 --- a/internal/app/controlplane/workflow_org_test.go +++ /dev/null @@ -1,209 +0,0 @@ -package controlplane_test - -import ( - "context" - "encoding/json" - "net" - "net/http" - "net/http/httptest" - "os" - "strings" - "testing" - "time" - - "google.golang.org/grpc" - - "github.com/samcharles93/archie-core/internal/app/controlplane" - "github.com/samcharles93/archie-core/internal/config" - pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" - "github.com/samcharles93/archie-core/internal/domain/access" - "github.com/samcharles93/archie-core/internal/domain/identity" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/domain/workflow" - infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" - "github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity" - "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" - "github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps" - "github.com/samcharles93/archie-core/internal/webui" -) - -func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) } - -func TestOrgWorkflowResources(t *testing.T) { - for _, kind := range []string{controlplane.WorkflowDefinitionsKind, controlplane.WorkflowEnablementKind} { - t.Run(kind, func(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - if err := db.BootstrapIdentities(ctx, nil); err != nil { - t.Fatal(err) - } - if err := db.UpgradeDefaultOrg(ctx); err != nil { - t.Fatal(err) - } - steps, err := workflowsteps.NewManager() - if err != nil { - t.Fatal(err) - } - server, err := controlplane.NewServer(db, steps) - if err != nil { - t.Fatal(err) - } - if _, _, err := server.ImportConfig(ctx, config.Config{}); err != nil { - t.Fatal(err) - } - owner, err := db.Create(ctx, identity.Identity{ID: "acme-owner", Kind: identity.KindUser, DisplayName: "Owner", Lifecycle: identity.LifecycleActive}, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: "owner", At: time.Now()}) - if err != nil { - t.Fatal(err) - } - if _, err := db.CreateOrg(ctx, org.Org{ID: "acme", Name: "Acme"}, owner.ID); err != nil { - t.Fatal(err) - } - client := workflowRPC(t, server, db) - principal, err := db.PrincipalFor(ctx, owner.ID) - if err != nil { - t.Fatal(err) - } - other := access.WithPrincipal(ctx, principal) - initial, err := client.ControlPlane().Query(other, &pb.QueryRequest{Kind: kind}) - if err != nil { - t.Fatalf("new org cannot read its shipped %s: %v", kind, err) - } - if initial.Resource.OrgId != "acme" { - t.Fatalf("read org %q", initial.Resource.OrgId) - } - value := []byte(`{"orgs":{"acme":{"disabled":["tdd"]}}}`) - if kind == controlplane.WorkflowDefinitionsKind { - collection := workflow.ShippedDefinitions() - collection.Definitions = append(collection.Definitions, workflow.WorkflowDefinitionEntry{ID: "acme-only", YAML: "id: acme-only\nrepository: none\nsteps:\n - type: workflow.finish\n"}) - value, err = json.Marshal(collection) - if err != nil { - t.Fatal(err) - } - } - changed, err := client.ControlPlane().Command(other, &pb.CommandRequest{Kind: kind, Command: "replace", ValueJson: value, ExpectedVersion: initial.Resource.Version, Actor: "owner", Source: "test", RequestId: "edit"}) - if err != nil { - t.Fatal(err) - } - if _, _, err := server.ImportConfig(ctx, config.Config{}); err != nil { - t.Fatal(err) - } - for _, tc := range []struct { - ctxOrg org.OrgID - want bool - }{{"acme", true}, {org.DefaultOrgID, false}} { - queryCtx := ctx - if tc.ctxOrg == "acme" { - queryCtx = other - } - got, err := client.ControlPlane().Query(queryCtx, &pb.QueryRequest{Kind: kind}) - if err != nil { - t.Fatal(err) - } - if strings.Contains(string(got.Resource.ValueJson), "acme") != tc.want { - t.Fatalf("org %q got %s", tc.ctxOrg, got.Resource.ValueJson) - } - if tc.want && got.Resource.Version != changed.Resource.Version { - t.Fatalf("boot replaced operator's document") - } - } - if kind == controlplane.WorkflowDefinitionsKind { - checkWorkflowDashboard(t, db, client) - } - }) - } -} - -func workflowRPC(t *testing.T, control *controlplane.Server, db *pgstore.TaskDB) *staterpc.Client { - t.Helper() - listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { _ = listener.Close() }) - grants := &staterpc.TaskGrants{} - callers := rpcidentity.Callers{Principals: db} - srv := grpc.NewServer(grpc.ChainUnaryInterceptor(grants.UnaryInterceptor("test-token"), callers.Unary()), grpc.ChainStreamInterceptor(grants.StreamInterceptor("test-token"), callers.Stream())) - staterpc.RegisterServer(srv, staterpc.Deps{ControlPlane: control, Tasks: db, Principals: db, Identities: db}) - t.Cleanup(srv.Stop) - go func() { _ = srv.Serve(listener) }() - client, closeClient, err := staterpc.Dial(listener.Addr().String(), "archie-ui", "test-token") - if err != nil { - t.Fatal(err) - } - t.Cleanup(closeClient) - return client -} - -func TestValidateStoredOrgWorkflows(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - if err := db.BootstrapIdentities(ctx, nil); err != nil { - t.Fatal(err) - } - if err := db.UpgradeDefaultOrg(ctx); err != nil { - t.Fatal(err) - } - steps, err := workflowsteps.NewManager() - if err != nil { - t.Fatal(err) - } - server, err := controlplane.NewServer(db, steps) - if err != nil { - t.Fatal(err) - } - if _, err := db.CreateOrg(ctx, org.Org{ID: "acme", Name: "Acme"}, identity.SystemID); err != nil { - t.Fatal(err) - } - if _, err := db.PutResource(ctx, storecontract.ResourceWrite{OrgID: "acme", Kind: controlplane.WorkflowDefinitionsKind, Value: []byte(`{"definitions":[{"id":"broken","yaml":"id: broken\nsteps:\n - type: nonexistent\n"}]}`), Actor: "owner", Source: "test", RequestID: "invalid"}); err != nil { - t.Fatal(err) - } - if _, err := server.ValidateStored(ctx); err == nil || !strings.Contains(err.Error(), "acme") { - t.Fatalf("invalid org workflow was not reported: %v", err) - } -} - -func checkWorkflowDashboard(t *testing.T, db *pgstore.TaskDB, client *staterpc.Client) { - t.Helper() - ctx := t.Context() - policies := append(access.ShippedOrgPolicies("acme"), access.ShippedOrgPolicies(org.DefaultOrgID)...) - chain := infraaccess.New(policies) - srv := &webui.Server{Store: client, ControlPlane: client.ControlPlane(), Access: chain, Principals: client, Authenticate: func(ctx context.Context, token string) (identity.Identity, error) { - return db.Get(ctx, identity.IdentityID(token)) - }} - handler := srv.Handler() - for _, tc := range []struct { - method, path, body string - status int - }{ - {"GET", "/api/workflows", "", http.StatusOK}, - {"POST", "/api/work-requests", `{"identity":"acme-owner","workflow":"acme-only","title":"Org work","instructions":"Finish"}`, http.StatusCreated}, - } { - r := httptest.NewRequestWithContext(ctx, tc.method, tc.path, strings.NewReader(tc.body)) - r.Header.Set("Authorization", "Bearer acme-owner") - r.Header.Set("Content-Type", "application/json") - r.Header.Set("X-Archie-CSRF", "1") - w := httptest.NewRecorder() - handler.ServeHTTP(w, r) - if w.Code != tc.status { - t.Fatalf("%s %s: %d %s", tc.method, tc.path, w.Code, w.Body.String()) - } - if tc.method == "GET" && !strings.Contains(w.Body.String(), "acme-only") { - t.Fatalf("dashboard omitted the org's workflow: %s", w.Body.String()) - } - if tc.method == "POST" { - var reply struct { - TaskID int64 `json:"task_id"` - } - if err := json.Unmarshal(w.Body.Bytes(), &reply); err != nil { - t.Fatal(err) - } - run, err := db.TaskByID(ctx, reply.TaskID) - if err != nil || run.Org != "acme" || run.Workflow != "acme-only" { - t.Fatalf("admitted task %#v err %v", run, err) - } - } - } -} diff --git a/internal/infrastructure/postgres/binding_dispatch_test.go b/internal/infrastructure/postgres/binding_dispatch_test.go deleted file mode 100644 index b20ea6b4e..000000000 --- a/internal/infrastructure/postgres/binding_dispatch_test.go +++ /dev/null @@ -1,226 +0,0 @@ -package postgres_test - -import ( - "context" - "errors" - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/domain/binding" - "github.com/samcharles93/archie-core/internal/domain/eventtype" - "github.com/samcharles93/archie-core/internal/domain/mapping" - "github.com/samcharles93/archie-core/internal/domain/source" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -// A capture a binding evaluated without dispatching is recorded once and not -// offered to that binding again; a later dispatch of the pair is refused. A -// dispatch records the task it started, and the ledger lists by any end. -func TestEvaluatedCaptureIsNotReoffered(t *testing.T) { - ctx := t.Context() - eda := pgstore.EDA(t, nil) - - eventTypeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: "forge", Name: "push"}) - if err != nil { - t.Fatal(err) - } - mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: "push", EventTypeID: eventTypeID}) - if err != nil { - t.Fatal(err) - } - bindingID, err := eda.InsertBinding(ctx, binding.Binding{ - Name: "on-push", Matcher: binding.Matcher{Source: "forge"}, MappingID: mappingID, Workflow: "tdd", - }) - if err != nil { - t.Fatal(err) - } - if err := eda.ApproveBinding(ctx, bindingID); err != nil { - t.Fatal(err) - } - captureID, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{ - Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(), - }, time.Hour, 100) - if err != nil { - t.Fatal(err) - } - - assertUndispatched(t, eda, 1) - if err := eda.RecordDispatch(ctx, bindingID, 1, captureID, 0, "filtered"); err != nil { - t.Fatalf("record filtered evaluation: %v", err) - } - assertUndispatched(t, eda, 0) - if err := eda.RecordDispatch(ctx, bindingID, 1, captureID, 0, ""); !errors.Is(err, storecontract.ErrAlreadyDispatched) { - t.Fatalf("dispatch after a filtered evaluation = %v, want ErrAlreadyDispatched", err) - } - - // A second capture dispatches; the ledger links it to the task it started. - dispatched, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{ - Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(), - }, time.Hour, 100) - if err != nil { - t.Fatal(err) - } - if err := eda.RecordDispatch(ctx, bindingID, 1, dispatched, 0, ""); err != nil { - t.Fatal(err) - } - if err := eda.SetDispatchTask(ctx, bindingID, dispatched, 42); err != nil { - t.Fatal(err) - } - rows, err := eda.ListDispatches(ctx, storecontract.DispatchFilter{TaskID: 42, Limit: 10}) - if err != nil { - t.Fatal(err) - } - if len(rows) != 1 || rows[0].CaptureID != dispatched || rows[0].BindingID != bindingID { - t.Fatalf("dispatches for task 42 = %+v, want the dispatched capture", rows) - } - all, err := eda.ListDispatches(ctx, storecontract.DispatchFilter{BindingID: bindingID, Limit: 10}) - if err != nil { - t.Fatal(err) - } - if len(all) != 2 { - t.Fatalf("dispatches for the binding = %d, want 2", len(all)) - } -} - -// A sender's retry of one delivery is captured again but dispatches once, -// even when both copies are offered before either is claimed. -func TestRedeliveryDispatchesOnce(t *testing.T) { - ctx := t.Context() - eda := pgstore.EDA(t, nil) - eventTypeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: "forge", Name: "push"}) - if err != nil { - t.Fatal(err) - } - mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: "push", EventTypeID: eventTypeID}) - if err != nil { - t.Fatal(err) - } - bindingID, err := eda.InsertBinding(ctx, binding.Binding{ - Name: "on-push", Matcher: binding.Matcher{Source: "forge"}, MappingID: mappingID, Workflow: "tdd", - }) - if err != nil { - t.Fatal(err) - } - if err := eda.ApproveBinding(ctx, bindingID); err != nil { - t.Fatal(err) - } - capture := func(delivery string) string { - id, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{ - Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(), Delivery: delivery, - }, time.Hour, 100) - if err != nil { - t.Fatal(err) - } - return id - } - - original, retry := capture("delivery-1"), capture("delivery-1") - assertUndispatched(t, eda, 2) - if err := eda.RecordDispatch(ctx, bindingID, 1, original, 0, ""); err != nil { - t.Fatal(err) - } - if err := eda.RecordDispatch(ctx, bindingID, 1, retry, 0, ""); !errors.Is(err, storecontract.ErrAlreadyDispatched) { - t.Fatalf("dispatch of the retry = %v, want ErrAlreadyDispatched", err) - } - assertUndispatched(t, eda, 0) - - capture("delivery-2") - capture("") - assertUndispatched(t, eda, 2) - captures, err := eda.ListCaptures(ctx, 10) - if err != nil { - t.Fatal(err) - } - if len(captures) != 4 { - t.Fatalf("captures = %d, want 4: a retry is still recorded", len(captures)) - } -} - -func assertUndispatched(t *testing.T, eda *postgres.EDA, want int) { - t.Helper() - got, err := eda.ListUndispatchedCaptures(t.Context(), []string{"forge"}, 10) - if err != nil { - t.Fatal(err) - } - if len(got) != want { - t.Fatalf("undispatched captures = %d, want %d", len(got), want) - } -} - -// A binding moves pending -> armed -> paused -> pending, and refuses any other -// transition; a paused binding is offered no captures. -func TestBindingLifecycle(t *testing.T) { - ctx := t.Context() - eda := pgstore.EDA(t, nil) - eventTypeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: "forge", Name: "push"}) - if err != nil { - t.Fatal(err) - } - mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: "push", EventTypeID: eventTypeID}) - if err != nil { - t.Fatal(err) - } - id, err := eda.InsertBinding(ctx, binding.Binding{ - Name: "on-push", Matcher: binding.Matcher{Source: "forge"}, MappingID: mappingID, Workflow: "tdd", - }) - if err != nil { - t.Fatal(err) - } - if _, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{ - Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(), - }, time.Hour, 100); err != nil { - t.Fatal(err) - } - steps := []struct { - name string - apply func(context.Context, string) error - wantErr error - status binding.Status - offered int - }{ - {"a pending binding cannot pause", eda.PauseBinding, storecontract.ErrBindingTransition, binding.StatusPendingApproval, 0}, - {"approve arms it", eda.ApproveBinding, nil, binding.StatusArmed, 1}, - {"an armed binding cannot resume", eda.ResumeBinding, storecontract.ErrBindingTransition, binding.StatusArmed, 1}, - {"pause stops it", eda.PauseBinding, nil, binding.StatusPaused, 0}, - {"a paused binding cannot be approved", eda.ApproveBinding, storecontract.ErrBindingTransition, binding.StatusPaused, 0}, - {"resume returns it to approval", eda.ResumeBinding, nil, binding.StatusPendingApproval, 0}, - } - for _, step := range steps { - if err := step.apply(ctx, id); !errors.Is(err, step.wantErr) { - t.Fatalf("%s: err = %v, want %v", step.name, err, step.wantErr) - } - b, err := eda.GetBinding(ctx, id) - if err != nil { - t.Fatal(err) - } - if b.Status != step.status { - t.Fatalf("%s: status = %q, want %q", step.name, b.Status, step.status) - } - assertUndispatched(t, eda, step.offered) - } - if err := eda.PauseBinding(ctx, "missing"); !errors.Is(err, storecontract.ErrBindingNotFound) { - t.Fatalf("pause of a missing binding = %v, want ErrBindingNotFound", err) - } - - // A source is not deleted while an armed binding fires on it. - if err := eda.InsertSource(ctx, source.Source{Path: "forge", Signing: source.SigningSigned}); err != nil { - t.Fatal(err) - } - if err := eda.ApproveBinding(ctx, id); err != nil { - t.Fatal(err) - } - if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceInUse) { - t.Fatalf("delete of a source an armed binding fires on = %v, want ErrSourceInUse", err) - } - if err := eda.PauseBinding(ctx, id); err != nil { - t.Fatal(err) - } - if err := eda.DeleteSource(ctx, "forge"); err != nil { - t.Fatalf("delete of a source with no armed binding = %v", err) - } - if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceNotFound) { - t.Fatalf("second delete = %v, want ErrSourceNotFound", err) - } -} diff --git a/internal/infrastructure/postgres/capture_list_org_test.go b/internal/infrastructure/postgres/capture_list_org_test.go deleted file mode 100644 index 55484e69e..000000000 --- a/internal/infrastructure/postgres/capture_list_org_test.go +++ /dev/null @@ -1,59 +0,0 @@ -package postgres_test - -import ( - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -// ListCaptures returns only the acting org's captures; a caller with no org -// in context keeps the default org's. -func TestListCapturesIsOrgScoped(t *testing.T) { - pool := pgstore.Pool(t) - eda := postgres.NewEDA(pool, nil) - ctx := t.Context() - - insert := func(source string) string { - id, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{Source: source, Body: "{}"}, time.Hour, 100) - if err != nil { - t.Fatal(err) - } - return id - } - sysID, acmeID := insert("sys-hook"), insert("acme-hook") - // Captures take their org from the source, which current code always - // writes to the default org, so reassign the fixture directly. - if _, err := pool.Exec(ctx, "UPDATE captures SET org_id=$1 WHERE id=$2", "acme", acmeID); err != nil { - t.Fatal(err) - } - - cases := []struct { - name string - orgID org.OrgID - withOrg bool - want string - }{ - {"no org keeps the default org", "", false, sysID}, - {"default org lists only its own", org.DefaultOrgID, true, sysID}, - {"other org lists only its own", "acme", true, acmeID}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - listCtx := ctx - if tc.withOrg { - listCtx = org.WithOrg(ctx, tc.orgID) - } - got, err := eda.ListCaptures(listCtx, 10) - if err != nil { - t.Fatal(err) - } - if len(got) != 1 || got[0].ID != tc.want { - t.Fatalf("captures = %#v, want only %s", got, tc.want) - } - }) - } -} diff --git a/internal/infrastructure/postgres/capture_prune_org_test.go b/internal/infrastructure/postgres/capture_prune_org_test.go deleted file mode 100644 index cfce225b9..000000000 --- a/internal/infrastructure/postgres/capture_prune_org_test.go +++ /dev/null @@ -1,74 +0,0 @@ -package postgres_test - -import ( - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/domain/source" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -// Count retention keeps the newest N captures within the just-inserted -// capture's org, so one org's volume never evicts another org's captures. -func TestCaptureCountPruneStaysInOrg(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - eda := postgres.NewEDA(db.Pool, nil) - for _, owned := range []struct{ path, org string }{{"prune-a", "org-a"}, {"prune-b", "org-b"}} { - if err := eda.InsertSource(ctx, source.Source{Path: owned.path, Signing: source.SigningUnsigned}); err != nil { - t.Fatal(err) - } - // Source ownership is set by another seam; write it directly. - if _, err := db.Pool.Exec(ctx, "UPDATE sources SET org_id=$1 WHERE path=$2", owned.org, owned.path); err != nil { - t.Fatal(err) - } - } - base := time.Now().UTC().Truncate(time.Second) - insert := func(path string, at time.Time) string { - id, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{Source: path, Body: "{}", ReceivedAt: at}, 0, 3) - if err != nil { - t.Fatal(err) - } - return id - } - // Quiet org first: its captures must survive the loud org's volume. - for i := range 2 { - insert("prune-b", base.Add(time.Duration(i)*time.Second)) - } - var loud []string - for i := range 5 { - loud = append(loud, insert("prune-a", base.Add(time.Duration(10+i)*time.Second))) - } - count := func(owner string) int { - var n int - if err := db.Pool.QueryRow(ctx, "SELECT count(*) FROM captures WHERE org_id=$1", owner).Scan(&n); err != nil { - t.Fatal(err) - } - return n - } - present := func(id string) bool { - var n int - if err := db.Pool.QueryRow(ctx, "SELECT count(*) FROM captures WHERE id=$1", id).Scan(&n); err != nil { - t.Fatal(err) - } - return n == 1 - } - if got := count("org-b"); got != 2 { - t.Fatalf("quiet org kept %d captures, want 2", got) - } - if got := count("org-a"); got != 3 { - t.Fatalf("loud org kept %d captures, want 3", got) - } - for _, id := range loud[:2] { - if present(id) { - t.Fatalf("loud org's oldest capture %s survived past the cap", id) - } - } - for _, id := range loud[2:] { - if !present(id) { - t.Fatalf("loud org's newest capture %s missing", id) - } - } -} diff --git a/internal/infrastructure/postgres/dispatch_org_test.go b/internal/infrastructure/postgres/dispatch_org_test.go deleted file mode 100644 index 92bee20d7..000000000 --- a/internal/infrastructure/postgres/dispatch_org_test.go +++ /dev/null @@ -1,164 +0,0 @@ -package postgres_test - -import ( - "net" - "net/http" - "net/http/httptest" - "strings" - "testing" - "time" - - "google.golang.org/grpc" - - "github.com/samcharles93/archie-core/internal/domain/binding" - "github.com/samcharles93/archie-core/internal/domain/eventtype" - "github.com/samcharles93/archie-core/internal/domain/identity" - "github.com/samcharles93/archie-core/internal/domain/mapping" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/source" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/infrastructure/captureintake" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" -) - -func TestPendingDispatchInCallerOrg(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - if err := db.BootstrapIdentities(ctx, nil); err != nil { - t.Fatal(err) - } - if err := db.UpgradeDefaultOrg(ctx); err != nil { - t.Fatal(err) - } - for _, who := range []identity.Identity{{ID: "acme-owner", Kind: identity.KindUser, DisplayName: "Owner", Lifecycle: identity.LifecycleActive}, {ID: "acme-bot", Kind: identity.KindBot, DisplayName: "bot", Lifecycle: identity.LifecycleActive}} { - if _, err := db.Create(ctx, who, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: string(who.ID)}); err != nil { - t.Fatal(err) - } - } - if _, err := db.CreateOrg(ctx, org.Org{ID: "acme", Name: "Acme"}, "acme-owner"); err != nil { - t.Fatal(err) - } - if err := db.AssignAgent(ctx, org.AgentAssignment{IdentityID: "acme-bot", OrgID: "acme"}); err != nil { - t.Fatal(err) - } - eda := postgres.NewEDA(db.Pool, nil) - client := captureRPC(t, eda, db) - receiver := &captureintake.Receiver{Captures: client, Sources: client, Retention: time.Hour, MaxEvents: 100} - mux := http.NewServeMux() - receiver.Register(mux) - ids := map[org.OrgID]string{} - bindings := map[org.OrgID]string{} - paths := []string{} - for _, owner := range []org.OrgID{org.DefaultOrgID, "acme"} { - path := "webhook-" + string(owner) - paths = append(paths, path) - if err := eda.InsertSource(org.WithOrg(ctx, owner), source.Source{Path: path, Signing: source.SigningUnsigned}); err != nil { - t.Fatal(err) - } - // Workspace selection is not yet a write path, so only the workspace - // needs a fixture fixup; the source org comes from the insert itself. - if _, err := db.Pool.Exec(ctx, "UPDATE sources SET workspace_id='incoming' WHERE path=$1", path); err != nil { - t.Fatal(err) - } - typeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: path, Name: string(owner), Rule: eventtype.Rule{Headers: []eventtype.HeaderCondition{{Name: "X-Org", Value: string(owner)}}}}) - if err != nil { - t.Fatal(err) - } - mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: string(owner), EventTypeID: typeID}) - if err != nil { - t.Fatal(err) - } - bindingID, err := eda.InsertBinding(ctx, binding.Binding{Name: string(owner), Matcher: binding.Matcher{Source: path}, MappingID: mappingID, Workflow: "deploy"}) - if err != nil { - t.Fatal(err) - } - if err := eda.ApproveBinding(ctx, bindingID); err != nil { - t.Fatal(err) - } - // Catalogue writes still default to the system org; capture ownership - // must come from the persisted source, never a caller-supplied org. - for _, record := range []struct{ table, id string }{{"event_types", typeID}, {"mappings", mappingID}, {"bindings", bindingID}} { - if _, err := db.Pool.Exec(ctx, "UPDATE "+record.table+" SET org_id=$1 WHERE id=$2", owner, record.id); err != nil { - t.Fatal(err) - } - } - request := httptest.NewRequestWithContext(ctx, http.MethodPost, "/webhooks/capture/"+path, strings.NewReader("{}")) - request.Header.Set("X-Org", string(owner)) - response := httptest.NewRecorder() - mux.ServeHTTP(response, request) - if response.Code != http.StatusAccepted { - t.Fatalf("capture: %d %s", response.Code, response.Body.String()) - } - var captureID, captureOrg, workspace string - if err := db.Pool.QueryRow(ctx, "SELECT id, org_id, workspace_id FROM captures WHERE source=$1", path).Scan(&captureID, &captureOrg, &workspace); err != nil { - t.Fatal(err) - } - if captureOrg != string(owner) || workspace != "incoming" { - t.Fatalf("capture org %s workspace %s, want %s/incoming", captureOrg, workspace, owner) - } - ids[owner] = captureID - bindings[owner] = bindingID - } - for _, owner := range []org.OrgID{org.DefaultOrgID, "acme"} { - t.Run(string(owner), func(t *testing.T) { - captures, err := eda.ListUndispatchedCaptures(org.WithOrg(t.Context(), owner), paths, 1) - if err != nil || len(captures) != 1 || captures[0].ID != ids[owner] { - t.Fatalf("org %s got captures %#v err %v", owner, captures, err) - } - bindingID := bindings[owner] - if err := client.RecordDispatch(t.Context(), bindingID, 1, ids[owner], 0, ""); err != nil { - t.Fatal(err) - } - taskIdentity := "" - if owner == "acme" { - taskIdentity = "acme-bot" - } - run, err := client.EnqueueBindingTask(t.Context(), "", "", "Org dispatch", "", "deploy", taskIdentity, bindingID, 1, nil) - if err != nil || run.Org != owner { - t.Fatalf("binding task %#v err %v", run, err) - } - if err := client.SetDispatchTask(t.Context(), bindingID, ids[owner], run.ID); err != nil { - t.Fatal(err) - } - pending, err := eda.ListUndispatchedCaptures(org.WithOrg(t.Context(), owner), paths, 1) - if err != nil || len(pending) != 0 { - t.Fatalf("dispatched capture still pending: %#v err %v", pending, err) - } - }) - } - t.Run("unknown source", func(t *testing.T) { - id, err := client.InsertCapture(org.WithOrg(t.Context(), "acme"), storecontract.CapturedEvent{Source: "unknown", Body: "{}"}, time.Hour, 100) - if err != nil { - t.Fatal(err) - } - var owner, workspace string - if err := db.Pool.QueryRow(t.Context(), "SELECT org_id,workspace_id FROM captures WHERE id=$1", id).Scan(&owner, &workspace); err != nil { - t.Fatal(err) - } - if owner != string(org.DefaultOrgID) || workspace != string(org.DefaultWorkspaceID) { - t.Fatalf("unknown source owner %s/%s", owner, workspace) - } - }) -} - -func captureRPC(t *testing.T, eda *postgres.EDA, db *pgstore.TaskDB) *staterpc.Client { - t.Helper() - listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { _ = listener.Close() }) - grants := &staterpc.TaskGrants{} - srv := grpc.NewServer(grpc.ChainUnaryInterceptor(grants.UnaryInterceptor("test-token")), grpc.ChainStreamInterceptor(grants.StreamInterceptor("test-token"))) - staterpc.RegisterServer(srv, staterpc.Deps{Tasks: db, Captures: eda, Sources: eda, BindingDispatcher: eda, BindingTaskCreator: db}) - t.Cleanup(srv.Stop) - go func() { _ = srv.Serve(listener) }() - client, closeClient, err := staterpc.Dial(listener.Addr().String(), "archied", "test-token") - if err != nil { - t.Fatal(err) - } - t.Cleanup(closeClient) - return client -} diff --git a/internal/infrastructure/postgres/eda_org_test.go b/internal/infrastructure/postgres/eda_org_test.go deleted file mode 100644 index bbe9f90e9..000000000 --- a/internal/infrastructure/postgres/eda_org_test.go +++ /dev/null @@ -1,106 +0,0 @@ -package postgres_test - -import ( - "context" - "testing" - - "github.com/samcharles93/archie-core/internal/domain/binding" - "github.com/samcharles93/archie-core/internal/domain/eventtype" - "github.com/samcharles93/archie-core/internal/domain/mapping" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/source" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -// An org's sources, event types, mappings and bindings are invisible to a -// request scoped to another org, which can neither change nor delete them. -// An unscoped internal service still sees them. -func TestEDARecordsStayInTheirOrg(t *testing.T) { - scoped := func(id org.OrgID) context.Context { - return org.WithScope(org.WithOrg(t.Context(), id), id) - } - acme := scoped("acme") - eda := postgres.NewEDA(pgstore.Pool(t), nil) - if err := eda.InsertSource(acme, source.Source{Path: "acme-hook", Signing: source.SigningUnsigned}); err != nil { - t.Fatal(err) - } - typeID, err := eda.InsertEventType(acme, eventtype.EventType{Source: "acme-hook", Name: "push"}) - if err != nil { - t.Fatal(err) - } - mappingID, err := eda.InsertMapping(acme, mapping.Mapping{Name: "push", EventTypeID: typeID}) - if err != nil { - t.Fatal(err) - } - bindingID, err := eda.InsertBinding(acme, binding.Binding{Name: "deploy", Matcher: binding.Matcher{Source: "acme-hook"}, MappingID: mappingID, Workflow: "deploy"}) - if err != nil { - t.Fatal(err) - } - - for _, tt := range []struct { - name string - ctx func() context.Context //nolint:containedctx // table input, not stored state - sees bool - }{ - {"the owning org", func() context.Context { return acme }, true}, - {"another org", func() context.Context { return scoped("other") }, false}, - {"an internal service", t.Context, true}, - } { - t.Run(tt.name, func(t *testing.T) { - ctx := tt.ctx() - sources, err := eda.ListSources(ctx) - if err != nil { - t.Fatal(err) - } - src, err := eda.GetSource(ctx, "acme-hook") - if err != nil { - t.Fatal(err) - } - types, err := eda.ListEventTypes(ctx) - if err != nil { - t.Fatal(err) - } - mappings, err := eda.ListMappings(ctx) - if err != nil { - t.Fatal(err) - } - m, err := eda.GetMapping(ctx, mappingID) - if err != nil { - t.Fatal(err) - } - bindings, err := eda.ListBindings(ctx) - if err != nil { - t.Fatal(err) - } - b, err := eda.GetBinding(ctx, bindingID) - if err != nil { - t.Fatal(err) - } - seen := []bool{len(sources) == 1, src != nil, len(types) == 1, len(mappings) == 1, m != nil, len(bindings) == 1, b != nil} - for i, s := range seen { - if s != tt.sees { - t.Fatalf("read %d visible = %v, want %v", i, s, tt.sees) - } - } - if tt.sees { - return - } - if err := eda.ApproveBinding(ctx, bindingID); err == nil { - t.Fatal("approved another org's binding") - } - if err := eda.SetSourceSecret(ctx, "acme-hook", "stolen"); err == nil { - t.Fatal("rotated another org's source secret") - } - if err := eda.DeleteBinding(ctx, bindingID); err == nil { - t.Fatal("deleted another org's binding") - } - if err := eda.DeleteMapping(ctx, mappingID); err == nil { - t.Fatal("deleted another org's mapping") - } - if err := eda.DeleteEventType(ctx, typeID); err == nil { - t.Fatal("deleted another org's event type") - } - }) - } -} diff --git a/internal/infrastructure/postgres/org_sys_migration_test.go b/internal/infrastructure/postgres/org_sys_migration_test.go deleted file mode 100644 index 138888b66..000000000 --- a/internal/infrastructure/postgres/org_sys_migration_test.go +++ /dev/null @@ -1,234 +0,0 @@ -package postgres_test - -import ( - "database/sql" - "fmt" - "slices" - "strings" - "testing" - - "github.com/jackc/pgx/v5/pgxpool" - "github.com/jackc/pgx/v5/stdlib" - "github.com/pressly/goose/v3" - - "github.com/samcharles93/archie-core/internal/domain/identity" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" -) - -// orgSysTables are the tables whose org_id defaulted to 'default' before the -// rename; the migration points them at 'org-sys'. -var orgSysTables = []string{ - "bindings", "captures", "event_types", "events", "mappings", - "resource_history", "resources", "sources", "step_executions", "tasks", - "tool_calls", "transitions", -} - -// TestOrgSysMigrationMovesExistingDefaultRows exercises the rename on the path -// a real install takes: a database at the previous schema with rows already -// under the old org id. -func TestOrgSysMigrationMovesExistingDefaultRows(t *testing.T) { - ctx := t.Context() - pool, err := postgres.Open(ctx, pgtest.URL(t)) - if err != nil { - t.Fatal(err) - } - t.Cleanup(pool.Close) - db := stdlib.OpenDBFromPool(pool) - t.Cleanup(func() { _ = db.Close() }) - provider, err := goose.NewProvider(goose.DialectPostgres, db, postgres.Migrations()) - if err != nil { - t.Fatal(err) - } - - if _, err := provider.UpTo(ctx, 32); err != nil { - t.Fatalf("migrate to 32: %v", err) - } - seedDefaultOrg(t, pool) - if _, err := provider.UpTo(ctx, 33); err != nil { - t.Fatalf("migrate to 33: %v", err) - } - - if name := orgName(t, pool, "org-sys"); name != "System" { - t.Fatalf("org-sys name = %q, want System", name) - } - assertNoOrg(t, pool, "default") - - tables := orgIDTables(t, pool) - for _, name := range tables { - var left int - if err := pool.QueryRow(ctx, fmt.Sprintf("SELECT count(*) FROM %s WHERE org_id = 'default'", name)).Scan(&left); err != nil { - t.Fatal(err) - } - if left != 0 { - t.Fatalf("%s left %d rows in the old org", name, left) - } - } - // The seeded rows are on the new org, so the move was not a delete. - for _, name := range []string{ - "workspaces", "tasks", "events", "resources", "step_executions", "access_policies", - "installed_packages", "installed_package_requirements", "installed_package_contributions", - } { - var moved int - if err := pool.QueryRow(ctx, fmt.Sprintf("SELECT count(*) FROM %s WHERE org_id = 'org-sys'", name)).Scan(&moved); err != nil { - t.Fatal(err) - } - if moved == 0 { - t.Fatalf("%s moved no rows to org-sys", name) - } - } - // The new default is the renamed org, on exactly the tables that had one. - for _, name := range tables { - var def sql.NullString - if err := pool.QueryRow(ctx, `SELECT column_default FROM information_schema.columns WHERE table_name = $1 AND column_name = 'org_id'`, name).Scan(&def); err != nil { - t.Fatal(err) - } - want := slices.Contains(orgSysTables, name) - got := def.Valid && strings.Contains(def.String, "'org-sys'") - if got != want { - t.Fatalf("%s org_id default = %q, want org-sys = %v", name, def.String, want) - } - } - assertForeignKey(t, pool, "installed_package_requirements", "installed_package_requirements_org_id_required_name_fkey") - assertForeignKey(t, pool, "installed_package_contributions", "installed_package_contributions_org_id_package_name_fkey") - - // The down migration is the exact reverse. - if _, err := provider.DownTo(ctx, 32); err != nil { - t.Fatalf("migrate down to 32: %v", err) - } - if name := orgName(t, pool, "default"); name != "Default" { - t.Fatalf("default org name = %q, want Default", name) - } - assertNoOrg(t, pool, "org-sys") -} - -// TestOrgOperatorUpgrade pins the operator phase: a fresh install and an -// existing one each end with exactly one person owner of org-sys, and running -// the upgrade again adds no second. -func TestOrgOperatorUpgrade(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - - if err := db.UpgradeDefaultOrg(ctx); err != nil { - t.Fatalf("fresh upgrade: %v", err) - } - assertOneOperator(t, db.Pool) - if err := db.UpgradeDefaultOrg(ctx); err != nil { - t.Fatalf("second upgrade: %v", err) - } - assertOneOperator(t, db.Pool) - - // An existing install that ran the first two phases but never the operator - // phase: drop the phase's ledger row and what it wrote, then upgrade again. - if _, err := db.Pool.Exec(ctx, `DELETE FROM org_upgrades WHERE phase = 'operator'`); err != nil { - t.Fatalf("reset operator phase: %v", err) - } - for _, sql := range []string{ - `DELETE FROM memberships WHERE identity_id = $1`, - `DELETE FROM identities WHERE id = $1`, - } { - if _, err := db.Pool.Exec(ctx, sql, string(identity.OperatorID())); err != nil { - t.Fatalf("reset operator phase: %v", err) - } - } - if err := db.UpgradeDefaultOrg(ctx); err != nil { - t.Fatalf("upgrade on an existing install: %v", err) - } - assertOneOperator(t, db.Pool) -} - -func assertOneOperator(t *testing.T, pool *pgxpool.Pool) { - t.Helper() - var owners int - if err := pool.QueryRow(t.Context(), ` - SELECT count(*) FROM memberships m - JOIN identities i ON i.id = m.identity_id - WHERE m.org_id = 'org-sys' AND m.workspace_id IS NULL AND m.role = 'owner' AND i.kind = 'user' - `).Scan(&owners); err != nil { - t.Fatal(err) - } - if owners != 1 { - t.Fatalf("person owners of org-sys = %d, want 1", owners) - } - var kind string - if err := pool.QueryRow(t.Context(), `SELECT kind FROM identities WHERE id = $1`, string(identity.OperatorID())).Scan(&kind); err != nil { - t.Fatalf("operator identity: %v", err) - } - if kind != string(identity.KindUser) { - t.Fatalf("operator kind = %q, want %q", kind, identity.KindUser) - } -} - -func seedDefaultOrg(t *testing.T, pool *pgxpool.Pool) { - t.Helper() - ctx := t.Context() - statements := []string{ - `INSERT INTO orgs (id, name) VALUES ('default', 'Default')`, - `INSERT INTO workspaces (id, org_id, name) VALUES ('default', 'default', 'Default')`, - `INSERT INTO tasks (owner, repo, issue_number, identity, org_id, workspace_id) VALUES ('o', 'r', 1, 'sam', 'default', 'default')`, - `INSERT INTO events (at, kind, org_id, workspace_id) VALUES (now(), 'task_started', 'default', 'default')`, - `INSERT INTO resources (org_id, kind, value, version, updated_at) VALUES ('default', 'workflows', '\x00', 1, now())`, - `INSERT INTO step_executions (org_id, execution_id, attempt, kind, name) VALUES ('default', 1, 1, 'stage', 'build')`, - `INSERT INTO access_policies (level, org_id, policy_id, text) VALUES ('org', 'default', 'p', 'allow')`, - `INSERT INTO installed_packages (org_id, name, reference, digest, descriptor, layer) VALUES ('default', 'pkg', 'ref', 'sha256:' || repeat('a', 64), '{}', '\x00')`, - `INSERT INTO installed_packages (org_id, name, reference, digest, descriptor, layer) VALUES ('default', 'dep', 'ref', 'sha256:' || repeat('b', 64), '{}', '\x00')`, - `INSERT INTO installed_package_requirements (org_id, package_name, required_name, required_digest) VALUES ('default', 'pkg', 'dep', 'sha256:' || repeat('b', 64))`, - `INSERT INTO installed_package_contributions (org_id, package_name, family, entry_id) VALUES ('default', 'pkg', 'fam', 'e1')`, - } - for _, sql := range statements { - if _, err := pool.Exec(ctx, sql); err != nil { - t.Fatalf("seed %q: %v", sql, err) - } - } -} - -// orgIDTables lists every table that carries an org_id column. -func orgIDTables(t *testing.T, pool *pgxpool.Pool) []string { - t.Helper() - rows, err := pool.Query(t.Context(), `SELECT table_name FROM information_schema.columns WHERE column_name = 'org_id' ORDER BY table_name`) - if err != nil { - t.Fatal(err) - } - defer rows.Close() - var tables []string - for rows.Next() { - var name string - if err := rows.Scan(&name); err != nil { - t.Fatal(err) - } - tables = append(tables, name) - } - return tables -} - -func orgName(t *testing.T, pool *pgxpool.Pool, id string) string { - t.Helper() - var name string - if err := pool.QueryRow(t.Context(), `SELECT name FROM orgs WHERE id = $1`, id).Scan(&name); err != nil { - t.Fatalf("org %s: %v", id, err) - } - return name -} - -func assertNoOrg(t *testing.T, pool *pgxpool.Pool, id string) { - t.Helper() - var n int - if err := pool.QueryRow(t.Context(), `SELECT count(*) FROM orgs WHERE id = $1`, id).Scan(&n); err != nil { - t.Fatal(err) - } - if n != 0 { - t.Fatalf("org %s still exists", id) - } -} - -func assertForeignKey(t *testing.T, pool *pgxpool.Pool, table, name string) { - t.Helper() - var n int - if err := pool.QueryRow(t.Context(), `SELECT count(*) FROM pg_constraint WHERE contype = 'f' AND conrelid = $1::regclass AND conname = $2`, table, name).Scan(&n); err != nil { - t.Fatal(err) - } - if n != 1 { - t.Fatalf("foreign key %s on %s = %d, want 1", name, table, n) - } -} diff --git a/internal/infrastructure/postgres/orgs_test.go b/internal/infrastructure/postgres/orgs_test.go deleted file mode 100644 index d304a186f..000000000 --- a/internal/infrastructure/postgres/orgs_test.go +++ /dev/null @@ -1,75 +0,0 @@ -package postgres_test - -import ( - "errors" - "testing" - - "github.com/samcharles93/archie-core/internal/domain/identity" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -// TestOrgStoreMembers pins the org membership queries: a role change updates -// the existing membership, the list joins the identity, removal deletes, agent -// assignment lists, and an unknown role is refused naming it. -func TestOrgStoreMembers(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - for _, id := range []string{"sam", "bot"} { - kind := identity.KindUser - name := "Sam" - if id == "bot" { - kind, name = identity.KindBot, "Bot" - } - if _, err := db.Create(ctx, identity.Identity{ID: identity.IdentityID(id), Kind: kind, DisplayName: name, Lifecycle: identity.LifecycleActive}, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: "id-" + id}); err != nil { - t.Fatalf("create identity %s: %v", id, err) - } - } - if _, err := db.CreateOrg(ctx, org.Org{ID: "acme", Name: "Acme"}, "sam"); err != nil { - t.Fatalf("create org: %v", err) - } - - if err := db.EnsureMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme", Role: org.RoleDeveloper}); err != nil { - t.Fatalf("ensure membership: %v", err) - } - if err := db.EnsureMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme", Role: org.RoleAdmin}); err != nil { - t.Fatalf("change membership: %v", err) - } - members, err := db.ListMembers(ctx, "acme") - if err != nil { - t.Fatalf("list members: %v", err) - } - if len(members) != 1 { - t.Fatalf("members = %d, want 1", len(members)) - } - got := members[0] - if got.IdentityID != "sam" || got.Kind != identity.KindUser || got.DisplayName != "Sam" || got.Role != org.RoleAdmin { - t.Fatalf("member = %#v", got) - } - - if err := db.RemoveMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme"}); err != nil { - t.Fatalf("remove membership: %v", err) - } - if members, err = db.ListMembers(ctx, "acme"); err != nil || len(members) != 0 { - t.Fatalf("after remove: members = %d, err %v", len(members), err) - } - // Removing again is not an error: the target is simply gone. - if err := db.RemoveMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme"}); err != nil { - t.Fatalf("second remove: %v", err) - } - - if err := db.AssignAgent(ctx, org.AgentAssignment{IdentityID: "bot", OrgID: "acme"}); err != nil { - t.Fatalf("assign agent: %v", err) - } - agents, err := db.ListOrgAgents(ctx, "acme") - if err != nil { - t.Fatalf("list agents: %v", err) - } - if len(agents) != 1 || agents[0].IdentityID != "bot" { - t.Fatalf("agents = %#v", agents) - } - - if err := db.EnsureMembership(ctx, org.Membership{IdentityID: "sam", OrgID: "acme", Role: "superuser"}); !errors.Is(err, org.ErrInvalidRole) { - t.Fatalf("unknown role err = %v, want ErrInvalidRole", err) - } -} diff --git a/internal/infrastructure/postgres/pgstore/pgstore.go b/internal/infrastructure/postgres/pgstore/pgstore.go deleted file mode 100644 index 12ef3aa53..000000000 --- a/internal/infrastructure/postgres/pgstore/pgstore.go +++ /dev/null @@ -1,61 +0,0 @@ -// Package pgstore opens migrated PostgreSQL stores on a throwaway pgtest -// database, for tests outside internal/infrastructure/postgres. A package -// using it calls pgtest.Main from TestMain. -package pgstore - -import ( - "testing" - - "github.com/jackc/pgx/v5/pgxpool" - - "github.com/samcharles93/archie-core/internal/infrastructure/bindingcipher" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" -) - -// TaskDB is the State Store's task database: the task, event, status and -// identity surface plus the control-plane resources, over one pool. -type TaskDB struct { - *postgres.Store - *postgres.Resources - Pool *pgxpool.Pool -} - -// Pool opens a pool on a fresh migrated database, closed when t ends. -func Pool(t testing.TB) *pgxpool.Pool { - t.Helper() - return PoolAt(t, pgtest.URL(t)) -} - -// PoolAt opens a pool on url and migrates it, closed when t ends. Opening a -// second pool on the same url is how a test models a restart. -func PoolAt(t testing.TB, url string) *pgxpool.Pool { - t.Helper() - pool, err := postgres.Open(t.Context(), url) - if err != nil { - t.Fatalf("pgstore: open: %v", err) - } - t.Cleanup(pool.Close) - if err := postgres.Migrate(t.Context(), pool, postgres.Migrations()); err != nil { - t.Fatalf("pgstore: migrate: %v", err) - } - return pool -} - -// On returns the task database over pool. -func On(pool *pgxpool.Pool) *TaskDB { - return &TaskDB{Store: postgres.New(pool), Resources: postgres.NewResources(pool), Pool: pool} -} - -// Open returns the task database on a fresh migrated database. -func Open(t testing.TB) *TaskDB { - t.Helper() - return On(Pool(t)) -} - -// EDA returns the event-capture store on a fresh migrated database. A nil -// cipher keeps binding secrets in plaintext. -func EDA(t testing.TB, cipher bindingcipher.BindingCipher) *postgres.EDA { - t.Helper() - return postgres.NewEDA(Pool(t), cipher) -} diff --git a/internal/infrastructure/postgres/pgtest/pgtest.go b/internal/infrastructure/postgres/pgtest/pgtest.go deleted file mode 100644 index bcf8b25b0..000000000 --- a/internal/infrastructure/postgres/pgtest/pgtest.go +++ /dev/null @@ -1,85 +0,0 @@ -// Package pgtest runs a throwaway PostgreSQL 18 container for one test binary -// and gives each test its own database on it. -// -// func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) } -// -// It needs a running Docker daemon. -package pgtest - -import ( - "context" - "fmt" - "net/url" - "os" - "sync/atomic" - "testing" - - "github.com/jackc/pgx/v5" - "github.com/testcontainers/testcontainers-go" - tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres" -) - -// image is the server tests run against: the same major version archie -// requires. -const image = "postgres:18" - -var ( - adminURL string - nextDB atomic.Int64 -) - -// Main starts the container, runs the tests and removes the container, -// returning the exit code. A container that cannot start is a test failure, -// not a skip. -func Main(m *testing.M) int { - ctx := context.Background() - container, err := tcpostgres.Run(ctx, image, - tcpostgres.WithDatabase("postgres"), - tcpostgres.WithUsername("archie"), - tcpostgres.WithPassword("archie"), - tcpostgres.BasicWaitStrategies(), - ) - defer func() { _ = testcontainers.TerminateContainer(container) }() - if err != nil { - fmt.Fprintf(os.Stderr, "pgtest: start %s (is Docker running?): %v\n", image, err) - return 1 - } - adminURL, err = container.ConnectionString(ctx, "sslmode=disable") - if err != nil { - fmt.Fprintf(os.Stderr, "pgtest: connection string: %v\n", err) - return 1 - } - return m.Run() -} - -// URL creates an empty database for t, drops it when t ends, and returns a -// connection URL for it. -func URL(t testing.TB) string { - t.Helper() - if adminURL == "" { - t.Fatal("pgtest: no container running; call pgtest.Main from TestMain") - } - name := fmt.Sprintf("t%d", nextDB.Add(1)) - exec(t, "CREATE DATABASE "+name) - t.Cleanup(func() { exec(t, "DROP DATABASE "+name+" WITH (FORCE)") }) - - u, err := url.Parse(adminURL) - if err != nil { - t.Fatalf("pgtest: parse connection string: %v", err) - } - u.Path = "/" + name - return u.String() -} - -func exec(t testing.TB, sql string) { - t.Helper() - ctx := context.Background() - conn, err := pgx.Connect(ctx, adminURL) - if err != nil { - t.Fatalf("pgtest: connect: %v", err) - } - defer func() { _ = conn.Close(ctx) }() - if _, err := conn.Exec(ctx, sql); err != nil { - t.Fatalf("pgtest: %s: %v", sql, err) - } -} diff --git a/internal/infrastructure/postgres/presence_checks_test.go b/internal/infrastructure/postgres/presence_checks_test.go deleted file mode 100644 index 593a3287a..000000000 --- a/internal/infrastructure/postgres/presence_checks_test.go +++ /dev/null @@ -1,34 +0,0 @@ -package postgres_test - -import ( - "reflect" - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/domain/health" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -func TestPresenceReplacesDependencyChecks(t *testing.T) { - store := postgres.New(pgstore.Pool(t)) - record := storecontract.Presence{Service: "gateway", InstanceID: "instance", StartedAt: time.Now(), ReportedAt: time.Now()} - for _, checks := range [][]health.Component{ - {{Name: "nats", Status: health.StatusDegraded, Detail: "disconnected"}}, - {{Name: "nats", Status: health.StatusOK, Ready: true}, {Name: "state_store", Status: health.StatusOK, Ready: true}}, - {}, - } { - record.Checks = checks - if err := store.PutPresence(t.Context(), record); err != nil { - t.Fatal(err) - } - rows, err := store.ListPresence(t.Context()) - if err != nil { - t.Fatal(err) - } - if len(rows) != 1 || !reflect.DeepEqual(rows[0].Checks, checks) { - t.Fatalf("presence = %+v; want replacement checks %+v", rows, checks) - } - } -} diff --git a/internal/infrastructure/postgres/resume_test.go b/internal/infrastructure/postgres/resume_test.go deleted file mode 100644 index 73fc31e60..000000000 --- a/internal/infrastructure/postgres/resume_test.go +++ /dev/null @@ -1,93 +0,0 @@ -package postgres_test - -import ( - "encoding/json" - "errors" - "os" - "testing" - - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/domain/workflow/task" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" - "github.com/samcharles93/archie-core/internal/taskstate" -) - -func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) } - -// A retry that resumes starts from the results its After stage recorded in -// its last run, and is refused when that run did not move past the stage. -func TestRetryTaskResume(t *testing.T) { - cases := []struct { - name string - resume task.Resume - want string - wantErr error - }{ - {name: "after a completed stage", resume: task.Resume{From: "check", After: "analyse"}, want: `{"analyse":{"summary":"found it"}}`}, - {name: "from the first stage", resume: task.Resume{From: "analyse"}, want: `{}`}, - {name: "after a stage that failed", resume: task.Resume{From: "publish", After: "check"}, wantErr: storecontract.ErrResumeIncomplete}, - {name: "after a stage that never ran", resume: task.Resume{From: "publish", After: "nowhere"}, wantErr: storecontract.ErrResumeIncomplete}, - {name: "plain retry", want: `{}`}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - if _, err := db.EnqueueChatTask(ctx, "", "", "resume", "", "tdd", "", "", nil); err != nil { - t.Fatal(err) - } - run, err := db.ClaimNext(ctx) - if err != nil || run == nil { - t.Fatalf("claim: %v", err) - } - finish := func(name string, to taskstate.StepStatus, results string) { - id, _, err := db.StartStep(ctx, task.StepStart{ExecutionID: run.ID, Attempt: run.Attempt, Kind: task.StepKindStage, Name: name}) - if err != nil { - t.Fatal(err) - } - var raw []byte - if results != "" { - raw = []byte(results) - } - if _, err := db.FinishStep(ctx, task.StepFinish{StepID: id, ExecutionID: run.ID, From: taskstate.StepRunning, To: to, Results: raw}); err != nil { - t.Fatal(err) - } - } - finish("analyse", taskstate.StepSucceeded, `{"analyse":{"summary":"found it"}}`) - finish("check", taskstate.StepFailed, "") - if err := db.ParkTask(ctx, run.ID, taskstate.Running, "stage check failed", taskstate.ParkNeedsHuman); err != nil { - t.Fatal(err) - } - - err = db.RetryTask(ctx, run.ID, taskstate.Parked, "", string(taskstate.RetryRefreshOntoBase), tc.resume) - if !errors.Is(err, tc.wantErr) { - t.Fatalf("RetryTask error = %v, want %v", err, tc.wantErr) - } - got, err := db.TaskByID(ctx, run.ID) - if err != nil { - t.Fatal(err) - } - if tc.wantErr != nil { - if got.Status != taskstate.Parked { - t.Fatalf("refused retry left the task %s, want parked", got.Status) - } - return - } - if got.ResumeFrom != tc.resume.From || !sameJSON(t, got.ResumeResults, tc.want) { - t.Fatalf("resume = %q %s, want %q %s", got.ResumeFrom, got.ResumeResults, tc.resume.From, tc.want) - } - }) - } -} - -func sameJSON(t *testing.T, got []byte, want string) bool { - t.Helper() - var a, b any - if json.Unmarshal(got, &a) != nil || json.Unmarshal([]byte(want), &b) != nil { - return false - } - ga, _ := json.Marshal(a) - gb, _ := json.Marshal(b) - return string(ga) == string(gb) -} diff --git a/internal/infrastructure/postgres/review_queue_test.go b/internal/infrastructure/postgres/review_queue_test.go deleted file mode 100644 index 45defe625..000000000 --- a/internal/infrastructure/postgres/review_queue_test.go +++ /dev/null @@ -1,139 +0,0 @@ -package postgres_test - -import ( - "testing" - - "github.com/samcharles93/archie-core/internal/domain/workflow" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/taskstate" -) - -// A review that arrives while a remediation owns the task is queued behind it -// and remediated when that run finishes, in arrival order. A re-delivered -// review is absorbed rather than queued to run a second time. -func TestSecondReviewDuringRemediationIsQueued(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - id := openTaskAtPROpen(t, db) - - first, second := reviewUnit(t, 11), reviewUnit(t, 22) - - if err := db.BeginRemediation(ctx, id, first); err != nil { - t.Fatalf("begin first remediation: %v", err) - } - if err := db.BeginRemediation(ctx, id, second); err != nil { - t.Fatalf("review arriving during remediation was refused, not queued: %v", err) - } - if err := db.BeginRemediation(ctx, id, first); err != nil { - t.Fatalf("re-delivered review was refused: %v", err) - } - assertActiveReview(t, db, id, first) - - // An inline comment on the waiting review is collected into its queued - // unit rather than dropped, and travels with it when it is promoted. - comment := workflow.ReviewUnitComment{CommentID: 5, Path: "main.go", Line: 3, Body: "rename this"} - if err := db.UpdateReviewPayload(ctx, id, reviewUnitWith(t, 22, comment)); err != nil { - t.Fatalf("comment on the queued review was refused: %v", err) - } - if err := db.UpdateReviewPayload(ctx, id, reviewUnitWith(t, 22, comment)); err != nil { - t.Fatalf("re-delivered comment was refused: %v", err) - } - - // The first round runs and finishes; the second, still waiting, is - // promoted behind it with its comment. - runRemediationRound(t, db, id) - assertActiveReview(t, db, id, withComment(t, second, comment)) - - // The second round runs and finds nothing left waiting. - runRemediationRound(t, db, id) - task, err := db.TaskByID(ctx, id) - if err != nil { - t.Fatal(err) - } - if task.Status != taskstate.PROpen { - t.Fatalf("status after both rounds = %q, want %q (a review was queued twice)", task.Status, taskstate.PROpen) - } -} - -// openTaskAtPROpen enqueues a task, claims it and opens its pull request. -func openTaskAtPROpen(t *testing.T, db *pgstore.TaskDB) int64 { - t.Helper() - ctx := t.Context() - created, err := db.EnqueueChatTask(ctx, "acme", "widget", "a task", "", "tdd", "", "", nil) - if err != nil { - t.Fatal(err) - } - if _, err := db.ClaimNext(ctx); err != nil { - t.Fatal(err) - } - if err := db.Transition(ctx, created.ID, taskstate.Running, taskstate.PROpen, "opened the pull request"); err != nil { - t.Fatal(err) - } - return created.ID -} - -// runRemediationRound claims the queued task and finishes one round of it. -func runRemediationRound(t *testing.T, db *pgstore.TaskDB, id int64) { - t.Helper() - ctx := t.Context() - run, err := db.ClaimNext(ctx) - if err != nil { - t.Fatal(err) - } - if run == nil || run.ID != id { - t.Fatalf("claim: got %v, want task %d queued", run, id) - } - if err := db.Transition(ctx, id, taskstate.Running, taskstate.PROpen, "remediated a review round"); err != nil { - t.Fatal(err) - } -} - -// assertActiveReview asserts the task is queued on payload, which is the -// review a remediation would run next. -func assertActiveReview(t *testing.T, db *pgstore.TaskDB, id int64, payload string) { - t.Helper() - task, err := db.TaskByID(t.Context(), id) - if err != nil { - t.Fatal(err) - } - if task.Status != taskstate.Queued || task.Workflow != "remediate" { - t.Fatalf("task is %q on workflow %q, want a queued remediation", task.Status, task.Workflow) - } - if task.ReviewPayload != payload { - t.Fatalf("active review = %s, want %s", task.ReviewPayload, payload) - } -} - -func reviewUnit(t *testing.T, reviewID int64) string { - t.Helper() - payload, err := workflow.EncodeReviewUnit(workflow.ReviewUnit{ - ReviewID: reviewID, State: "requested_changes", Body: "please change this", - }) - if err != nil { - t.Fatal(err) - } - return payload -} - -func reviewUnitWith(t *testing.T, reviewID int64, comment workflow.ReviewUnitComment) string { - t.Helper() - payload, err := workflow.EncodeReviewUnit(workflow.ReviewUnit{ReviewID: reviewID, Comments: []workflow.ReviewUnitComment{comment}}) - if err != nil { - t.Fatal(err) - } - return payload -} - -func withComment(t *testing.T, payload string, comment workflow.ReviewUnitComment) string { - t.Helper() - unit, err := workflow.DecodeReviewUnit(payload) - if err != nil { - t.Fatal(err) - } - unit.Comments = append(unit.Comments, comment) - encoded, err := workflow.EncodeReviewUnit(unit) - if err != nil { - t.Fatal(err) - } - return encoded -} diff --git a/internal/infrastructure/postgres/session_org_test.go b/internal/infrastructure/postgres/session_org_test.go deleted file mode 100644 index 1a7a0c490..000000000 --- a/internal/infrastructure/postgres/session_org_test.go +++ /dev/null @@ -1,62 +0,0 @@ -package postgres_test - -import ( - "context" - "errors" - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/domain/messaging" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/gateway" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -// One org's chat sessions and messages are invisible to another, which can -// neither read nor overwrite them. -func TestChatSessionsStayInTheirOrg(t *testing.T) { - acme := org.WithOrg(t.Context(), "acme") - other := org.WithOrg(t.Context(), "other") - store := gateway.NewPostgresSessionStore(pgstore.Pool(t)) - now := time.Now().UTC() - if err := store.Save(acme, gateway.SessionContext{SessionID: "s1", Title: "acme plans", CreatedAt: now, LastActiveAt: now}); err != nil { - t.Fatal(err) - } - if err := store.SaveMessage(acme, "s1", messaging.Message{ID: "m1", Role: messaging.RoleUser, Text: "secret"}); err != nil { - t.Fatal(err) - } - - for _, tt := range []struct { - name string - ctx func() context.Context //nolint:containedctx // table input, not stored state - sees bool - }{ - {"the owning org", func() context.Context { return acme }, true}, - {"another org", func() context.Context { return other }, false}, - } { - t.Run(tt.name, func(t *testing.T) { - ctx := tt.ctx() - listed, err := store.List(ctx) - if err != nil { - t.Fatal(err) - } - got, err := store.Get(ctx, "s1") - if err != nil { - t.Fatal(err) - } - messages, readErr := store.RecentMessages(ctx, "s1", 10) - if sees := len(listed) == 1 && got != nil && readErr == nil && len(messages) == 1; sees != tt.sees { - t.Fatalf("listed %d, got %v, messages %d (%v); want visible %v", len(listed), got != nil, len(messages), readErr, tt.sees) - } - if tt.sees { - return - } - if err := store.Save(ctx, gateway.SessionContext{SessionID: "s1", Title: "taken"}); !errors.Is(err, gateway.ErrSessionNotFound) { - t.Fatalf("overwrite err = %v, want ErrSessionNotFound", err) - } - if err := store.SaveMessage(ctx, "s1", messaging.Message{ID: "m2", Role: messaging.RoleUser, Text: "injected"}); !errors.Is(err, gateway.ErrSessionNotFound) { - t.Fatalf("append err = %v, want ErrSessionNotFound", err) - } - }) - } -} diff --git a/internal/infrastructure/postgres/source_org_test.go b/internal/infrastructure/postgres/source_org_test.go deleted file mode 100644 index a256def16..000000000 --- a/internal/infrastructure/postgres/source_org_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package postgres_test - -import ( - "testing" - - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/source" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -// TestInsertSourceKeepsActingOrg pins that a source created while acting in a -// non-default org is owned by that org, so its captures inherit the org. -func TestInsertSourceKeepsActingOrg(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - eda := postgres.NewEDA(db.Pool, nil) - if err := eda.InsertSource(org.WithOrg(ctx, "acme"), source.Source{Path: "acme-hook", Signing: source.SigningUnsigned}); err != nil { - t.Fatal(err) - } - var owner, workspace string - if err := db.Pool.QueryRow(ctx, "SELECT org_id, workspace_id FROM sources WHERE path=$1", "acme-hook").Scan(&owner, &workspace); err != nil { - t.Fatal(err) - } - if owner != "acme" || workspace != string(org.DefaultWorkspaceID) { - t.Fatalf("source owner %s/%s, want acme/%s", owner, workspace, org.DefaultWorkspaceID) - } - // Lookup stays global: the capture receiver resolves by path alone. - got, err := eda.GetSource(ctx, "acme-hook") - if err != nil || got == nil { - t.Fatalf("GetSource = %#v, err %v", got, err) - } -} diff --git a/internal/infrastructure/postgres/task_org_test.go b/internal/infrastructure/postgres/task_org_test.go deleted file mode 100644 index c52d4a194..000000000 --- a/internal/infrastructure/postgres/task_org_test.go +++ /dev/null @@ -1,83 +0,0 @@ -package postgres_test - -import ( - "context" - "testing" - - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/events" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -// An org's tasks, their events and the stats built from them are invisible to -// a request scoped to another org. An event a task emits belongs to the -// task's org whatever org the writer acts in, and a caller cannot start work -// under another org's identity. -func TestTasksStayInTheirOrg(t *testing.T) { - scoped := func(id org.OrgID) context.Context { - return org.WithScope(org.WithOrg(t.Context(), id), id) - } - db := pgstore.Open(t) - var taskID int64 - if err := db.Pool.QueryRow(t.Context(), - `INSERT INTO tasks (owner, repo, issue_number, identity, workflow, status, tokens_used, org_id, workspace_id) - VALUES ('o', 'r', 1, 'sam', 'deploy', 'running', 10, 'acme', 'default') RETURNING id`, - ).Scan(&taskID); err != nil { - t.Fatal(err) - } - if _, err := db.InsertEvent(t.Context(), events.Event{Kind: "stage_finish", TaskID: taskID, Workflow: "deploy", Stage: "plan"}); err != nil { - t.Fatal(err) - } - - for _, tt := range []struct { - name string - ctx func() context.Context //nolint:containedctx // table input, not stored state - sees bool - }{ - {"the owning org", func() context.Context { return scoped("acme") }, true}, - {"another org", func() context.Context { return scoped("other") }, false}, - {"an internal service", t.Context, true}, - } { - t.Run(tt.name, func(t *testing.T) { - ctx := tt.ctx() - listed, err := db.Tasks(ctx, 10) - if err != nil { - t.Fatal(err) - } - got, err := db.TaskByID(ctx, taskID) - if err != nil { - t.Fatal(err) - } - counts, err := db.StatusCounts(ctx) - if err != nil { - t.Fatal(err) - } - timeline, err := db.TaskEvents(ctx, taskID) - if err != nil { - t.Fatal(err) - } - feed, err := db.EventsSince(ctx, "", 10) - if err != nil { - t.Fatal(err) - } - workflows, err := db.WorkflowStats(ctx) - if err != nil { - t.Fatal(err) - } - stages, err := db.StageStats(ctx) - if err != nil { - t.Fatal(err) - } - seen := []bool{len(listed) == 1, got != nil, counts["running"] == 1, len(timeline) == 1, len(feed) == 1, len(workflows) == 1, len(stages) == 1} - for i, s := range seen { - if s != tt.sees { - t.Fatalf("read %d visible = %v, want %v", i, s, tt.sees) - } - } - }) - } - // An identity of the system org starts no work for a caller in acme. - if _, err := db.EnqueueChatTask(scoped("acme"), "o", "r", "t", "b", "deploy", "operator", "", nil); err == nil { - t.Fatal("acme started a task under a system-org identity") - } -} diff --git a/internal/infrastructure/postgres/task_origin_test.go b/internal/infrastructure/postgres/task_origin_test.go deleted file mode 100644 index 9b0920ff6..000000000 --- a/internal/infrastructure/postgres/task_origin_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package postgres_test - -import ( - "slices" - "testing" - - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/taskstate" -) - -// A conversation's /stop finds the queued and running tasks it created, and -// only those: not another conversation's, not finished ones, not originless. -func TestActiveTasksByOrigin(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - enqueue := func(origin string) int64 { - created, err := db.EnqueueChatTask(ctx, "", "", "work", "", "tdd", "", origin, nil) - if err != nil { - t.Fatal(err) - } - return created.ID - } - running, queued, finished := enqueue("telegram:1"), enqueue("telegram:1"), enqueue("telegram:1") - enqueue("telegram:2") - enqueue("") - if err := db.Transition(ctx, running, taskstate.Queued, taskstate.Running, ""); err != nil { - t.Fatal(err) - } - if err := db.Transition(ctx, finished, taskstate.Queued, taskstate.Declined, ""); err != nil { - t.Fatal(err) - } - - cases := []struct { - origin string - want []int64 - }{ - {"telegram:1", []int64{running, queued}}, - {"telegram:2", []int64{finished + 1}}, - {"", nil}, - } - for _, tc := range cases { - tasks, err := db.ActiveTasksByOrigin(ctx, tc.origin) - if err != nil { - t.Fatal(err) - } - var got []int64 - for _, task := range tasks { - got = append(got, task.ID) - } - if !slices.Equal(got, tc.want) { - t.Errorf("origin %q: tasks %v, want %v", tc.origin, got, tc.want) - } - } -} diff --git a/internal/infrastructure/postgres/usage_org_test.go b/internal/infrastructure/postgres/usage_org_test.go deleted file mode 100644 index 3264ee0b7..000000000 --- a/internal/infrastructure/postgres/usage_org_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package postgres_test - -import ( - "testing" - "time" - - "github.com/samcharles93/archie-core/internal/domain/usage" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" -) - -// A task's usage is billed to the task's org whatever org the caller names; -// usage with no task keeps the caller's org. -func TestRecordUsageBillsTheTaskOrg(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - var taskID int64 - if err := db.Pool.QueryRow(ctx, - `INSERT INTO tasks (owner, repo, issue_number, identity, org_id, workspace_id) VALUES ('o', 'r', 1, 'sam', 'acme', 'default') RETURNING id`, - ).Scan(&taskID); err != nil { - t.Fatal(err) - } - for _, tt := range []struct { - name string - record usage.Record - want string - }{ - {"a task bills its own org", usage.Record{Org: "other", Source: usage.SourceTask, TaskID: taskID}, "acme"}, - {"chat keeps the caller's org", usage.Record{Org: "org-sys", Source: usage.SourceChat}, "org-sys"}, - } { - t.Run(tt.name, func(t *testing.T) { - tt.record.Step, tt.record.At = tt.name, time.Now() - tt.record = tt.record.WithRef("openai/gpt-6-luna") - if err := db.RecordUsage(ctx, tt.record); err != nil { - t.Fatal(err) - } - var got string - if err := db.Pool.QueryRow(ctx, "SELECT org_id FROM model_usage WHERE step = $1", tt.name).Scan(&got); err != nil { - t.Fatal(err) - } - if got != tt.want { - t.Fatalf("org = %q, want %q", got, tt.want) - } - }) - } -} diff --git a/internal/infrastructure/postgres/workflow_call_test.go b/internal/infrastructure/postgres/workflow_call_test.go deleted file mode 100644 index 39ce936a7..000000000 --- a/internal/infrastructure/postgres/workflow_call_test.go +++ /dev/null @@ -1,122 +0,0 @@ -package postgres_test - -import ( - "testing" - - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" - "github.com/samcharles93/archie-core/internal/taskstate" -) - -// StartCall is idempotent per call key: the same caller and key return the -// child already started, so a retry of a lost EnqueueCallTask reply, or of the -// stage that made the call, does not start a second child and run its work -// twice. Another call site -- the same declared path starting a different -// workflow -- still gets its own child. -func TestStartCallReturnsOneChildPerCallKey(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - if _, err := db.EnqueueChatTask(ctx, "", "", "caller", "", "tdd", "", "", nil); err != nil { - t.Fatal(err) - } - caller, err := db.ClaimNext(ctx) - if err != nil || caller == nil { - t.Fatalf("claim: %v", err) - } - - first, err := db.StartCall(ctx, caller.ID, "check@callee-a", "callee-a", nil) - if err != nil { - t.Fatal(err) - } - retry, err := db.StartCall(ctx, caller.ID, "check@callee-a", "callee-a", nil) - if err != nil { - t.Fatal(err) - } - if retry.ID != first.ID { - t.Fatalf("the retry started task %d, want the child already started, %d", retry.ID, first.ID) - } - other, err := db.StartCall(ctx, caller.ID, "check@callee-b", "callee-b", nil) - if err != nil { - t.Fatal(err) - } - if other.ID == first.ID { - t.Fatalf("the same path starting another workflow returned the first child, %d", first.ID) - } - - var rows int - if err := db.Pool.QueryRow(ctx, - `SELECT count(*) FROM tasks WHERE call_parent_task_id = $1`, caller.ID).Scan(&rows); err != nil { - t.Fatal(err) - } - if rows != 2 { - t.Fatalf("the caller started %d children, want one per call key (2)", rows) - } -} - -// A child that reached a hard terminal failure can never do the work, so it -// frees its key: it left no result the caller can use, and the caller's retry -// starting a fresh child duplicates nothing. A parked child keeps the key -- -// it is recoverable by retrying the child, which is what the caller's own park -// names. -func TestStartCallFreesTheKeyOfAHardTerminalChild(t *testing.T) { - tests := []struct { - name string - // move takes the child out of queued the way its real ending does. - move func(t *testing.T, db *pgstore.TaskDB, childID int64) - wantNew bool - }{ - { - name: "a declined child frees its key", - move: func(t *testing.T, db *pgstore.TaskDB, childID int64) { - t.Helper() - if err := db.Transition(t.Context(), childID, taskstate.Queued, taskstate.Declined, "the operator declined it"); err != nil { - t.Fatal(err) - } - }, - wantNew: true, - }, - { - name: "a parked child keeps its key", - move: func(t *testing.T, db *pgstore.TaskDB, childID int64) { - t.Helper() - // Parked is reachable only from running, so the child is - // claimed first, as a worker would claim it. - claimed, err := db.ClaimNext(t.Context()) - if err != nil || claimed == nil || claimed.ID != childID { - t.Fatalf("claim child %d: %v, got %v", childID, err, claimed) - } - if err := db.ParkTask(t.Context(), childID, taskstate.Running, "needs an operator", taskstate.ParkNeedsHuman); err != nil { - t.Fatal(err) - } - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - ctx := t.Context() - db := pgstore.Open(t) - if _, err := db.EnqueueChatTask(ctx, "", "", "caller", "", "tdd", "", "", nil); err != nil { - t.Fatal(err) - } - caller, err := db.ClaimNext(ctx) - if err != nil || caller == nil { - t.Fatalf("claim: %v", err) - } - first, err := db.StartCall(ctx, caller.ID, "check@callee", "callee", nil) - if err != nil { - t.Fatal(err) - } - tt.move(t, db, first.ID) - - retry, err := db.StartCall(ctx, caller.ID, "check@callee", "callee", nil) - if err != nil { - t.Fatal(err) - } - if tt.wantNew && retry.ID == first.ID { - t.Fatalf("the retry returned the child that can never do the work, %d, want a fresh child", first.ID) - } - if !tt.wantNew && retry.ID != first.ID { - t.Fatalf("the retry started task %d, want the parked child it can retry, %d", retry.ID, first.ID) - } - }) - } -}