|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +// Copyright (C) 2026 SourceBridge Contributors |
| 3 | + |
| 4 | +package graphql |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/sourcebridge/sourcebridge/internal/appdeps" |
| 11 | + knowledgepkg "github.com/sourcebridge/sourcebridge/internal/knowledge" |
| 12 | + "github.com/sourcebridge/sourcebridge/internal/usage" |
| 13 | +) |
| 14 | + |
| 15 | +// TestMarkArtifactReady_IncrementsCounter verifies that a successful call to |
| 16 | +// markArtifactReady increments ArtifactsCounter by exactly 1, and that a |
| 17 | +// failed call (store returns an error for an unknown ID) does NOT increment |
| 18 | +// the counter. |
| 19 | +func TestMarkArtifactReady_IncrementsCounter(t *testing.T) { |
| 20 | + t.Cleanup(usage.ResetCountersForTest) |
| 21 | + |
| 22 | + mem := knowledgepkg.NewMemStore() |
| 23 | + r := &Resolver{Deps: &appdeps.AppDeps{KnowledgeStore: mem}} |
| 24 | + |
| 25 | + // Seed an artifact so UpdateKnowledgeArtifactStatus has something to update. |
| 26 | + artifact := &knowledgepkg.Artifact{ |
| 27 | + ID: "art-001", |
| 28 | + Status: knowledgepkg.StatusGenerating, |
| 29 | + } |
| 30 | + if _, err := mem.StoreKnowledgeArtifact(context.Background(), artifact); err != nil { |
| 31 | + t.Fatalf("StoreKnowledgeArtifact: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + // Happy path: markArtifactReady should succeed and increment the counter. |
| 35 | + if err := r.markArtifactReady(context.Background(), artifact.ID); err != nil { |
| 36 | + t.Fatalf("markArtifactReady: unexpected error: %v", err) |
| 37 | + } |
| 38 | + if got := usage.ArtifactsCounter.Total(); got != 1 { |
| 39 | + t.Fatalf("after success: expected ArtifactsCounter.Total() == 1, got %d", got) |
| 40 | + } |
| 41 | + |
| 42 | + // Verify the artifact is actually in READY state. |
| 43 | + stored := mem.GetKnowledgeArtifact(context.Background(), artifact.ID) |
| 44 | + if stored == nil { |
| 45 | + t.Fatal("GetKnowledgeArtifact: returned nil after markArtifactReady") |
| 46 | + } |
| 47 | + if stored.Status != knowledgepkg.StatusReady { |
| 48 | + t.Fatalf("artifact status: expected %q, got %q", knowledgepkg.StatusReady, stored.Status) |
| 49 | + } |
| 50 | + |
| 51 | + // Failure path: a non-existent artifact ID should return an error and |
| 52 | + // must NOT increment the counter. |
| 53 | + if err := r.markArtifactReady(context.Background(), "does-not-exist"); err == nil { |
| 54 | + t.Fatal("markArtifactReady on unknown ID: expected error, got nil") |
| 55 | + } |
| 56 | + if got := usage.ArtifactsCounter.Total(); got != 1 { |
| 57 | + t.Fatalf("after failure: expected ArtifactsCounter.Total() == 1 (unchanged), got %d", got) |
| 58 | + } |
| 59 | +} |
0 commit comments