Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 37bb04136c |
@@ -1,12 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
sqlmock "github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
|
||||
)
|
||||
|
||||
// TestExtractExpiresInSeconds covers the JSON parser used at enqueue time
|
||||
@@ -63,207 +58,3 @@ func TestExtractExpiresInSeconds(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ── QueueStatusByID ─────────────────────────────────────────────────────────────
|
||||
|
||||
func setupQueueStatusDB(t *testing.T) sqlmock.Sqlmock {
|
||||
t.Helper()
|
||||
mockDB, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create sqlmock: %v", err)
|
||||
}
|
||||
prevDB := db.DB
|
||||
db.DB = mockDB
|
||||
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
|
||||
return mock
|
||||
}
|
||||
|
||||
func TestQueueStatusByID_Success(t *testing.T) {
|
||||
mock := setupQueueStatusDB(t)
|
||||
queueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
||||
wsID := "cccccccc-cccc-cccc-cccc-cccccccccccc"
|
||||
|
||||
rows := sqlmock.NewRows([]string{
|
||||
"id", "workspace_id", "status", "priority", "attempts",
|
||||
"last_error", "enqueued_at", "dispatched_at", "completed_at", "expires_at",
|
||||
"response_body",
|
||||
}).AddRow(
|
||||
queueID, wsID, "queued", 50, 0,
|
||||
nil, // last_error
|
||||
"2026-01-01T00:00:00Z", // enqueued_at
|
||||
nil, // dispatched_at
|
||||
nil, // completed_at
|
||||
nil, // expires_at
|
||||
nil, // response_body
|
||||
)
|
||||
mock.ExpectQuery(`SELECT`).
|
||||
WithArgs(queueID).
|
||||
WillReturnRows(rows)
|
||||
|
||||
qs, err := QueueStatusByID(context.Background(), queueID)
|
||||
if err != nil {
|
||||
t.Fatalf("QueueStatusByID returned error: %v", err)
|
||||
}
|
||||
if qs.ID != queueID {
|
||||
t.Errorf("ID = %q, want %q", qs.ID, queueID)
|
||||
}
|
||||
if qs.WorkspaceID != wsID {
|
||||
t.Errorf("WorkspaceID = %q, want %q", qs.WorkspaceID, wsID)
|
||||
}
|
||||
if qs.Status != "queued" {
|
||||
t.Errorf("Status = %q, want %q", qs.Status, "queued")
|
||||
}
|
||||
if qs.Priority != 50 {
|
||||
t.Errorf("Priority = %d, want 50", qs.Priority)
|
||||
}
|
||||
if qs.LastError != nil {
|
||||
t.Errorf("LastError = %v, want nil", qs.LastError)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueStatusByID_NotFound(t *testing.T) {
|
||||
mock := setupQueueStatusDB(t)
|
||||
queueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
||||
|
||||
mock.ExpectQuery(`SELECT`).
|
||||
WithArgs(queueID).
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
|
||||
qs, err := QueueStatusByID(context.Background(), queueID)
|
||||
if err != sql.ErrNoRows {
|
||||
t.Errorf("expected sql.ErrNoRows, got %v", err)
|
||||
}
|
||||
if qs != nil {
|
||||
t.Errorf("expected nil queue status, got %+v", qs)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueStatusByID_DBError(t *testing.T) {
|
||||
mock := setupQueueStatusDB(t)
|
||||
queueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
||||
|
||||
mock.ExpectQuery(`SELECT`).
|
||||
WithArgs(queueID).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
qs, err := QueueStatusByID(context.Background(), queueID)
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
}
|
||||
if qs != nil {
|
||||
t.Errorf("expected nil queue status, got %+v", qs)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueStatusByID_CompletedWithResponse(t *testing.T) {
|
||||
mock := setupQueueStatusDB(t)
|
||||
queueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
||||
wsID := "cccccccc-cccc-cccc-cccc-cccccccccccc"
|
||||
|
||||
respBody := []byte(`{"text":"delegation result"}`)
|
||||
rows := sqlmock.NewRows([]string{
|
||||
"id", "workspace_id", "status", "priority", "attempts",
|
||||
"last_error", "enqueued_at", "dispatched_at", "completed_at", "expires_at",
|
||||
"response_body",
|
||||
}).AddRow(
|
||||
queueID, wsID, "completed", 50, 1,
|
||||
nil,
|
||||
"2026-01-01T00:00:00Z",
|
||||
"2026-01-01T00:01:00Z",
|
||||
"2026-01-01T00:02:00Z",
|
||||
nil,
|
||||
respBody,
|
||||
)
|
||||
mock.ExpectQuery(`SELECT`).
|
||||
WithArgs(queueID).
|
||||
WillReturnRows(rows)
|
||||
|
||||
qs, err := QueueStatusByID(context.Background(), queueID)
|
||||
if err != nil {
|
||||
t.Fatalf("QueueStatusByID returned error: %v", err)
|
||||
}
|
||||
if qs.Status != "completed" {
|
||||
t.Errorf("Status = %q, want completed", qs.Status)
|
||||
}
|
||||
if qs.ResponseBody == nil {
|
||||
t.Fatal("ResponseBody should be set for completed status")
|
||||
}
|
||||
if string(qs.ResponseBody) != `{"text":"delegation result"}` {
|
||||
t.Errorf("ResponseBody = %q, want %q", string(qs.ResponseBody), `{"text":"delegation result"}`)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ── queueRowAuthFields ──────────────────────────────────────────────────────────
|
||||
|
||||
func TestQueueRowAuthFields_Success(t *testing.T) {
|
||||
mock := setupQueueStatusDB(t)
|
||||
queueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
||||
callerID := "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
|
||||
wsID := "cccccccc-cccc-cccc-cccc-cccccccccccc"
|
||||
|
||||
rows := sqlmock.NewRows([]string{"caller_id", "workspace_id"}).
|
||||
AddRow(callerID, wsID)
|
||||
mock.ExpectQuery(`SELECT caller_id, workspace_id FROM a2a_queue WHERE id`).
|
||||
WithArgs(queueID).
|
||||
WillReturnRows(rows)
|
||||
|
||||
gotCaller, gotWs, err := queueRowAuthFields(context.Background(), queueID)
|
||||
if err != nil {
|
||||
t.Fatalf("queueRowAuthFields returned error: %v", err)
|
||||
}
|
||||
if gotCaller != callerID {
|
||||
t.Errorf("callerID = %q, want %q", gotCaller, callerID)
|
||||
}
|
||||
if gotWs != wsID {
|
||||
t.Errorf("workspaceID = %q, want %q", gotWs, wsID)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueRowAuthFields_NotFound(t *testing.T) {
|
||||
mock := setupQueueStatusDB(t)
|
||||
queueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
||||
|
||||
mock.ExpectQuery(`SELECT caller_id, workspace_id FROM a2a_queue WHERE id`).
|
||||
WithArgs(queueID).
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
|
||||
_, _, err := queueRowAuthFields(context.Background(), queueID)
|
||||
if err != sql.ErrNoRows {
|
||||
t.Errorf("expected sql.ErrNoRows, got %v", err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueRowAuthFields_DBError(t *testing.T) {
|
||||
mock := setupQueueStatusDB(t)
|
||||
queueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
||||
|
||||
mock.ExpectQuery(`SELECT caller_id, workspace_id FROM a2a_queue WHERE id`).
|
||||
WithArgs(queueID).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
_, _, err := queueRowAuthFields(context.Background(), queueID)
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -516,51 +516,3 @@ func TestDrainQueueForWorkspace_ClaimGuarding_SecondDrainGetsEmpty(t *testing.T)
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ── QueueDepth ──────────────────────────────────────────────────────────────────
|
||||
|
||||
func TestQueueDepth_ReturnsCount(t *testing.T) {
|
||||
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create sqlmock: %v", err)
|
||||
}
|
||||
prevDB := db.DB
|
||||
db.DB = mockDB
|
||||
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
|
||||
|
||||
wsID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
||||
mock.ExpectQuery(`SELECT COUNT(*) FROM a2a_queue WHERE workspace_id = $1 AND status = 'queued'`).
|
||||
WithArgs(wsID).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(42))
|
||||
|
||||
got := QueueDepth(context.Background(), wsID)
|
||||
if got != 42 {
|
||||
t.Errorf("QueueDepth returned %d, want 42", got)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueDepth_ZeroWhenEmpty(t *testing.T) {
|
||||
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create sqlmock: %v", err)
|
||||
}
|
||||
prevDB := db.DB
|
||||
db.DB = mockDB
|
||||
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
|
||||
|
||||
wsID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
||||
mock.ExpectQuery(`SELECT COUNT(*) FROM a2a_queue WHERE workspace_id = $1 AND status = 'queued'`).
|
||||
WithArgs(wsID).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
|
||||
|
||||
got := QueueDepth(context.Background(), wsID)
|
||||
if got != 0 {
|
||||
t.Errorf("QueueDepth returned %d, want 0", got)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet sqlmock expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -947,73 +947,6 @@ func TestVerifyDiscordSignature_WrongLengthPubKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== matchesChatID pure function ====================
|
||||
|
||||
func TestMatchesChatID_ExactMatch(t *testing.T) {
|
||||
cfg := map[string]interface{}{"chat_id": "123456"}
|
||||
if !matchesChatID(cfg, "123456") {
|
||||
t.Error("expected true for exact match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchesChatID_NoMatch(t *testing.T) {
|
||||
cfg := map[string]interface{}{"chat_id": "123456"}
|
||||
if matchesChatID(cfg, "654321") {
|
||||
t.Error("expected false for non-matching chat ID")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchesChatID_PrefixNoMatch(t *testing.T) {
|
||||
// "123" is a prefix of "123456" but not an exact match.
|
||||
cfg := map[string]interface{}{"chat_id": "123456"}
|
||||
if matchesChatID(cfg, "123") {
|
||||
t.Error("expected false for prefix of stored chat ID")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchesChatID_CommaSeparatedMultiple(t *testing.T) {
|
||||
cfg := map[string]interface{}{"chat_id": "111,222,333"}
|
||||
for _, id := range []string{"111", "222", "333"} {
|
||||
if !matchesChatID(cfg, id) {
|
||||
t.Errorf("expected true for %q in comma-separated list", id)
|
||||
}
|
||||
}
|
||||
if matchesChatID(cfg, "444") {
|
||||
t.Error("expected false for ID not in list")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchesChatID_WhitespaceTrimmed(t *testing.T) {
|
||||
cfg := map[string]interface{}{"chat_id": "111, 222 , 333"}
|
||||
if !matchesChatID(cfg, "222") {
|
||||
t.Error("expected true for whitespace-trimmed match")
|
||||
}
|
||||
if matchesChatID(cfg, " 222") {
|
||||
t.Error("expected false for whitespace in query (not trimmed from query)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchesChatID_EmptyChatID(t *testing.T) {
|
||||
cfg := map[string]interface{}{"chat_id": ""}
|
||||
if matchesChatID(cfg, "123456") {
|
||||
t.Error("expected false for empty chat_id in config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchesChatID_MissingChatIDKey(t *testing.T) {
|
||||
cfg := map[string]interface{}{}
|
||||
if matchesChatID(cfg, "123456") {
|
||||
t.Error("expected false when chat_id key is missing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchesChatID_NonStringChatID(t *testing.T) {
|
||||
cfg := map[string]interface{}{"chat_id": 123456} // wrong type
|
||||
if matchesChatID(cfg, "123456") {
|
||||
t.Error("expected false when chat_id is not a string")
|
||||
}
|
||||
}
|
||||
|
||||
// TestChannelHandler_Webhook_Discord_NoKey_Returns401 verifies that a Discord
|
||||
// webhook request is rejected with 401 when no public key is configured in the
|
||||
// DB and DISCORD_APP_PUBLIC_KEY env var is not set.
|
||||
|
||||
@@ -653,3 +653,239 @@ func TestSanitizeUTF8(t *testing.T) {
|
||||
t.Errorf("sanitizeUTF8 did not produce valid UTF-8: %x", []byte(out))
|
||||
}
|
||||
}
|
||||
|
||||
// ── extractResponseSummary coverage ───────────────────────────────────────────
|
||||
|
||||
func TestExtractResponseSummary_EmptyBody(t *testing.T) {
|
||||
s := New(nil, nil)
|
||||
if got := s.extractResponseSummary(nil); got != "" {
|
||||
t.Errorf("nil body: got %q, want %q", got, "")
|
||||
}
|
||||
if got := s.extractResponseSummary([]byte{}); got != "" {
|
||||
t.Errorf("empty body: got %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractResponseSummary_InvalidJSON(t *testing.T) {
|
||||
s := New(nil, nil)
|
||||
got := s.extractResponseSummary([]byte(`not json`))
|
||||
if got != "" {
|
||||
t.Errorf("invalid JSON: got %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractResponseSummary_NoResultKey(t *testing.T) {
|
||||
s := New(nil, nil)
|
||||
got := s.extractResponseSummary([]byte(`{"error": "oops"}`))
|
||||
if got != "" {
|
||||
t.Errorf("no result key: got %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractResponseSummary_EmptyResult(t *testing.T) {
|
||||
s := New(nil, nil)
|
||||
got := s.extractResponseSummary([]byte(`{"result": {}}`))
|
||||
if got != "" {
|
||||
t.Errorf("empty result: got %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractResponseSummary_NoPartsKey(t *testing.T) {
|
||||
s := New(nil, nil)
|
||||
got := s.extractResponseSummary([]byte(`{"result": {"data": "hello"}}`))
|
||||
if got != "" {
|
||||
t.Errorf("no parts key: got %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractResponseSummary_EmptyPartsArray(t *testing.T) {
|
||||
s := New(nil, nil)
|
||||
got := s.extractResponseSummary([]byte(`{"result": {"parts": []}}`))
|
||||
if got != "" {
|
||||
t.Errorf("empty parts: got %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractResponseSummary_PartsWithText(t *testing.T) {
|
||||
s := New(nil, nil)
|
||||
got := s.extractResponseSummary([]byte(`{"result": {"parts": [{"text": "Hello world"}]}}`))
|
||||
if got != "Hello world" {
|
||||
t.Errorf("got %q, want %q", got, "Hello world")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractResponseSummary_MultipleParts(t *testing.T) {
|
||||
// The function returns the FIRST non-empty text it finds.
|
||||
s := New(nil, nil)
|
||||
got := s.extractResponseSummary([]byte(`{"result": {"parts": [{"text": ""}, {"text": "second"}]}}`))
|
||||
if got != "second" {
|
||||
t.Errorf("got %q, want %q", got, "second")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractResponseSummary_NonStringText(t *testing.T) {
|
||||
s := New(nil, nil)
|
||||
got := s.extractResponseSummary([]byte(`{"result": {"parts": [{"text": 42}]}}`))
|
||||
if got != "" {
|
||||
t.Errorf("non-string text: got %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
// ── isEmptyResponse coverage ─────────────────────────────────────────────────────
|
||||
|
||||
func TestIsEmptyResponse_NilAndEmpty(t *testing.T) {
|
||||
if !isEmptyResponse(nil) {
|
||||
t.Error("nil body should be empty")
|
||||
}
|
||||
if !isEmptyResponse([]byte{}) {
|
||||
t.Error("empty body should be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEmptyResponse_SentinelMarkers(t *testing.T) {
|
||||
cases := []string{
|
||||
`(no response generated)`,
|
||||
`"text": "(no response generated)"`,
|
||||
`"text":""`,
|
||||
`"text": ""`,
|
||||
}
|
||||
for _, marker := range cases {
|
||||
body := []byte(`{"result":{"parts":[{"text":"` + marker + `"}]}}`)
|
||||
if !isEmptyResponse(body) {
|
||||
t.Errorf("body with marker %q should be empty", marker)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEmptyResponse_RealContent(t *testing.T) {
|
||||
// Any body containing a sentinel marker is treated as empty.
|
||||
// Bodies with no marker and with non-empty text are NOT empty.
|
||||
cases := []struct {
|
||||
body string
|
||||
isEmpty bool
|
||||
}{
|
||||
{`{"result":{"parts":[{"text":"Hello world"}]}}`, false},
|
||||
{`{"result":{"parts":[{"text":"goodbye"}]}}`, false},
|
||||
{`{"text":"hello"}`, false},
|
||||
{`{"result":{"parts":[]}}`, false},
|
||||
// sentinel markers trigger empty=true
|
||||
{`{"result":{"parts":[{"text":"(no response generated) plus more"}]}}`, true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := isEmptyResponse([]byte(tc.body))
|
||||
if got != tc.isEmpty {
|
||||
t.Errorf("isEmptyResponse(%q) = %v, want %v", tc.body, got, tc.isEmpty)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEmptyResponse_PartialMarker(t *testing.T) {
|
||||
// The marker must appear as a complete substring. Partial matches don't count.
|
||||
body := []byte(`{"result":{"parts":[{"text":"(no response gen"}]}}`)
|
||||
if isEmptyResponse(body) {
|
||||
t.Error(`partial marker "(no response gen" should NOT match`)
|
||||
}
|
||||
}
|
||||
|
||||
// ── maybeSweepPhantomBusy coverage ─────────────────────────────────────────────
|
||||
|
||||
// phantomSweepInterval = 5 minutes. maybeSweepPhantomBusy skips the DB query
|
||||
// when lastSweepAt is within the interval.
|
||||
|
||||
func TestMaybeSweepPhantomBusy_SkipsWithinInterval(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
// No DB calls expected since we skip within the interval.
|
||||
|
||||
s := New(nil, nil)
|
||||
s.mu.Lock()
|
||||
s.lastSweepAt = time.Now() // just swept
|
||||
s.mu.Unlock()
|
||||
|
||||
s.maybeSweepPhantomBusy(context.Background())
|
||||
|
||||
// Verify no DB calls were made.
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unexpected DB call: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaybeSweepPhantomBusy_RunsWhenStale(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
|
||||
// Return a row so the sweep logs one reset.
|
||||
rows := sqlmock.NewRows([]string{"id", "name"}).
|
||||
AddRow("ws-phantom", "Phantom Agent")
|
||||
mock.ExpectQuery(`UPDATE workspaces`).
|
||||
WillReturnRows(rows)
|
||||
|
||||
s := New(nil, nil)
|
||||
s.mu.Lock()
|
||||
s.lastSweepAt = time.Now().Add(-6 * time.Minute) // older than 5 min interval
|
||||
s.mu.Unlock()
|
||||
|
||||
s.maybeSweepPhantomBusy(context.Background())
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet DB expectations: %v", err)
|
||||
}
|
||||
|
||||
// Verify lastSweepAt was updated.
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
if time.Since(s.lastSweepAt) > time.Second {
|
||||
t.Error("lastSweepAt should be updated to time.Now() after a sweep")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaybeSweepPhantomBusy_StaleFirstSweep(t *testing.T) {
|
||||
// Zero time.Time is treated as "never swept" — time.Since(zero) is many years,
|
||||
// which is > 5 min, so the sweep runs on first call.
|
||||
mock := setupTestDB(t)
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "name"})
|
||||
mock.ExpectQuery(`UPDATE workspaces`).
|
||||
WillReturnRows(rows)
|
||||
|
||||
s := New(nil, nil)
|
||||
// lastSweepAt is zero (never swept) — this should trigger the sweep.
|
||||
s.maybeSweepPhantomBusy(context.Background())
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet DB expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ── sweepPhantomBusy DB-error coverage ──────────────────────────────────────────
|
||||
|
||||
func TestSweepPhantomBusy_QueryError(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
|
||||
mock.ExpectQuery(`UPDATE workspaces`).
|
||||
WillReturnError(errDBDown)
|
||||
|
||||
s := New(nil, nil)
|
||||
// Should not panic — error is logged and function returns.
|
||||
s.sweepPhantomBusy(context.Background())
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet DB expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSweepPhantomBusy_RowsError(t *testing.T) {
|
||||
// Query succeeds but rows.Next() returns an error.
|
||||
mock := setupTestDB(t)
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "name"}).
|
||||
AddRow("ws-1", "Test Agent").
|
||||
RowError(0, errDBDown)
|
||||
mock.ExpectQuery(`UPDATE workspaces`).
|
||||
WillReturnRows(rows)
|
||||
|
||||
s := New(nil, nil)
|
||||
s.sweepPhantomBusy(context.Background())
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("unmet DB expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user