Files
wehub-resource-sync f99010fae1
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Windows) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:36 +08:00

84 lines
2.1 KiB
Go

//go:build pgtest
package postgres
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.kenn.io/agentsview/internal/db"
)
func TestStoreInsightCRUD(t *testing.T) {
pgURL := testPGURL(t)
ensureStoreSchema(t, pgURL)
store, err := NewStore(pgURL, testSchema, true)
require.NoError(t, err, "NewStore")
defer store.Close()
ctx := context.Background()
project := "insight-project"
cacheKey := "insight-cache-key"
firstID, err := store.InsertInsight(db.Insight{
Type: "daily_activity",
DateFrom: "2026-03-12",
DateTo: "2026-03-12",
Project: &project,
Agent: "claude",
Content: "first insight",
CacheKey: cacheKey,
CacheStatus: "fresh",
})
require.NoError(t, err, "InsertInsight first")
require.NotZero(t, firstID)
time.Sleep(10 * time.Millisecond)
secondID, err := store.InsertInsight(db.Insight{
Type: "daily_activity",
DateFrom: "2026-03-12",
DateTo: "2026-03-12",
Project: &project,
Agent: "claude",
Content: "second insight",
CacheKey: cacheKey,
CacheStatus: "hit",
})
require.NoError(t, err, "InsertInsight second")
require.NotZero(t, secondID)
assert.NotEqual(t, firstID, secondID)
listed, err := store.ListInsights(ctx, db.InsightFilter{
Type: "daily_activity",
Project: project,
})
require.NoError(t, err, "ListInsights")
require.Len(t, listed, 2)
assert.Equal(t, secondID, listed[0].ID)
assert.Equal(t, firstID, listed[1].ID)
got, err := store.GetInsight(ctx, firstID)
require.NoError(t, err, "GetInsight")
require.NotNil(t, got)
assert.Equal(t, "first insight", got.Content)
require.NotNil(t, got.Project)
assert.Equal(t, project, *got.Project)
cached, err := store.GetCachedInsight(ctx, cacheKey)
require.NoError(t, err, "GetCachedInsight")
require.NotNil(t, cached)
assert.Equal(t, secondID, cached.ID)
assert.Equal(t, "hit", cached.CacheStatus)
require.NoError(t, store.DeleteInsight(firstID), "DeleteInsight first")
got, err = store.GetInsight(ctx, firstID)
require.NoError(t, err, "GetInsight after delete")
assert.Nil(t, got)
}