package tui import ( "context" "git.a-wan.net/alan/pipelines/internal/buildinfo" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/server" "git.a-wan.net/alan/pipelines/internal/wstest" "net/http/httptest" "path/filepath" "strings" "testing" "time" ) // TestEditorPublicationSurfacesBuildMismatchNotice proves a short-lived TUI // command client routes its build/protocol notice through the shared notice // path instead of dropping it (the artifacts-field regression). func TestEditorPublicationSurfacesBuildMismatchNotice(t *testing.T) { stale := buildinfo.Info{Version: "v0-stale-server", Commit: "deadbeef"} endpoint := wstest.Server(t, func(request protocol.Message) protocol.Message { switch request.Type { case protocol.List: return protocol.Message{Type: protocol.List, Build: &stale, Protocol: protocol.ProtocolVersion, Pipelines: []protocol.PipelineSummary{{ID: 7, Name: "test", Version: 1}}} case protocol.GetPipeline: return protocol.Message{Type: protocol.GetPipeline, Detail: &protocol.PipelineDetail{PipelineSummary: protocol.PipelineSummary{ID: 7, Name: "test", Version: 1}, Source: "published"}} } return protocol.Message{Type: protocol.Pong} }) edit := editedMsg{name: "test", source: "published", baseVersion: 0} result := publicationCheck(context.Background(), endpoint, edit, 0) if len(result.notices) != 1 { t.Fatalf("short-lived client dropped the build mismatch notice: %+v", result.notices) } m := New(context.Background(), endpoint) updated, _ := m.finishEditorPublication(result) m = updated.(Model) if !strings.Contains(strings.Join(m.lines, "\n"), "warning:") { t.Fatalf("notice not routed to the shared TUI notice path: %q", m.lines) } } func TestEditorReturnFindsPublishedVersionAndReloadsLatest(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "publication.db")) if err != nil { t.Fatal(err) } defer store.Close() handler := server.HandlerWithOptions(ctx, server.Options{AllowAuthoring: true}, store) srv := httptest.NewServer(handler) defer func() { cancel(); srv.Close(); server.WaitRuns(handler) }() source := pipeline.Template([]string{"planner"}, true) if _, err := store.Submit(ctx, "test", source); err != nil { t.Fatal(err) } items, _ := store.List(ctx) original, _ := store.Get(ctx, items[0].ID, 1) edited := strings.Replace(source, "Describe the task.", "Published in OpenCode.", 1) if edited == source { t.Fatal("fixture did not change source") } if _, err := store.Submit(ctx, "test", edited); err != nil { t.Fatal(err) } latest := strings.Replace(edited, "Published in OpenCode.", "Another newer update.", 1) if _, err := store.Submit(ctx, "test", latest); err != nil { t.Fatal(err) } m := New(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect") connected := m.connect()().(connectedMsg) defer connected.conn.CloseNow() updated, _ := m.Update(connected) m = updated.(Model) m.detail = original m.keys.detail = true updated, check := m.Update(editedMsg{name: "test", path: "retained.md", source: edited, baseVersion: 1}) m = updated.(Model) if check == nil || m.confirmOpen { t.Fatal("must check before asking to submit") } updated, _ = m.Update(check()) m = updated.(Model) if m.confirmOpen || m.pendingDraft != nil || m.detail.Version != 3 || m.detail.Source != latest || m.pipelines[0].Version != 3 || !strings.Contains(m.submitNotice, "Already published") { t.Fatal("did not find earlier publication and reload latest", m.submitNotice, m.detail) } items, _ = store.List(ctx) if items[0].Version != 3 { t.Fatal("publication check created a duplicate version") } // A local unsent draft must still need approval, even if the server changed. unsent := strings.Replace(edited, "Published in OpenCode.", "Local unsent edits.", 1) updated, check = m.Update(editedMsg{name: "test", path: "retained.md", source: unsent, baseVersion: 1}) m = updated.(Model) updated, _ = m.Update(check()) m = updated.(Model) if !m.confirmOpen || m.pendingDraft.source != unsent || m.pendingDraft.baseVersion != 1 { t.Fatal("unsent changes or original conflict guard lost") } } func TestEditorPublicationFailureAndStaleResultDoNotDiscardDraft(t *testing.T) { m := New(context.Background(), "") m.editorCheckGeneration = 2 edit := editedMsg{name: "test", source: "unsent", baseVersion: 1} updated, _ := m.finishEditorPublication(editorPublicationMsg{generation: 1, edit: edit, published: true}) m = updated.(Model) if m.confirmOpen { t.Fatal("stale result changed editor state") } result := checkEditorPublication(context.Background(), "invalid-url", edit, 2)().(editorPublicationMsg) updated, _ = m.finishEditorPublication(result) m = updated.(Model) if !m.confirmOpen || m.pendingDraft.source != "unsent" || !strings.Contains(m.submitNotice, "Could not check") { t.Fatal("failed check lost local edits", m.submitNotice) } }