caelum0x 7c86828fec fix(rithub): RIT-ACT async-import pushed to the public URL, defeating its own purpose
Live-verified during the 2026-11-30 deploy pass: the mirror push used
job.HTMLURL (the engine's public ROOT_URL, https://rithub.dev/...) as
the git remote, not the loopback engine address. A real async migrate
of a tiny repo (octocat/Hello-World) got stuck in "pushing" for minutes
on an established-but-stalled TLS connection back out through
Cloudflare's edge to the box's own public hostname -- exactly the
Cloudflare-edge timeout this whole sidecar exists to eliminate.

Root cause: POST /user/repos on the real engine always reports html_url
built from its configured public ROOT_URL, regardless of whether the
create-repo call itself was made over loopback (which it correctly was).
Runner.Run then reused that public URL as the push target.

Fix: add Runner.UpstreamBase (the same loopback base main.go already
passes to the API client) and build the push target from it directly
(UpstreamBase + "/" + owner + "/" + repo + ".git"), never from
job.HTMLURL. Added a regression-guard test that asserts the push target
is loopback-based. go vet/test -race clean. Verified live after
redeploy: a real async migrate of octocat/Hello-World went
202 -> cloning -> pushing -> done in a few seconds, correct commit
landed, old sync path (async omitted) unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 21:10:00 +03:00

154 lines
5.0 KiB
Go

package migrate
import (
"context"
"errors"
"testing"
"github.com/caelum0x/railyard/apps/rithub-async-import/internal/jobs"
)
// fakeGitOps lets the orchestration logic in Runner.Run be tested without
// ever invoking the real git binary or touching the network.
type fakeGitOps struct {
cloneErr error
pushErr error
cloneCall int
pushCall int
lastPushTarget string
}
func (f *fakeGitOps) Clone(ctx context.Context, cloneAddr, destDir, authHeader string) error {
f.cloneCall++
return f.cloneErr
}
func (f *fakeGitOps) PushMirror(ctx context.Context, repoDir, targetURL, authHeader string) error {
f.pushCall++
f.lastPushTarget = targetURL
return f.pushErr
}
func seedJob(t *testing.T, store *jobs.Store) jobs.Job {
t.Helper()
job := jobs.Job{ID: "job1", Status: jobs.StatusCloning, RepoOwner: "caelum0x", RepoName: "hello", CloneAddr: "https://github.com/octocat/Hello-World.git", HTMLURL: "https://rithub.dev/caelum0x/hello"}
if err := store.Create(job); err != nil {
t.Fatalf("seeding job: %v", err)
}
return job
}
func TestRunner_Run_HappyPath(t *testing.T) {
store := newTestStore(t)
job := seedJob(t, store)
ops := &fakeGitOps{}
upstream := &fakeUpstream{}
r := &Runner{Store: store, Upstream: upstream, Ops: ops, UpstreamBase: "http://127.0.0.1:3350"}
r.Run(context.Background(), job, Request{CloneAddr: job.CloneAddr, RepoName: job.RepoName}, "caller-token")
if ops.cloneCall != 1 || ops.pushCall != 1 {
t.Fatalf("clone/push calls = %d/%d, want 1/1", ops.cloneCall, ops.pushCall)
}
// Regression guard (2026-11-30, live incident): the push target must
// be built from UpstreamBase (the engine's loopback address), never
// from job.HTMLURL (the engine's public ROOT_URL) -- pushing to the
// public URL round-trips through Cloudflare's edge and hangs, which
// is exactly what this service exists to avoid.
wantTarget := "http://127.0.0.1:3350/caelum0x/hello.git"
if ops.lastPushTarget != wantTarget {
t.Fatalf("push target = %q, want %q (must not be job.HTMLURL %q)", ops.lastPushTarget, wantTarget, job.HTMLURL)
}
if !upstream.updateCalled {
t.Fatal("expected UpdateRepoDescription to be called to clear the migrating marker")
}
final, ok := store.Get(job.ID)
if !ok {
t.Fatal("job missing after run")
}
if final.Status != jobs.StatusDone {
t.Fatalf("status = %q, want done; error=%q", final.Status, final.Error)
}
}
func TestRunner_Run_CloneFailure(t *testing.T) {
store := newTestStore(t)
job := seedJob(t, store)
ops := &fakeGitOps{cloneErr: errors.New("connection reset")}
r := &Runner{Store: store, Upstream: &fakeUpstream{}, Ops: ops}
r.Run(context.Background(), job, Request{CloneAddr: job.CloneAddr, RepoName: job.RepoName}, "caller-token")
if ops.pushCall != 0 {
t.Fatalf("push should not run after a clone failure, got %d calls", ops.pushCall)
}
final, _ := store.Get(job.ID)
if final.Status != jobs.StatusFailed {
t.Fatalf("status = %q, want failed", final.Status)
}
if final.Error == "" {
t.Fatal("expected a recorded error message")
}
}
func TestRunner_Run_PushFailure(t *testing.T) {
store := newTestStore(t)
job := seedJob(t, store)
ops := &fakeGitOps{pushErr: errors.New("permission denied")}
r := &Runner{Store: store, Upstream: &fakeUpstream{}, Ops: ops}
r.Run(context.Background(), job, Request{CloneAddr: job.CloneAddr, RepoName: job.RepoName}, "caller-token")
final, _ := store.Get(job.ID)
if final.Status != jobs.StatusFailed {
t.Fatalf("status = %q, want failed", final.Status)
}
}
func TestRunner_Run_DescriptionUpdateFailureStillSucceeds(t *testing.T) {
store := newTestStore(t)
job := seedJob(t, store)
ops := &fakeGitOps{}
upstream := &fakeUpstream{updateDesc: func(ctx context.Context, token, owner, name, description string) error {
return errors.New("transient 500")
}}
r := &Runner{Store: store, Upstream: upstream, Ops: ops}
r.Run(context.Background(), job, Request{CloneAddr: job.CloneAddr, RepoName: job.RepoName}, "caller-token")
final, _ := store.Get(job.ID)
if final.Status != jobs.StatusDone {
t.Fatalf("status = %q, want done even if clearing the marker failed", final.Status)
}
if final.Error == "" {
t.Fatal("expected the description-update failure to be noted, even though the job still succeeded")
}
}
func TestBasicAuthHeader(t *testing.T) {
cases := []struct {
name string
user, pass, token string
wantEmpty bool
}{
{name: "anonymous", wantEmpty: true},
{name: "token only", token: "abc123"},
{name: "username and password", user: "alice", pass: "secret"},
{name: "username with token as password", user: "alice", token: "abc123"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := basicAuthHeader(tc.user, tc.pass, tc.token)
if tc.wantEmpty && got != "" {
t.Fatalf("basicAuthHeader(...) = %q, want empty", got)
}
if !tc.wantEmpty && got == "" {
t.Fatal("basicAuthHeader(...) = empty, want a Basic header")
}
if got != "" && got[:6] != "Basic " {
t.Fatalf("basicAuthHeader(...) = %q, want it to start with \"Basic \"", got)
}
})
}
}