An open-source, faster Devin alternative
TypeScript
add true unit test
Brian Reardon4214439799b7
diff --git a/internal/db/store.go b/internal/db/store.goindex 3ee03c43..f6ea9397 100644--- a/internal/db/store.go+++ b/internal/db/store.go@@ -40,6 +40,25 @@ type Store struct {// write. workerID is intentionally omitted (the sandbox is leaving its worker).type TerminalHook func(sandboxID string, orgID uuid.UUID, status, reason string)+// isTerminalSessionStatus reports whether a session status means the sandbox is+// gone for good and billing must stop — as opposed to `hibernated` (a pause,+// billing resumes on wake) or `running`/`pending`/`migrating` (still live).+// Terminal statuses both close the open scale_event AND fire the terminal hook+// (which publishes `stopped` to D1). `failed` (pending→failed) is terminal:+// the create never succeeded, so it must stop billing — it was historically+// omitted, which let failed-creates bill forever on the edge.+//+// Extracted as a pure predicate so it has DB-free unit coverage: adding a new+// terminal status without classifying it here is the easy regression.+func isTerminalSessionStatus(status string) bool {+ switch status {+ case "stopped", "error", "failed":+ return true+ default:+ return false+ }+}+// SetEncryptor configures the encryption key for project secrets.func (s *Store) SetEncryptor(enc *crypto.Encryptor) {s.encryptor = enc@@ -778,11 +797,7 @@ func (s *Store) UpdateSandboxSessionStatus(ctx context.Context, sandboxID, statuargs = []interface{}{status, sandboxID}}- // Terminal = the sandbox is no longer running and should stop billing.- // `failed` (pending→failed, create never succeeded) is terminal too — it- // was previously omitted from both the scale-event close and any D1 publish,- // which let failed-creates bill forever on the edge.- terminal := status == "stopped" || status == "error" || status == "failed"+ terminal := isTerminalSessionStatus(status)tx, err := s.pool.Begin(ctx)if err != nil {diff --git a/internal/db/terminal_status_test.go b/internal/db/terminal_status_test.gonew file mode 100644index 00000000..e842c9e7--- /dev/null+++ b/internal/db/terminal_status_test.go@@ -0,0 +1,25 @@+package db++import "testing"++// Pure, DB-free coverage of the terminal-status classification that drives both+// the scale-event close and the `stopped`→D1 publish. The firing behavior+// (RowsAffected coupling) is covered by the pgfixture integration test; this+// guards the classification itself — adding a new terminal status (e.g.+// "killed") without listing it here is the easy regression that would silently+// leave it billing on the edge.+func TestIsTerminalSessionStatus(t *testing.T) {+ terminal := []string{"stopped", "error", "failed"}+ notTerminal := []string{"running", "pending", "migrating", "hibernated", "woke", "", "unknown"}++ for _, s := range terminal {+ if !isTerminalSessionStatus(s) {+ t.Errorf("isTerminalSessionStatus(%q) = false, want true (must stop billing + publish stopped)", s)+ }+ }+ for _, s := range notTerminal {+ if isTerminalSessionStatus(s) {+ t.Errorf("isTerminalSessionStatus(%q) = true, want false (hibernated/running must NOT publish stopped)", s)+ }+ }+}