Files
kandev/apps/backend/internal/linear/poller_test.go
T
João Salavisa 7b5b3f329d feat: add Linear integration (#736)
* feat: add Linear integration

Mirrors the existing Jira integration so a workspace can connect to Linear
with a personal API key, browse and search issues, and start a Kandev task
from any issue. Backend uses Linear's GraphQL API with a workspace-scoped
config + secret store and a 90s auth-health poller; the frontend adds a
settings page, an issue dialog with state transitions, link/import buttons
in the task UI, and a paginated /linear browse page with a Start task flow.

* fix(linear): address PR review feedback

- Route httpListStates through Service.ListStates instead of clientFor
- Extract repeated workspace_id required string into a constant
- Log GetConfig errors during credential resolution instead of dropping them
- Clear popover error state on close in import + link buttons
- Pick the first workflow with steps in the quick task launcher (don't hard-pin to workflows[0])
- Reset useLinearEnabled state on workspace change via tag-based pattern
- Add cursor-pointer to anchor + Link elements per project convention
- Add tests: handlers_test.go, linear-api.test.ts, use-linear-enabled.test.ts, linear-issue-common.test.tsx
- Document internal/jira and internal/linear in CLAUDE.md/AGENTS.md

* fix(linear): address second-round PR review feedback

- Round Linear's fractional state position instead of truncating it via
  int(math.Round(s.Position)) so adjacent fractional positions don't collapse
- Force LinearSettings to remount on workspace change with key={id} so the
  previous workspace's config / form / testResult / teams cannot bleed into
  the next workspace before the new fetches settle
- Broaden the legacy-value useLinearEnabled test to cover "true", "1", "yes",
  and "legacy" so a regression to strict value === "true" would fail

* fix(linear): address third-round PR review feedback

- Propagate context.Canceled / context.DeadlineExceeded from ListStates in
  GetIssue instead of swallowing them — a caller cancellation must not return
  a partial-success response
- Drop the one-off contains() test helper in handlers_test.go and use
  strings.Contains directly so the assertion is self-documenting
- Add getLinearIssue URL-shape test that locks identifier-as-path-segment
  vs workspaceId-as-query-param so accidentally swapping them would fail

* test(linear): extract repeated literals in linear-api.test.ts

Pulls "ws-1", the API base URL, and the auth-method literal into module-
level constants so the assertions read as the route shape they care about
and the duplicate-strings ESLint rule (currently warn, threshold 4)
stays quiet as the file grows.

---------

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

188 lines
4.7 KiB
Go

package linear
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/kandev/kandev/internal/common/logger"
)
type pollerFixture struct {
store *Store
secrets *fakeSecretStore
client *fakeClient
svc *Service
poller *Poller
}
func newPollerFixture(t *testing.T) *pollerFixture {
t.Helper()
f := &pollerFixture{
store: newTestStore(t),
secrets: newFakeSecretStore(),
client: &fakeClient{},
}
f.svc = NewService(f.store, f.secrets, func(_ *LinearConfig, _ string) Client {
return f.client
}, logger.Default())
f.poller = NewPoller(f.svc, logger.Default())
return f
}
// saveConfig persists a workspace config directly via the store + secret
// fakes — bypassing Service.SetConfig avoids racing against its async probe.
func (f *pollerFixture) saveConfig(t *testing.T, workspaceID, secret string) {
t.Helper()
ctx := context.Background()
if err := f.store.UpsertConfig(ctx, &LinearConfig{
WorkspaceID: workspaceID,
AuthMethod: AuthMethodAPIKey,
}); err != nil {
t.Fatalf("save config %s: %v", workspaceID, err)
}
if err := f.secrets.Set(ctx, SecretKeyForWorkspace(workspaceID),
"linear", secret); err != nil {
t.Fatalf("save secret %s: %v", workspaceID, err)
}
}
func TestPoller_ProbeAll_RecordsSuccess(t *testing.T) {
f := newPollerFixture(t)
f.saveConfig(t, "ws-1", "tok")
f.client.testAuthFn = func() (*TestConnectionResult, error) {
return &TestConnectionResult{OK: true, OrgSlug: "acme"}, nil
}
f.poller.probeAll(context.Background())
cfg, _ := f.store.GetConfig(context.Background(), "ws-1")
if cfg == nil {
t.Fatal("config disappeared")
}
if !cfg.LastOk {
t.Error("expected LastOk=true after successful probe")
}
if cfg.OrgSlug != "acme" {
t.Errorf("expected org_slug captured, got %q", cfg.OrgSlug)
}
}
func TestPoller_ProbeAll_RecordsFailure(t *testing.T) {
f := newPollerFixture(t)
f.saveConfig(t, "ws-1", "tok")
f.client.testAuthFn = func() (*TestConnectionResult, error) {
return &TestConnectionResult{OK: false, Error: "401 unauthorized"}, nil
}
f.poller.probeAll(context.Background())
cfg, _ := f.store.GetConfig(context.Background(), "ws-1")
if cfg.LastOk {
t.Error("expected LastOk=false after failed probe")
}
if cfg.LastError != "401 unauthorized" {
t.Errorf("expected error preserved, got %q", cfg.LastError)
}
}
func TestPoller_ProbeAll_ClientError(t *testing.T) {
f := newPollerFixture(t)
f.saveConfig(t, "ws-1", "tok")
f.client.testAuthFn = func() (*TestConnectionResult, error) {
return nil, errors.New("network timeout")
}
f.poller.probeAll(context.Background())
cfg, _ := f.store.GetConfig(context.Background(), "ws-1")
if cfg.LastOk {
t.Error("expected LastOk=false on client error")
}
}
func TestPoller_ProbeAll_NoWorkspaces(t *testing.T) {
f := newPollerFixture(t)
f.poller.probeAll(context.Background())
}
func TestPoller_ProbeAll_MultipleWorkspaces(t *testing.T) {
f := newPollerFixture(t)
f.saveConfig(t, "ws-a", "tok-a")
f.saveConfig(t, "ws-b", "tok-b")
calls := 0
f.client.testAuthFn = func() (*TestConnectionResult, error) {
calls++
return &TestConnectionResult{OK: true}, nil
}
f.poller.probeAll(context.Background())
if calls != 2 {
t.Errorf("expected 2 probe calls, got %d", calls)
}
for _, id := range []string{"ws-a", "ws-b"} {
cfg, _ := f.store.GetConfig(context.Background(), id)
if !cfg.LastOk || cfg.LastCheckedAt == nil {
t.Errorf("workspace %s missing health update: %+v", id, cfg)
}
}
}
func TestPoller_StartStop(t *testing.T) {
f := newPollerFixture(t)
f.saveConfig(t, "ws-1", "tok")
probed := make(chan struct{}, 1)
f.client.testAuthFn = func() (*TestConnectionResult, error) {
select {
case probed <- struct{}{}:
default:
}
return &TestConnectionResult{OK: true}, nil
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
f.poller.Start(ctx)
defer f.poller.Stop()
select {
case <-probed:
case <-time.After(2 * time.Second):
t.Fatal("poller did not record an initial probe within 2s")
}
}
func TestPoller_StartIsIdempotent(t *testing.T) {
f := newPollerFixture(t)
f.saveConfig(t, "ws-1", "tok")
var calls int32
probed := make(chan struct{}, 1)
f.client.testAuthFn = func() (*TestConnectionResult, error) {
atomic.AddInt32(&calls, 1)
select {
case probed <- struct{}{}:
default:
}
return &TestConnectionResult{OK: true}, nil
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
f.poller.Start(ctx)
f.poller.Start(ctx)
select {
case <-probed:
case <-time.After(2 * time.Second):
t.Fatal("poller did not run an initial probe within 2s")
}
f.poller.Stop()
if got := atomic.LoadInt32(&calls); got != 1 {
t.Errorf("expected exactly 1 probe call from the initial run, got %d", got)
}
}