Files
kandev/apps/backend/internal/integrations/secretadapter/secretadapter_test.go
T
João Salavisa 83e01477df refactor: extract shared shapes between jira and linear integrations (#759)
* refactor: extract shared shapes between jira and linear integrations

Factors out the duplicated logic between internal/jira + internal/linear
(backend) and components/jira + components/linear (frontend) into reusable
shared shapes. Follow-up to PR #736.

Backend:
- New internal/integrations/secretadapter wraps secrets.SecretStore with
  the upsert API both jira.SecretStore and linear.SecretStore expect;
  drops both adapter copies from cmd/kandev/services.go.
- New internal/integrations/healthpoll owns the 90s auth-health loop via
  a Prober interface; linear/poller.go is a thin wrapper, jira/poller.go
  keeps only its (jira-specific) issue-watch loop.
- provideServices split into initGitHubService / initJiraService /
  initLinearService — now 75 lines (was ~125, exceeded the 80-line limit).

Frontend:
- New hooks/domains/integrations/use-integration-availability.ts and
  use-integration-enabled.ts; jira/linear hooks reduce to one-line
  delegates and move from components/<x>/ to hooks/domains/<x>/ per the
  project convention Greptile flagged on PR #736.
- New components/integrations/{auth-status-banner,auth-error-message,
  validated-popover}.tsx; the four link/import-bar wrappers drop from
  ~110 lines to ~35 each, and JiraLinkButton picks up the
  clear-error-on-close behaviour LinearLinkButton already had.

Docs:
- New 'Adding a new third-party integration' section in CLAUDE.md/AGENTS.md
  documenting the shared shapes and intentional Jira/Linear divergence
  (transition vs state, JQL vs filter object, eventBus only on jira).

* fix: address PR #759 review feedback

- validated-popover: outline-with-label trigger now sets type="button" and
  honours triggerDisabled, matching ghost-icon (Greptile, Claude, CodeRabbit).
- jira-link-button / linear-link-button: move updateTask() into the awaited
  fetch path so a failed title update surfaces as the popover's inline error
  instead of an unhandled rejection (CodeRabbit).
- use-integration-availability: monotonic requestId race guard prevents a
  slow earlier probe from overwriting a newer auth result (CodeRabbit).
- use-integration-enabled: drop unnecessary async on synchronous init
  (Claude).
- jira/linear poller smoke tests: wrap in testing/synctest instead of
  time.After(2s) per backend testing convention (CodeRabbit nitpick).

Skipped with reasoning on the threads:
- secretadapter Set TOCTOU — single-process SQLite + single-writer per
  workspace; constraint violation on conflict surfaces as an error to the
  caller, not silent corruption.
- healthpoll SetInterval validation — the function isn't called from
  production code today; a panic on misuse would be a footgun and silent
  ignore would just hide bugs differently.

* fix: address PR #759 round-2 review feedback

- jira-settings / linear-settings: drop the local STATUS_REFRESH_MS = 90_000
  copies and import the shared INTEGRATION_STATUS_REFRESH_MS so changing the
  cadence only touches one file (Claude).
- healthpoll: rename Poller.ProbeAll → probeAll. The only callers are the
  loop and the in-package tests, so widening the public API wasn't buying
  anything (Claude).

* fix: invalidate ValidatedPopover in-flight submits on close

If the user clicked Submit and dismissed the popover before the request
settled, the next time they re-opened it would inherit `loading=true` (the
disabled-while-loading submit button was stuck) and a late rejection from
the previous session could repopulate `error` in the new one.

Track the submit via a `useRef` token, bump it on close, and ignore any
result/rejection whose token no longer matches. Also reset `loading` and
`error` on close so the next open starts clean. Adds a regression test
covering both the late-resolution and late-rejection paths.

Addresses CodeRabbit review on PR #759.

* ci: trigger workflow re-run

GitHub Actions did not dispatch the pull_request workflows on the previous
two pushes (1d869f0e, 0ab6c034); only pull_request_target and CodeQL fired.
Empty commit to nudge synchronize event.

* fix: address PR #759 round-3 review feedback

- jira.Poller.Stop: add nil-receiver guard so callers using the
  defer-Stop-after-NewPoller pattern don't panic when initJiraService
  returned nil (Greptile + Claude). Matches linear.Poller's guard.
- healthpoll TestStart_RunsImmediateProbe + TestStart_IsIdempotent:
  rewrap in testing/synctest with synctest.Wait() instead of
  time.After(2s) wall-clock waits. Drops the now-unused probedAll signal
  channel from fakeProber (Claude).

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
2026-05-02 11:50:19 +01:00

160 lines
4.0 KiB
Go

package secretadapter
import (
"context"
"errors"
"testing"
"github.com/kandev/kandev/internal/secrets"
)
// fakeStore is a minimal in-memory secrets.SecretStore for adapter tests. We
// only implement the methods the adapter actually calls; everything else
// fails the test if invoked, which catches accidental dependency creep.
type fakeStore struct {
rows map[string]string
getErr error
failOps map[string]error
}
func newFakeStore() *fakeStore {
return &fakeStore{rows: map[string]string{}, failOps: map[string]error{}}
}
func (f *fakeStore) Create(_ context.Context, s *secrets.SecretWithValue) error {
if err := f.failOps["create"]; err != nil {
return err
}
if _, exists := f.rows[s.ID]; exists {
return errors.New("constraint violation")
}
f.rows[s.ID] = s.Value
return nil
}
func (f *fakeStore) Get(_ context.Context, id string) (*secrets.Secret, error) {
if f.getErr != nil {
return nil, f.getErr
}
if _, ok := f.rows[id]; !ok {
return nil, errors.New("secret not found: " + id)
}
return &secrets.Secret{ID: id}, nil
}
func (f *fakeStore) Reveal(_ context.Context, id string) (string, error) {
v, ok := f.rows[id]
if !ok {
return "", errors.New("secret not found: " + id)
}
return v, nil
}
func (f *fakeStore) Update(_ context.Context, id string, req *secrets.UpdateSecretRequest) error {
if err := f.failOps["update"]; err != nil {
return err
}
if _, ok := f.rows[id]; !ok {
return errors.New("secret not found: " + id)
}
if req.Value != nil {
f.rows[id] = *req.Value
}
return nil
}
func (f *fakeStore) Delete(_ context.Context, id string) error {
delete(f.rows, id)
return nil
}
func (f *fakeStore) List(_ context.Context) ([]*secrets.SecretListItem, error) {
return nil, errors.New("not implemented")
}
func (f *fakeStore) Close() error { return nil }
func TestAdapter_Set_CreatesWhenMissing(t *testing.T) {
store := newFakeStore()
a := New(store)
if err := a.Set(context.Background(), "k1", "name", "v1"); err != nil {
t.Fatalf("Set: %v", err)
}
if got := store.rows["k1"]; got != "v1" {
t.Errorf("expected value v1, got %q", got)
}
}
func TestAdapter_Set_UpdatesWhenPresent(t *testing.T) {
store := newFakeStore()
store.rows["k1"] = "old"
a := New(store)
if err := a.Set(context.Background(), "k1", "name", "new"); err != nil {
t.Fatalf("Set: %v", err)
}
if got := store.rows["k1"]; got != "new" {
t.Errorf("expected value new, got %q", got)
}
}
func TestAdapter_Exists_AbsenceVsError(t *testing.T) {
store := newFakeStore()
a := New(store)
got, err := a.Exists(context.Background(), "missing")
if err != nil || got {
t.Errorf("missing: want (false, nil), got (%v, %v)", got, err)
}
store.rows["here"] = "x"
got, err = a.Exists(context.Background(), "here")
if err != nil || !got {
t.Errorf("present: want (true, nil), got (%v, %v)", got, err)
}
store.getErr = errors.New("db is down")
got, err = a.Exists(context.Background(), "anything")
if err == nil || got {
t.Errorf("transient error: want (false, err), got (%v, %v)", got, err)
}
}
func TestAdapter_Set_DoesNotMaskTransientError(t *testing.T) {
store := newFakeStore()
store.rows["k1"] = "v1"
store.getErr = errors.New("db is down")
a := New(store)
// A transient Get error must surface, not silently fall through to Create.
err := a.Set(context.Background(), "k1", "name", "new")
if err == nil {
t.Fatal("expected error from transient Get failure, got nil")
}
}
func TestAdapter_Reveal_DelegatesToStore(t *testing.T) {
store := newFakeStore()
store.rows["k1"] = "secret"
a := New(store)
got, err := a.Reveal(context.Background(), "k1")
if err != nil || got != "secret" {
t.Errorf("got (%q, %v), want (\"secret\", nil)", got, err)
}
}
func TestAdapter_Delete_DelegatesToStore(t *testing.T) {
store := newFakeStore()
store.rows["k1"] = "v"
a := New(store)
if err := a.Delete(context.Background(), "k1"); err != nil {
t.Fatalf("Delete: %v", err)
}
if _, ok := store.rows["k1"]; ok {
t.Error("Delete did not remove the row")
}
}