|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + plugin "github.com/Paca-AI/plugin-sdk-go" |
| 8 | + "github.com/Paca-AI/plugin-sdk-go/plugintest" |
| 9 | +) |
| 10 | + |
| 11 | +// ── Helpers ─────────────────────────────────────────────────────────────────── |
| 12 | + |
| 13 | +const ( |
| 14 | + testProjectID = "project-1" |
| 15 | + testTaskID = "task-1" |
| 16 | +) |
| 17 | + |
| 18 | +func setupPlugin(t *testing.T) *plugintest.Context { |
| 19 | + t.Helper() |
| 20 | + tc := plugintest.NewContext(t) |
| 21 | + |
| 22 | + // Seed the shared tables referenced by FK. |
| 23 | + tc.DB.SeedRows("tasks", []string{"id", "project_id", "deleted_at"}, [][]any{ |
| 24 | + {testTaskID, testProjectID, nil}, |
| 25 | + }) |
| 26 | + |
| 27 | + // Seed empty plugin tables so queries return empty result sets instead of errors. |
| 28 | + tc.DB.SeedRows("github_integrations", |
| 29 | + []string{"id", "project_id", "access_token_enc", "created_at", "updated_at"}, |
| 30 | + nil) |
| 31 | + tc.DB.SeedRows("github_repositories", |
| 32 | + []string{"id", "project_id", "integration_id", "owner", "repo_name", "full_name", |
| 33 | + "webhook_id", "webhook_secret_enc", "default_branch", "created_at", "updated_at"}, |
| 34 | + nil) |
| 35 | + tc.DB.SeedRows("github_pull_requests", |
| 36 | + []string{"id", "project_id", "repo_id", "pr_number", "github_pr_id", "title", |
| 37 | + "state", "html_url", "head_branch", "base_branch", "author", "merged_at", "created_at", "updated_at"}, |
| 38 | + nil) |
| 39 | + tc.DB.SeedRows("github_task_pr_links", |
| 40 | + []string{"id", "task_id", "pull_request_id", "created_at"}, |
| 41 | + nil) |
| 42 | + tc.DB.SeedRows("github_task_branches", |
| 43 | + []string{"id", "task_id", "repo_id", "branch_name", "created_at"}, |
| 44 | + nil) |
| 45 | + |
| 46 | + var p githubPlugin |
| 47 | + if err := p.Init(tc.PluginContext()); err != nil { |
| 48 | + t.Fatal("Init failed:", err) |
| 49 | + } |
| 50 | + return tc |
| 51 | +} |
| 52 | + |
| 53 | +func callerReq() plugintest.Request { |
| 54 | + return plugintest.Request{ |
| 55 | + Caller: plugin.CallerIdentity{ |
| 56 | + ProjectID: testProjectID, |
| 57 | + CallerID: "member-1", |
| 58 | + CallerRole: "PROJECT_MEMBER", |
| 59 | + }, |
| 60 | + PathParams: map[string]string{}, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// ── Integration tests ───────────────────────────────────────────────────────── |
| 65 | + |
| 66 | +func TestGetIntegration_NotConnected(t *testing.T) { |
| 67 | + tc := setupPlugin(t) |
| 68 | + res := tc.Call("GET", "/github", callerReq()) |
| 69 | + |
| 70 | + if res.StatusCode != 200 { |
| 71 | + t.Fatalf("expected 200, got %d: %s", res.StatusCode, res.BodyString()) |
| 72 | + } |
| 73 | + var env struct { |
| 74 | + Success bool `json:"success"` |
| 75 | + Data integrationResponse `json:"data"` |
| 76 | + } |
| 77 | + if err := json.Unmarshal(res.Body, &env); err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + if !env.Success { |
| 81 | + t.Fatal("expected success=true") |
| 82 | + } |
| 83 | + if env.Data.Connected { |
| 84 | + t.Fatal("expected Connected=false when no token is set") |
| 85 | + } |
| 86 | + if env.Data.ProjectID != testProjectID { |
| 87 | + t.Fatalf("expected project_id=%s, got %s", testProjectID, env.Data.ProjectID) |
| 88 | + } |
| 89 | +} |
0 commit comments