CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s
272 lines
8.8 KiB
Go
272 lines
8.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/kazier/backend/dbtest"
|
|
)
|
|
|
|
func newTestRepo(t *testing.T) *Repository {
|
|
t.Helper()
|
|
conn := dbtest.OpenTestDatabase(t, repositoryTestURL)
|
|
return NewRepository(conn)
|
|
}
|
|
|
|
func seedOcrJobFixture(t *testing.T) (*Repository, string, string, string) {
|
|
t.Helper()
|
|
repo := newTestRepo(t)
|
|
userID, err := repo.Users.Create("ocr-user", "ocr-user", "hash", false)
|
|
if err != nil {
|
|
t.Fatalf("create user: %v", err)
|
|
}
|
|
deviceID := NewID()
|
|
if err := repo.Devices.Upsert(deviceID); err != nil {
|
|
t.Fatalf("register device: %v", err)
|
|
}
|
|
fileID := NewID()
|
|
if err := repo.Resources.InsertFile(userID, fileID, "scan.png", "", 10, nil, nil); err != nil {
|
|
t.Fatalf("insert file: %v", err)
|
|
}
|
|
return repo, userID, deviceID, fileID
|
|
}
|
|
|
|
func TestOcrJobsLifecycleTransitions(t *testing.T) {
|
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
|
jobID := NewID()
|
|
|
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
row, err := repo.OcrJobs.Get(deviceID, jobID)
|
|
if err != nil || row.Status != "queued" {
|
|
t.Fatalf("get (queued): %+v err=%v", row, err)
|
|
}
|
|
|
|
if err := repo.OcrJobs.TouchProcessing(deviceID, jobID); err != nil {
|
|
t.Fatalf("touch processing: %v", err)
|
|
}
|
|
row, _ = repo.OcrJobs.Get(deviceID, jobID)
|
|
if row.Status != "processing" {
|
|
t.Errorf("status attendu processing, got %q", row.Status)
|
|
}
|
|
|
|
if err := repo.OcrJobs.Complete(deviceID, jobID, "HELLO"); err != nil {
|
|
t.Fatalf("complete: %v", err)
|
|
}
|
|
row, _ = repo.OcrJobs.Get(deviceID, jobID)
|
|
if row.Status != "done" || row.Text == nil || *row.Text != "HELLO" {
|
|
t.Errorf("after complete: %+v", row)
|
|
}
|
|
}
|
|
|
|
func TestOcrJobsFail(t *testing.T) {
|
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
|
jobID := NewID()
|
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
// Fail n'est valide que depuis `processing` (transition gardée).
|
|
if err := repo.OcrJobs.TouchProcessing(deviceID, jobID); err != nil {
|
|
t.Fatalf("touch processing: %v", err)
|
|
}
|
|
if err := repo.OcrJobs.Fail(deviceID, jobID, "tesseract exploded"); err != nil {
|
|
t.Fatalf("fail: %v", err)
|
|
}
|
|
row, err := repo.OcrJobs.Get(deviceID, jobID)
|
|
if err != nil || row.Status != "failed" || row.Error == nil || *row.Error != "tesseract exploded" {
|
|
t.Errorf("après fail : %+v err=%v", row, err)
|
|
}
|
|
}
|
|
|
|
func TestOcrJobsGuardedTransitions(t *testing.T) {
|
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
|
newJob := func(t *testing.T) string {
|
|
t.Helper()
|
|
jobID := NewID()
|
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
return jobID
|
|
}
|
|
|
|
// Fail depuis `queued` → conflit, le job reste queued.
|
|
queued := newJob(t)
|
|
if err := repo.OcrJobs.Fail(deviceID, queued, "boom"); !errors.Is(err, ErrJobStateConflict) {
|
|
t.Errorf("fail depuis queued : attendu ErrJobStateConflict, got %v", err)
|
|
}
|
|
if row, _ := repo.OcrJobs.Get(deviceID, queued); row.Status != "queued" {
|
|
t.Errorf("fail depuis queued : status = %q, attendu queued", row.Status)
|
|
}
|
|
|
|
// Complete depuis `queued` → conflit.
|
|
if err := repo.OcrJobs.Complete(deviceID, queued, "x"); !errors.Is(err, ErrJobStateConflict) {
|
|
t.Errorf("complete depuis queued : attendu ErrJobStateConflict, got %v", err)
|
|
}
|
|
|
|
// Double TouchProcessing → conflit.
|
|
if err := repo.OcrJobs.TouchProcessing(deviceID, queued); err != nil {
|
|
t.Fatalf("touch processing: %v", err)
|
|
}
|
|
if err := repo.OcrJobs.TouchProcessing(deviceID, queued); !errors.Is(err, ErrJobStateConflict) {
|
|
t.Errorf("double touch : attendu ErrJobStateConflict, got %v", err)
|
|
}
|
|
|
|
// Double Complete → le premier gagne, le second est un conflit.
|
|
if err := repo.OcrJobs.Complete(deviceID, queued, "FIRST"); err != nil {
|
|
t.Fatalf("complete: %v", err)
|
|
}
|
|
if err := repo.OcrJobs.Complete(deviceID, queued, "SECOND"); !errors.Is(err, ErrJobStateConflict) {
|
|
t.Errorf("double complete : attendu ErrJobStateConflict, got %v", err)
|
|
}
|
|
if row, _ := repo.OcrJobs.Get(deviceID, queued); row.Text == nil || *row.Text != "FIRST" {
|
|
t.Errorf("double complete : texte = %+v, attendu FIRST", row.Text)
|
|
}
|
|
|
|
// Fail après Complete → conflit, le statut `done` est collant.
|
|
if err := repo.OcrJobs.Fail(deviceID, queued, "late"); !errors.Is(err, ErrJobStateConflict) {
|
|
t.Errorf("fail après done : attendu ErrJobStateConflict, got %v", err)
|
|
}
|
|
if row, _ := repo.OcrJobs.Get(deviceID, queued); row.Status != "done" {
|
|
t.Errorf("fail après done : status = %q, attendu done", row.Status)
|
|
}
|
|
|
|
// Complete après Fail → conflit.
|
|
failed := newJob(t)
|
|
if err := repo.OcrJobs.TouchProcessing(deviceID, failed); err != nil {
|
|
t.Fatalf("touch processing: %v", err)
|
|
}
|
|
if err := repo.OcrJobs.Fail(deviceID, failed, "boom"); err != nil {
|
|
t.Fatalf("fail: %v", err)
|
|
}
|
|
if err := repo.OcrJobs.Complete(deviceID, failed, "late"); !errors.Is(err, ErrJobStateConflict) {
|
|
t.Errorf("complete après failed : attendu ErrJobStateConflict, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOcrJobsGetScopedByDevice(t *testing.T) {
|
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
|
jobID := NewID()
|
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
|
|
otherDevice := NewID()
|
|
if err := repo.Devices.Upsert(otherDevice); err != nil {
|
|
t.Fatalf("register other device: %v", err)
|
|
}
|
|
if _, err := repo.OcrJobs.Get(otherDevice, jobID); !errors.Is(err, ErrJobNotFound) {
|
|
t.Errorf("get par un autre device : attendu ErrJobNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOcrJobsClaimNextFifo(t *testing.T) {
|
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
|
var jobIDs []string
|
|
for i := 0; i < 3; i++ {
|
|
jobID := NewID()
|
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
|
t.Fatalf("create %d: %v", i, err)
|
|
}
|
|
jobIDs = append(jobIDs, jobID)
|
|
}
|
|
|
|
for i, want := range jobIDs {
|
|
row, err := repo.OcrJobs.ClaimNext()
|
|
if err != nil {
|
|
t.Fatalf("claim %d: %v", i, err)
|
|
}
|
|
if row.ID != want {
|
|
t.Errorf("claim %d = %q, attendu %q (FIFO)", i, row.ID, want)
|
|
}
|
|
if row.Status != "processing" {
|
|
t.Errorf("claim %d status = %q, attendu processing", i, row.Status)
|
|
}
|
|
}
|
|
|
|
if _, err := repo.OcrJobs.ClaimNext(); !errors.Is(err, ErrNoQueuedJobs) {
|
|
t.Errorf("queue vide : attendu ErrNoQueuedJobs, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOcrJobsClaimNextSkipsProcessingAndDone(t *testing.T) {
|
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
|
job1 := NewID()
|
|
job2 := NewID()
|
|
if err := repo.OcrJobs.Create(job1, deviceID, fileID); err != nil {
|
|
t.Fatalf("create job1: %v", err)
|
|
}
|
|
if err := repo.OcrJobs.Create(job2, deviceID, fileID); err != nil {
|
|
t.Fatalf("create job2: %v", err)
|
|
}
|
|
if err := repo.OcrJobs.TouchProcessing(deviceID, job2); err != nil {
|
|
t.Fatalf("touch job2 processing: %v", err)
|
|
}
|
|
|
|
row, err := repo.OcrJobs.ClaimNext()
|
|
if err != nil {
|
|
t.Fatalf("claim: %v", err)
|
|
}
|
|
if row.ID != job1 {
|
|
t.Errorf("claim = %q, attendu job1 %q", row.ID, job1)
|
|
}
|
|
}
|
|
|
|
func TestOcrJobsResetStaleProcessing(t *testing.T) {
|
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
|
jobID := NewID()
|
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
if err := repo.OcrJobs.TouchProcessing(deviceID, jobID); err != nil {
|
|
t.Fatalf("touch processing: %v", err)
|
|
}
|
|
|
|
// Started_at = NOW : pas encore stale → aucun requeue.
|
|
if n, err := repo.OcrJobs.ResetStaleProcessing(time.Minute); err != nil {
|
|
t.Fatalf("reset: %v", err)
|
|
} else if n != 0 {
|
|
t.Errorf("reset récent : attendu 0, got %d", n)
|
|
}
|
|
|
|
// Vieillit le started_at puis re-reset → 1 requeue.
|
|
if _, err := repo.OcrJobs.DB.Exec(`UPDATE ocr_jobs SET started_at = NOW() - interval '2 minutes' WHERE job_id = $1`, jobID); err != nil {
|
|
t.Fatalf("vieillir started_at: %v", err)
|
|
}
|
|
if n, err := repo.OcrJobs.ResetStaleProcessing(time.Minute); err != nil {
|
|
t.Fatalf("reset stale: %v", err)
|
|
} else if n != 1 {
|
|
t.Errorf("reset stale : attendu 1, got %d", n)
|
|
}
|
|
|
|
row, err := repo.OcrJobs.Get(deviceID, jobID)
|
|
if err != nil {
|
|
t.Fatalf("get: %v", err)
|
|
}
|
|
if row.Status != "queued" {
|
|
t.Errorf("status après reset = %q, attendu queued", row.Status)
|
|
}
|
|
}
|
|
|
|
func TestOcrJobsResetStaleProcessingNullStartedAt(t *testing.T) {
|
|
repo, _, deviceID, fileID := seedOcrJobFixture(t)
|
|
jobID := NewID()
|
|
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
// Job passé en `processing` sans `started_at` (anomalie de claim) → stale.
|
|
if _, err := repo.OcrJobs.DB.Exec(
|
|
`UPDATE ocr_jobs SET status = 'processing', started_at = NULL WHERE job_id = $1`, jobID); err != nil {
|
|
t.Fatalf("forcer processing sans started_at: %v", err)
|
|
}
|
|
if n, err := repo.OcrJobs.ResetStaleProcessing(time.Minute); err != nil {
|
|
t.Fatalf("reset: %v", err)
|
|
} else if n != 1 {
|
|
t.Errorf("reset started_at NULL : attendu 1, got %d", n)
|
|
}
|
|
if row, _ := repo.OcrJobs.Get(deviceID, jobID); row.Status != "queued" {
|
|
t.Errorf("status après reset = %q, attendu queued", row.Status)
|
|
}
|
|
}
|