127 lines
5.1 KiB
Go
127 lines
5.1 KiB
Go
package semantic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
var testNow = time.Date(2026, 10, 11, 12, 0, 0, 0, time.UTC)
|
|
|
|
type schedulerFake struct {
|
|
jobs []domain.ScheduledJob
|
|
}
|
|
|
|
func (scheduler *schedulerFake) Schedule(_ context.Context, job domain.ScheduledJob) (bool, error) {
|
|
scheduler.jobs = append(scheduler.jobs, job)
|
|
return true, nil
|
|
}
|
|
|
|
func (*schedulerFake) Cancel(context.Context, string, string) (bool, error) { return false, nil }
|
|
|
|
type repositoriesFake struct {
|
|
outbound.ProjectRepositoryStore
|
|
|
|
requested []string
|
|
linked []string
|
|
}
|
|
|
|
func (store *repositoriesFake) Request(_ context.Context, _ uuid.UUID, key string, _ time.Time) error {
|
|
store.requested = append(store.requested, key)
|
|
return nil
|
|
}
|
|
|
|
func (store *repositoriesFake) DeleteUnlinked(_ context.Context, _ uuid.UUID, keys []string) error {
|
|
store.linked = keys
|
|
return nil
|
|
}
|
|
|
|
type unitFake struct {
|
|
outbound.UnitOfWork
|
|
|
|
scheduler *schedulerFake
|
|
repositories *repositoriesFake
|
|
}
|
|
|
|
func (unit unitFake) ScheduledJobs() outbound.JobScheduler { return unit.scheduler }
|
|
func (unit unitFake) ProjectRepositories() outbound.ProjectRepositoryStore { return unit.repositories }
|
|
|
|
func repo(t *testing.T, raw string) domain.GitHubRepository {
|
|
t.Helper()
|
|
parsed, err := domain.ParseGitHubRepositoryURL(raw)
|
|
if err != nil {
|
|
t.Fatalf("ParseGitHubRepositoryURL() error = %v", err)
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func TestTriggerSchedulesOnlyEnabledWorkAndNamesNoRepository(t *testing.T) {
|
|
unit := unitFake{scheduler: &schedulerFake{}, repositories: &repositoriesFake{}}
|
|
candidate := uuid.New()
|
|
kept := repo(t, "https://github.com/Private-Owner/kept")
|
|
added := repo(t, "https://github.com/Private-Owner/added")
|
|
trigger := NewTrigger(true, true)
|
|
if err := trigger.RepositoriesChanged(context.Background(), unit, candidate, []domain.GitHubRepository{kept}, []domain.GitHubRepository{kept, added}, testNow); err != nil {
|
|
t.Fatalf("RepositoriesChanged() error = %v", err)
|
|
}
|
|
if err := trigger.CandidateChanged(context.Background(), unit, candidate, testNow); err != nil {
|
|
t.Fatalf("CandidateChanged() error = %v", err)
|
|
}
|
|
if strings.Join(unit.repositories.requested, ",") != "private-owner/added" || len(unit.repositories.linked) != 2 {
|
|
t.Fatalf("requested %v linked %v, want only the new repository requested", unit.repositories.requested, unit.repositories.linked)
|
|
}
|
|
kinds := map[string]int{}
|
|
for _, job := range unit.scheduler.jobs {
|
|
kinds[job.Kind()]++
|
|
text := strings.ToLower(job.Key() + string(job.Payload()))
|
|
if strings.Contains(text, "private-owner") || strings.Contains(text, "github") {
|
|
t.Fatalf("job %s names the repository: %s %s", job.Kind(), job.Key(), job.Payload())
|
|
}
|
|
}
|
|
if kinds[RepositoryCheckJobKind] != 1 || kinds[CandidateJobKind] != 1 {
|
|
t.Fatalf("jobs = %v", kinds)
|
|
}
|
|
|
|
disabled := unitFake{scheduler: &schedulerFake{}, repositories: &repositoriesFake{}}
|
|
off := NewTrigger(false, false)
|
|
if err := off.RepositoriesChanged(context.Background(), disabled, candidate, nil, []domain.GitHubRepository{added}, testNow); err != nil {
|
|
t.Fatalf("RepositoriesChanged(off) error = %v", err)
|
|
}
|
|
if err := off.CandidateChanged(context.Background(), disabled, candidate, testNow); err != nil {
|
|
t.Fatalf("CandidateChanged(off) error = %v", err)
|
|
}
|
|
if len(disabled.scheduler.jobs) != 0 || len(disabled.repositories.requested) != 1 {
|
|
t.Fatalf("disabled trigger scheduled %d jobs and requested %v, want only the recorded request", len(disabled.scheduler.jobs), disabled.repositories.requested)
|
|
}
|
|
}
|
|
|
|
func TestCheckResultMapsFetchOutcomes(t *testing.T) {
|
|
previous := outbound.RepositoryCheck{Key: "a/b", State: outbound.RepositoryCheckVerified, Evidence: domain.RepositoryEvidence{Description: "old"}}
|
|
verified, wait, err := checkResult(previous, outbound.RepositoryFetch{Found: true, GitHubID: 3, Evidence: domain.RepositoryEvidence{Description: "new"}}, nil, testNow)
|
|
if err != nil || wait != 0 || verified.State != outbound.RepositoryCheckVerified || !evidenceChanged(previous, verified) {
|
|
t.Fatalf("verified = %+v, %v, %v", verified, wait, err)
|
|
}
|
|
missing, _, _ := checkResult(previous, outbound.RepositoryFetch{}, nil, testNow)
|
|
if missing.State != outbound.RepositoryCheckNotFound || missing.Evidence.Description != "" || !evidenceChanged(previous, missing) {
|
|
t.Fatalf("missing = %+v", missing)
|
|
}
|
|
limited, wait, err := checkResult(previous, outbound.RepositoryFetch{}, &outbound.RepositoryFetchError{Class: outbound.RepositoryFailureRateLimited, RetryAfter: time.Hour}, testNow)
|
|
if err != nil || wait != time.Hour || limited.State != outbound.RepositoryCheckUnavailable || limited.Failure != outbound.RepositoryFailureRateLimited {
|
|
t.Fatalf("limited = %+v, %v, %v", limited, wait, err)
|
|
}
|
|
if _, _, err := checkResult(previous, outbound.RepositoryFetch{}, &outbound.RepositoryFetchError{Class: outbound.RepositoryFailureNetwork}, testNow); !errors.Is(err, outbound.ErrRepositoryFetchUnavailable) {
|
|
t.Fatalf("a network failure was not returned for a retry: %v", err)
|
|
}
|
|
same := previous
|
|
if evidenceChanged(previous, same) {
|
|
t.Fatal("an unchanged check reported a change")
|
|
}
|
|
}
|