forked from mtusi2026/mtusi2026lab6
168 lines
5.6 KiB
Go
168 lines
5.6 KiB
Go
package postgres
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/media"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store"
|
|
)
|
|
|
|
// fixtureAttachment reserves an independent attachment for a real SQL scenario.
|
|
func fixtureAttachment(t *testing.T, s *Store) dto.Attachment {
|
|
t.Helper()
|
|
user := fixtureUser(t, s)
|
|
chat := fixtureChat(t, s, user)
|
|
id := uuid.New()
|
|
a := dto.Attachment{
|
|
ID: id,
|
|
ChatID: chat.ID,
|
|
OwnerID: user.ID,
|
|
Filename: "test.png",
|
|
ContentType: "image/png",
|
|
SizeBytes: 32,
|
|
SHA256: "synthetic-checksum",
|
|
Status: "uploading",
|
|
ObjectKey: "original/" + id.String(),
|
|
CreatedAt: time.Now().UTC().Add(-2 * time.Hour),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
if err := s.Reserve(t.Context(), a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return a
|
|
}
|
|
|
|
// TestAttachmentPublication verifies stored metadata survives a fresh read after commit.
|
|
func TestAttachmentPublication(t *testing.T) {
|
|
s := testStore(t)
|
|
a := fixtureAttachment(t, s)
|
|
published, err := s.Publish(t.Context(), a.ID)
|
|
if err != nil || published.Status != "ready" {
|
|
t.Fatalf("publish: %v", err)
|
|
}
|
|
got, err := s.Attachment(t.Context(), a.ID)
|
|
if err != nil || got.ObjectKey != a.ObjectKey || got.Status != "ready" {
|
|
t.Fatalf("stored metadata: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestAttachmentMessageScope checks a foreign chat cannot reference another chat's file.
|
|
func TestAttachmentMessageScope(t *testing.T) {
|
|
s := testStore(t)
|
|
a := fixtureAttachment(t, s)
|
|
if _, err := s.Publish(t.Context(), a.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
foreignUser := fixtureUser(t, s)
|
|
foreignChat := fixtureChat(t, s, foreignUser)
|
|
_, err := s.CreateMessage(t.Context(), dto.MessageDTO{ID: uuid.New(), ChatID: foreignChat.ID, SenderID: foreignUser.ID, AttachmentIDs: []string{a.ID.String()}, IdempotencyKey: "foreign"})
|
|
if !errors.Is(err, store.ErrNotFound) {
|
|
t.Fatalf("foreign attachment: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestAttachmentRetryConflict verifies attachment identity participates in the immutable request.
|
|
func TestAttachmentRetryConflict(t *testing.T) {
|
|
s := testStore(t)
|
|
a := fixtureAttachment(t, s)
|
|
if _, err := s.Publish(t.Context(), a.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := dto.MessageDTO{ID: uuid.New(), ChatID: a.ChatID, SenderID: a.OwnerID, Text: "same", AttachmentIDs: []string{a.ID.String()}, IdempotencyKey: "retry"}
|
|
if _, err := s.CreateMessage(t.Context(), m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.AttachmentIDs = nil
|
|
_, err := s.CreateMessage(t.Context(), m)
|
|
if !errors.Is(err, store.ErrConflict) {
|
|
t.Fatalf("changed attachment list: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestReclaimExcludesPublication demonstrates the durable cleanup state closes the publish race.
|
|
func TestReclaimExcludesPublication(t *testing.T) {
|
|
s := testStore(t)
|
|
a := fixtureAttachment(t, s)
|
|
claimed, err := s.Reclaim(t.Context(), a.ID, time.Now().Add(-time.Hour), false)
|
|
if err != nil || !claimed {
|
|
t.Fatalf("claim: %v", err)
|
|
}
|
|
if _, err := s.Publish(t.Context(), a.ID); !errors.Is(err, store.ErrNotFound) {
|
|
t.Fatalf("late publication: %v", err)
|
|
}
|
|
removed, err := s.Reclaim(t.Context(), a.ID, time.Now().Add(-time.Hour), true)
|
|
if err != nil || !removed {
|
|
t.Fatalf("cleanup: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestReclaimProtectsFreshReservation ensures a concurrent valid upload stays intact.
|
|
func TestReclaimProtectsFreshReservation(t *testing.T) {
|
|
s := testStore(t)
|
|
a := fixtureAttachment(t, s)
|
|
_, err := s.pool.Exec(t.Context(), `UPDATE attachments SET created_at=clock_timestamp() WHERE id=@id`, pgx.StrictNamedArgs{"id": a.ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claimed, err := s.Reclaim(t.Context(), a.ID, time.Now().Add(-time.Hour), false)
|
|
if err != nil || claimed {
|
|
t.Fatalf("fresh reservation removed: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestMetadataReconciliation detects missing originals without deleting referenced metadata.
|
|
func TestMetadataReconciliation(t *testing.T) {
|
|
s := testStore(t)
|
|
a := fixtureAttachment(t, s)
|
|
if _, err := s.Publish(t.Context(), a.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
objects := &reconcileObjects{exists: false}
|
|
dry, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, false)
|
|
if err != nil || dry.Missing != 1 || dry.Changed != 0 {
|
|
t.Fatalf("dry run: %+v %v", dry, err)
|
|
}
|
|
applied, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, true)
|
|
if err != nil || applied.Changed != 1 {
|
|
t.Fatalf("repair: %+v %v", applied, err)
|
|
}
|
|
again, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, true)
|
|
if err != nil || again.Changed != 0 {
|
|
t.Fatalf("repeat changed state: %+v %v", again, err)
|
|
}
|
|
objects.exists = true
|
|
restored, err := media.Reconcile(t.Context(), s, objects, uuid.Nil, time.Hour, true)
|
|
if err != nil || restored.Changed != 1 {
|
|
t.Fatalf("restore: %+v %v", restored, err)
|
|
}
|
|
}
|
|
|
|
// TestObjectReservationLookup protects unfinished uploads from orphan deletion.
|
|
func TestObjectReservationLookup(t *testing.T) {
|
|
s := testStore(t)
|
|
a := fixtureAttachment(t, s)
|
|
exists, err := s.HasObject(t.Context(), a.ObjectKey)
|
|
if err != nil || !exists {
|
|
t.Fatalf("reservation not protected: %v", err)
|
|
}
|
|
exists, err = s.HasObject(t.Context(), "original/"+uuid.NewString())
|
|
if err != nil || exists {
|
|
t.Fatalf("unknown object considered reserved: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestInventoryStopsAtUnavailableDatabase rejects a scan rather than reporting an empty inventory.
|
|
func TestInventoryStopsAtUnavailableDatabase(t *testing.T) {
|
|
s := testStore(t)
|
|
s.Close()
|
|
if _, err := s.Scan(t.Context(), uuid.Nil, 1000); err == nil {
|
|
t.Fatal("closed database reported empty inventory")
|
|
}
|
|
}
|