Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

39.7 KB
Raw Blame
import { and, eq, sql } from "drizzle-orm";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import db, { getDatabasePool, schema } from "../../apps/api/src/database";
import { importIssues } from "../../apps/api/src/github-integration/controllers/import-issues";
import { withGithubImportLock } from "../../apps/api/src/github-integration/import-lock";
import { IMPORT_PAGES_PER_REQUEST } from "../../apps/api/src/github-integration/import-pages";
import { createApp } from "../../apps/api/src/index";
import moveTask from "../../apps/api/src/task/controllers/move-task";
import { handleIssueOpened } from "../../apps/api/src/plugins/github/webhooks/issue-opened";
import { mockAuthenticatedSession } from "./helpers/auth";
import { resetTestDatabase } from "./helpers/database";
import {
  createProjectFixture,
  createWorkspaceMember,
} from "./helpers/fixtures";

const mocks = vi.hoisted(() => ({
  graphql: vi.fn(),
  verify: vi.fn(),
  comment: vi.fn(),
  publish: vi.fn(
    async (_name: string, _payload: { projectId: string; taskId?: string }) =>
      undefined,
  ),
}));
vi.mock("../../apps/api/src/plugins/github/utils/github-app", () => ({
  getVerifiedInstallationOctokit: mocks.verify,
  getGithubApp: () => ({
    getInstallationOctokit: async () => ({
      rest: { issues: { createComment: mocks.comment } },
    }),
  }),
}));
vi.mock("../../apps/api/src/events", async (original) => ({
  ...(await original<typeof import("../../apps/api/src/events")>()),
  publishEvent: mocks.publish,
}));
const old = "2020-01-01T00:00:00Z";
function connection<T>(
  nodes: T[],
  more = false,
  cursor: string | null = null,
  totalCount = nodes.length,
) {
  return {
    nodes,
    totalCount,
    pageInfo: { hasNextPage: more, endCursor: cursor },
  };
}
function issue(number: number, options: Record<string, unknown> = {}) {
  return {
    number,
    title: `Issue ${number}`,
    body: "Description",
    url: `https://github.com/example/repo/issues/${number}`,
    state: "OPEN",
    createdAt: old,
    author: null,
    labels: connection([]),
    comments: connection([]),
    ...options,
  };
}
function comment(number: number, options: Record<string, unknown> = {}) {
  return {
    body: `Comment ${number}`,
    url: `https://github.com/example/repo/issues/1#issuecomment-${number}`,
    createdAt: old,
    author: {
      login: "contributor",
      avatarUrl: "https://avatars.githubusercontent.com/u/1",
      __typename: "User",
    },
    ...options,
  };
}
function label(number: number) {
  return { name: `label-${number}`, color: "abcdef" };
}
function issuePage(
  nodes: ReturnType<typeof issue>[],
  more = false,
  cursor: string | null = null,
  total = nodes.length,
) {
  return {
    repository: {
      databaseId: 2,
      issues: connection(nodes, more, cursor, total),
    },
  };
}
const emptyPulls = () => ({
  repository: { databaseId: 2, pullRequests: connection([]) },
});
function serveIssues(count: number) {
  mocks.graphql.mockImplementation(
    async (query: string, variables: { cursor: string | null }) => {
      if (query.includes("query ImportPullRequests(")) return emptyPulls();
      const number = Number(variables.cursor ?? 0) + 1;
      return issuePage([issue(number)], number < count, String(number), count);
    },
  );
}
function deferred() {
  let resolve!: () => void;
  const promise = new Promise<void>((r) => {
    resolve = r;
  });
  return { promise, resolve };
}
beforeEach(async () => {
  await resetTestDatabase();
  mocks.graphql.mockReset();
  mocks.verify.mockReset().mockResolvedValue({ graphql: mocks.graphql });
  mocks.comment.mockReset();
  mocks.publish.mockReset().mockResolvedValue(undefined);
});
async function setup() {
  const member = await createWorkspaceMember({ role: "admin" });
  const { project, columns } = await createProjectFixture({
    workspaceId: member.workspace.id,
  });
  const config = {
    repositoryOwner: "example",
    repositoryName: "repo",
    installationId: 1,
    repositoryId: 2,
    verifiedGithubAccountId: "3",
    verifiedByUserId: member.user.id,
    commentTaskLinkOnGitHubIssue: false,
  };
  const [integration] = await db
    .insert(schema.integrationTable)
    .values({
      projectId: project.id,
      type: "github",
      isActive: true,
      config: JSON.stringify(config),
    })
    .returning();
  mockAuthenticatedSession(member.user);
  const { app } = createApp();
  const request = (runId?: string) =>
    app.request("/api/github-integration/import-issues", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        projectId: project.id,
        ...(runId ? { runId } : {}),
      }),
    });
  return { member, project, columns, integration, config, app, request };
}
async function saved() {
  return db.query.githubImportTable.findFirst();
}

describe("bounded resumable GitHub import", () => {
  it.each([false, true])(
    "fails closed for invalid stored rules (linked=%s)",
    async (linked) => {
      const { project, integration, config, request } = await setup();
      if (linked) {
        const [task] = await db
          .insert(schema.taskTable)
          .values({
            projectId: project.id,
            title: "Existing task",
            number: 1,
            status: "to-do",
          })
          .returning();
        await db.insert(schema.externalLinkTable).values({
          taskId: task!.id,
          integrationId: integration.id,
          resourceType: "issue",
          externalId: "1",
          url: "https://github.com/example/repo/issues/1",
        });
      }
      await db
        .update(schema.integrationTable)
        .set({
          config: JSON.stringify({
            ...config,
            syncRules: {
              outgoing: { mode: "all" },
              incoming: { mode: "labels", match: "any", labels: [] },
            },
          }),
        })
        .where(eq(schema.integrationTable.id, integration.id));
      serveIssues(1);
      const response = await request();
      expect(response.status).toBe(200);
      expect(await response.json()).toMatchObject({ imported: 0, skipped: 1 });
      const tasks = await db.query.taskTable.findMany();
      const links = await db.query.externalLinkTable.findMany();
      expect(tasks).toHaveLength(linked ? 1 : 0);
      expect(links).toHaveLength(linked ? 1 : 0);
      if (linked) {
        expect(tasks[0]?.title).toBe("Existing task");
        expect(JSON.parse(links[0]!.metadata!)).toMatchObject({
          syncFilterPaused: true,
        });
      }
    },
  );
  it("preserves provider colors when rule checks import labels before the page", async () => {
    const { member, integration, config, request } = await setup();
    await db.insert(schema.labelTable).values({
      workspaceId: member.workspace.id,
      name: "workspace",
      color: "#123456",
    });
    await db
      .update(schema.integrationTable)
      .set({
        config: JSON.stringify({
          ...config,
          syncRules: {
            outgoing: { mode: "all" },
            incoming: { mode: "labels", match: "any", labels: ["approved"] },
          },
        }),
      })
      .where(eq(schema.integrationTable.id, integration.id));
    const labels = [
      { name: "approved", color: "abcdef" },
      { name: "workspace", color: "fedcba" },
    ];
    mocks.verify.mockResolvedValue({
      graphql: mocks.graphql,
      rest: {
        issues: {
          get: async () => ({
            data: {
              labels: [
                ...labels,
                { name: "scope-only", color: "#654321" },
                "no-color",
              ],
            },
          }),
        },
      },
    });
    mocks.graphql.mockImplementation(async (query: string) =>
      query.includes("query ImportIssues(")
        ? issuePage([issue(1, { labels: connection(labels) })])
        : emptyPulls(),
    );
    expect((await request()).status).toBe(200);
    const link = (await db.query.externalLinkTable.findMany())[0]!;
    const assigned = await db.query.labelTable.findMany({
      where: eq(schema.labelTable.taskId, link.taskId),
    });
    expect(
      Object.fromEntries(assigned.map(({ name, color }) => [name, color])),
    ).toEqual({
      approved: "#abcdef",
      workspace: "#123456",
      "scope-only": "#654321",
      "no-color": "#6B7280",
    });
  });
  it("pauses a newly imported issue that misses the outgoing rule before announcing it", async () => {
    const { member, integration, config, request } = await setup();
    const [root] = await db
      .insert(schema.labelTable)
      .values({
        workspaceId: member.workspace.id,
        name: "export",
        color: "red",
      })
      .returning();
    await db
      .update(schema.integrationTable)
      .set({
        config: JSON.stringify({
          ...config,
          syncRules: {
            outgoing: { mode: "labels", match: "any", labels: [root.id] },
            incoming: { mode: "all" },
          },
        }),
      })
      .where(eq(schema.integrationTable.id, integration.id));
    mocks.verify.mockResolvedValue({
      graphql: mocks.graphql,
      rest: { issues: { get: async () => ({ data: { labels: [] } }) } },
    });
    serveIssues(1);
    expect((await request()).status).toBe(200);
    const link = (await db.query.externalLinkTable.findMany())[0]!;
    expect(JSON.parse(link.metadata!)).toMatchObject({
      syncFilterPaused: true,
    });
  });

  it.each(["new", "legacy-new", "legacy-existing"])(
    "preserves %s issue history across import requests",
    async (history) => {
      const { member, project, integration, config } = await setup();
      const existing = history === "legacy-existing";
      if (existing) {
        const [task] = await db
          .insert(schema.taskTable)
          .values({
            projectId: project.id,
            number: 1,
            title: "Existing imported task",
            status: "to-do",
          })
          .returning();
        await db.insert(schema.externalLinkTable).values({
          taskId: task.id,
          integrationId: integration.id,
          resourceType: "issue",
          externalId: "1",
          url: "https://github.com/example/repo/issues/1",
          createdAt: new Date(old),
          metadata: JSON.stringify({ createdFrom: "github-import" }),
        });
      }
      const [root] = await db
        .insert(schema.labelTable)
        .values({
          workspaceId: member.workspace.id,
          name: "export",
          color: "red",
        })
        .returning();
      await db
        .update(schema.integrationTable)
        .set({
          config: JSON.stringify({
            ...config,
            syncRules: {
              outgoing: existing
                ? { mode: "all" }
                : { mode: "labels", match: "any", labels: [root.id] },
              incoming: { mode: "all" },
            },
          }),
        })
        .where(eq(schema.integrationTable.id, integration.id));
      mocks.verify.mockResolvedValue({
        graphql: mocks.graphql,
        rest: { issues: { get: async () => ({ data: { labels: [] } }) } },
      });
      const comments = Array.from({ length: 85 }, (_, index) => comment(index));
      mocks.graphql.mockImplementation(
        async (query: string, vars: { cursor: string | null }) => {
          if (query.includes("query ImportIssues("))
            return issuePage([
              issue(1, {
                comments: connection(
                  comments.slice(0, 20),
                  true,
                  "20",
                  comments.length,
                ),
              }),
            ]);
          if (query.includes("query ImportIssueComments(")) {
            const start = Number(vars.cursor);
            const end = Math.min(start + 20, comments.length);
            return {
              repository: {
                databaseId: 2,
                issue: {
                  comments: connection(
                    comments.slice(start, end),
                    end < comments.length,
                    String(end),
                    comments.length,
                  ),
                },
              },
            };
          }
          return emptyPulls();
        },
      );
      let result = await importIssues(project.id);
      expect(result).toMatchObject({
        pending: true,
        imported: existing ? 0 : 1,
        updated: existing ? 1 : 0,
        skipped: 0,
      });
      expect(
        JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!)
          .syncFilterPaused === true,
      ).toBe(!existing);
      const initialComments = (await db.query.activityTable.findMany()).length;
      if (history !== "new") {
        const persisted = (await saved())!;
        delete persisted.state.currentIssue!.isNewTask;
        await db
          .update(schema.githubImportTable)
          .set({ state: persisted.state })
          .where(eq(schema.githubImportTable.integrationId, integration.id));
      }
      if (existing)
        await db
          .update(schema.integrationTable)
          .set({
            config: JSON.stringify({
              ...config,
              syncRules: {
                outgoing: { mode: "labels", match: "any", labels: [root.id] },
                incoming: { mode: "all" },
              },
            }),
          })
          .where(eq(schema.integrationTable.id, integration.id));
      result = await importIssues(project.id, result.runId);
      expect(result).toMatchObject({
        pending: false,
        imported: existing ? 0 : 1,
        updated: existing ? 1 : 0,
        skipped: existing ? 1 : 0,
      });
      expect(await db.query.activityTable.findMany()).toHaveLength(
        existing ? initialComments : comments.length,
      );
      expect(
        JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!),
      ).toMatchObject({ syncFilterPaused: true });
      expect(await importIssues(project.id)).toMatchObject({
        pending: false,
        imported: 0,
        skipped: 1,
      });
      expect(await db.query.activityTable.findMany()).toHaveLength(
        existing ? initialComments : comments.length,
      );
    },
  );
  it("rejects an incoming rule change while fetching a page before creating tasks or consuming its cursor", async () => {
    const { integration, config, request } = await setup();
    const started = deferred();
    const release = deferred();
    mocks.graphql.mockImplementationOnce(async () => {
      started.resolve();
      await release.promise;
      return issuePage([issue(1)], true, "first", 2);
    });
    const importing = request();
    await started.promise;
    await db
      .update(schema.integrationTable)
      .set({
        config: JSON.stringify({
          ...config,
          syncRules: {
            outgoing: { mode: "all" },
            incoming: { mode: "labels", labels: ["approved"], match: "any" },
          },
        }),
      })
      .where(eq(schema.integrationTable.id, integration.id));
    release.resolve();
    expect((await importing).status).toBe(409);
    expect(await db.query.taskTable.findMany()).toHaveLength(0);
    expect(await db.query.externalLinkTable.findMany()).toHaveLength(0);
    expect((await saved())?.state).toMatchObject({
      phase: "issues",
      issueCursor: null,
      imported: 0,
    });
  });

  it("bounds each HTTP step, persists resumable state, imports every issue and retries completion without restarting", async () => {
    const { request, app, project } = await setup();
    serveIssues(9);
    const observedCounts: number[] = [];
    mocks.publish.mockImplementation(async (name, payload) => {
      if (name === "project.updated") {
        expect(payload).toEqual({ projectId: project.id });
        observedCounts.push((await db.query.taskTable.findMany()).length);
      } else {
        expect(
          await db.query.taskTable.findFirst({
            where: eq(schema.taskTable.id, payload.taskId!),
          }),
        ).toBeDefined();
      }
    });
    const first = await request();
    expect(first.status).toBe(202);
    const progress = await first.json();
    expect(progress).toMatchObject({
      pending: true,
      imported: IMPORT_PAGES_PER_REQUEST,
      updated: 0,
    });
    expect(mocks.graphql).toHaveBeenCalledTimes(IMPORT_PAGES_PER_REQUEST);
    expect(observedCounts).toEqual([1, 2, 3, 4]);
    const info = await app.request(
      `/api/github-integration/project/${project.id}`,
    );
    expect((await info.json()).importProgress).toEqual(progress);
    let response = await request(progress.runId);
    expect(response.status).toBe(202);
    response = await request(progress.runId);
    expect(response.status).toBe(200);
    expect(await response.json()).toMatchObject({
      pending: false,
      imported: 9,
    });
    expect(await db.query.taskTable.findMany()).toHaveLength(9);
    expect(await db.query.externalLinkTable.findMany()).toHaveLength(9);
    const calls = mocks.graphql.mock.calls.length;
    expect((await request(progress.runId)).status).toBe(200);
    expect(mocks.graphql).toHaveBeenCalledTimes(calls);
    expect(await db.query.githubImportTable.findMany()).toHaveLength(1);
    expect(mocks.verify).toHaveBeenCalledWith(expect.anything(), true);
  });

  it("resumes after provider failure without replaying committed pages or leaking provider errors", async () => {
    const { request, project } = await setup();
    serveIssues(7);
    const original = mocks.graphql.getMockImplementation();
    if (!original) throw new Error("Expected provider mock");
    mocks.graphql.mockImplementation(async (query, vars) => {
      if (vars.cursor === "2") throw new Error("private-token-provider-url");
      return original(query, vars);
    });
    const failed = await request();
    expect(failed.status).toBe(502);
    expect(await failed.text()).not.toContain("private-token");
    expect(await db.query.taskTable.findMany()).toHaveLength(2);
    expect((await saved())?.state).toMatchObject({
      imported: 2,
      issueCursor: "2",
    });
    mocks.graphql.mockImplementation(original);
    let result = await importIssues(project.id);
    while (result.pending)
      result = await importIssues(project.id, result.runId);
    expect(result).toMatchObject({ imported: 7, updated: 0 });
    expect(await db.query.taskTable.findMany()).toHaveLength(7);
  });

  it("processes all label and comment pages, applies late system labels and deduplicates comments on reimport", async () => {
    const { project, columns } = await setup();
    const labels = Array.from({ length: 27 }, (_, n) => label(n));
    labels[25] = { name: "status:in-progress", color: "ffffff" };
    labels[26] = { name: "priority:urgent", color: "ffffff" };
    const comments = Array.from({ length: 85 }, (_, n) => comment(n));
    comments[2] = comment(2, {
      author: {
        login: "robot",
        avatarUrl: "https://example.test/a",
        __typename: "Bot",
      },
    });
    comments[3] = comment(3, {
      author: {
        login: "robot[bot]",
        avatarUrl: "https://example.test/a",
        __typename: "User",
      },
    });
    comments[84] = comment(84, { createdAt: "2099-01-01T00:00:00Z" });
    mocks.graphql.mockImplementation(
      async (query: string, vars: { cursor: string | null }) => {
        if (query.includes("query ImportIssues("))
          return issuePage([
            issue(1, {
              labels: connection(labels.slice(0, 25), true, "25", 27),
              comments: connection(comments.slice(0, 20), true, "20", 85),
            }),
          ]);
        if (query.includes("query ImportIssueLabels("))
          return {
            repository: {
              databaseId: 2,
              issue: { labels: connection(labels.slice(25), false, "27", 27) },
            },
          };
        if (query.includes("query ImportIssueComments(")) {
          const offset = Number(vars.cursor);
          const end = Math.min(85, offset + 20);
          return {
            repository: {
              databaseId: 2,
              issue: {
                comments: connection(
                  comments.slice(offset, end),
                  end < 85,
                  String(end),
                  85,
                ),
              },
            },
          };
        }
        return emptyPulls();
      },
    );
    const delivered: Array<{
      name: string;
      priority?: string;
      labels: number;
      comments: number;
    }> = [];
    mocks.publish.mockImplementation(async (name, payload) => {
      if (!payload.taskId) return;
      const committed = await db.query.taskTable.findFirst({
        where: eq(schema.taskTable.id, payload.taskId),
      });
      delivered.push({
        name,
        priority: committed?.priority,
        labels: (await db.query.labelTable.findMany()).length,
        comments: (await db.query.activityTable.findMany()).length,
      });
    });
    let result = await importIssues(project.id);
    expect(result.pending).toBe(true);
    expect((await saved())?.state).toMatchObject({
      phase: "comments",
      currentIssue: { commentCursor: "60", labelsRemaining: 0 },
    });
    result = await importIssues(project.id, result.runId);
    expect(result.pending).toBe(false);
    expect(await db.query.labelTable.findMany()).toHaveLength(25);
    expect(await db.query.activityTable.findMany()).toHaveLength(82);
    expect(delivered).toEqual(
      expect.arrayContaining([
        expect.objectContaining({
          name: "task.labels_updated",
          priority: "urgent",
          labels: 25,
        }),
        expect.objectContaining({ name: "comment.updated", comments: 82 }),
      ]),
    );
    expect(await db.query.taskTable.findFirst()).toMatchObject({
      priority: "urgent",
      status: "in-progress",
      columnId: columns.inProgress.id,
    });
    result = await importIssues(project.id);
    while (result.pending)
      result = await importIssues(project.id, result.runId);
    expect(result).toMatchObject({ imported: 0, updated: 1 });
    expect(await db.query.activityTable.findMany()).toHaveLength(82);
    expect(await db.query.labelTable.findMany()).toHaveLength(25);
  });

  it("does not chase comments added after the initial count or issues after the import boundary", async () => {
    const { project } = await setup();
    mocks.graphql.mockImplementation(async (query: string) => {
      if (query.includes("query ImportIssues("))
        return issuePage([
          issue(1, {
            comments: connection(
              Array.from({ length: 20 }, (_, n) => comment(n)),
              true,
              "20",
              21,
            ),
          }),
        ]);
      if (query.includes("query ImportIssueComments("))
        return {
          repository: {
            databaseId: 2,
            issue: {
              comments: connection(
                Array.from({ length: 20 }, (_, n) => comment(20 + n)),
                true,
                "40",
                99999,
              ),
            },
          },
        };
      return emptyPulls();
    });
    expect(await importIssues(project.id)).toMatchObject({
      pending: false,
      imported: 1,
    });
    expect(await db.query.activityTable.findMany()).toHaveLength(21);
    mocks.graphql.mockImplementation(async (query: string) =>
      query.includes("query ImportIssues(")
        ? issuePage(
            [issue(2, { createdAt: "2099-01-01T00:00:00Z" })],
            true,
            "2",
            999,
          )
        : emptyPulls(),
    );
    expect(await importIssues(project.id)).toMatchObject({
      pending: false,
      imported: 0,
    });
    expect(await db.query.taskTable.findMany()).toHaveLength(1);
  });

  it("rolls back task, link, comments, number allocation and cursor together when saving progress fails", async () => {
    const { project } = await setup();
    serveIssues(1);
    await db.execute(
      sql.raw(
        `CREATE FUNCTION fail_import_progress() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'test failure'; END $$`,
      ),
    );
    await db.execute(
      sql.raw(
        "CREATE TRIGGER fail_import_progress BEFORE UPDATE ON github_import FOR EACH ROW EXECUTE FUNCTION fail_import_progress()",
      ),
    );
    try {
      await expect(importIssues(project.id)).rejects.toThrow();
      expect(await db.query.taskTable.findMany()).toHaveLength(0);
      expect(await db.query.externalLinkTable.findMany()).toHaveLength(0);
      expect((await saved())?.state).toMatchObject({
        phase: "issues",
        issueCursor: null,
        imported: 0,
      });
      expect(await db.query.projectTable.findFirst()).toMatchObject({
        lastTaskNumber: 0,
      });
    } finally {
      await db.execute(
        sql.raw("DROP TRIGGER fail_import_progress ON github_import"),
      );
      await db.execute(sql.raw("DROP FUNCTION fail_import_progress()"));
    }
    expect(await importIssues(project.id)).toMatchObject({
      imported: 1,
      pending: false,
    });
    expect(await db.query.taskTable.findFirst()).toMatchObject({ number: 1 });
  });

  it("rechecks workspace access on every continuation and keeps state scoped to its integration", async () => {
    const { request, project, app } = await setup();
    serveIssues(7);
    const first = await (await request()).json();
    const calls = mocks.graphql.mock.calls.length;
    const other = await createWorkspaceMember({ role: "admin" });
    mockAuthenticatedSession(other.user);
    expect((await request(first.runId)).status).toBe(404);
    expect(
      (await app.request(`/api/github-integration/project/${project.id}`))
        .status,
    ).toBe(404);
    expect(mocks.graphql).toHaveBeenCalledTimes(calls);
  });

  it("rejects wrong run IDs and changed repository identities without consuming old cursors", async () => {
    const { project, integration, config } = await setup();
    serveIssues(7);
    const first = await importIssues(project.id);
    await expect(
      importIssues(project.id, "not-this-run"),
    ).rejects.toMatchObject({ status: 409 });
    await db
      .update(schema.integrationTable)
      .set({ config: JSON.stringify({ ...config, repositoryId: 22 }) })
      .where(eq(schema.integrationTable.id, integration.id));
    await expect(importIssues(project.id, first.runId)).rejects.toMatchObject({
      status: 409,
    });
    expect(mocks.graphql).toHaveBeenCalledTimes(4);
  });

  it("does not commit a fetched page after the integration is disabled", async () => {
    const { project, integration } = await setup();
    mocks.graphql.mockImplementation(async () => {
      await db
        .update(schema.integrationTable)
        .set({ isActive: false })
        .where(eq(schema.integrationTable.id, integration.id));
      return issuePage([issue(1)]);
    });
    await expect(importIssues(project.id)).rejects.toMatchObject({
      status: 409,
    });
    expect(await db.query.taskTable.findMany()).toHaveLength(0);
  });

  it.each(["identity", "cursor", "oversized"])(
    "rejects invalid provider %s without advancing saved progress",
    async (kind) => {
      const { project } = await setup();
      mocks.graphql.mockResolvedValue(
        kind === "identity"
          ? {
              ...issuePage([issue(1)]),
              repository: {
                ...issuePage([issue(1)]).repository,
                databaseId: 99,
              },
            }
          : kind === "cursor"
            ? issuePage([issue(1)], true, null)
            : issuePage([issue(1), issue(2)]),
      );
      await expect(importIssues(project.id)).rejects.toMatchObject({
        status: kind === "identity" ? 409 : 502,
      });
      expect((await saved())?.state.imported).toBe(0);
      expect(await db.query.taskTable.findMany()).toHaveLength(0);
    },
  );

  it("pauses on an empty unfinished page instead of silently dropping remaining issues", async () => {
    const { project } = await setup();
    mocks.graphql.mockResolvedValue(issuePage([], true, "next", 10));
    await expect(importIssues(project.id)).rejects.toMatchObject({
      status: 502,
    });
    expect((await saved())?.state).toMatchObject({
      imported: 0,
      issueCursor: null,
    });
  });

  it("does not loop on a repeated cursor and retains earlier pages", async () => {
    const { project } = await setup();
    mocks.graphql.mockResolvedValue(issuePage([issue(1)], true, "same", 100));
    await expect(importIssues(project.id)).rejects.toMatchObject({
      status: 502,
    });
    expect(mocks.graphql).toHaveBeenCalledTimes(2);
    expect((await saved())?.state).toMatchObject({
      imported: 1,
      issueCursor: "same",
    });
    expect(await db.query.taskTable.findMany()).toHaveLength(1);
  });

  it("finishes a disappeared source without losing other issues or modifying another project's task", async () => {
    const { project } = await setup();
    mocks.graphql.mockImplementation(
      async (query: string, vars: { cursor: string | null }) => {
        if (query.includes("query ImportIssues("))
          return vars.cursor
            ? issuePage([issue(2)])
            : issuePage(
                [
                  issue(1, {
                    comments: connection([comment(1)], true, "first", 100),
                  }),
                ],
                true,
                "1",
                2,
              );
        if (query.includes("query ImportIssueComments("))
          return { repository: { databaseId: 2, issue: null } };
        return emptyPulls();
      },
    );
    expect(await importIssues(project.id)).toMatchObject({
      pending: false,
      imported: 2,
      skipped: 1,
    });
    expect(await db.query.taskTable.findMany()).toHaveLength(2);
  });

  it("deleting an integration removes its saved progress but preserves imported tasks", async () => {
    const { project, integration } = await setup();
    serveIssues(8);
    await importIssues(project.id);
    await db
      .delete(schema.integrationTable)
      .where(eq(schema.integrationTable.id, integration.id));
    expect(await saved()).toBeUndefined();
    expect(await db.query.taskTable.findMany()).toHaveLength(4);
  });

  it("limits simultaneous imports across processes and releases slots after failure", async () => {
    const client = await getDatabasePool().connect();
    await client.query(
      "SELECT pg_advisory_lock(773623, 0), pg_advisory_lock(773623, 1)",
    );
    try {
      await expect(
        withGithubImportLock("any", async () => {}),
      ).rejects.toMatchObject({
        status: 429,
      });
    } finally {
      await client.query("SELECT pg_advisory_unlock_all()");
      client.release();
    }
    await expect(
      withGithubImportLock("any", async () => {
        throw new Error("failed");
      }),
    ).rejects.toThrow("failed");
    expect(await withGithubImportLock("any", async () => "free")).toBe("free");
  });

  it("rejects overlapping steps for one project and permits later continuation", async () => {
    const { project, request } = await setup();
    serveIssues(1);
    const gate = deferred();
    const started = deferred();
    const original = mocks.graphql.getMockImplementation();
    if (!original) throw new Error("Expected provider mock");
    mocks.graphql.mockImplementation(async (query, vars) => {
      started.resolve();
      await gate.promise;
      return original(query, vars);
    });
    const first = importIssues(project.id);
    await started.promise;
    try {
      const busy = await request();
      expect(busy.status).toBe(429);
      expect(busy.headers.get("Retry-After")).toBe("1");
    } finally {
      gate.resolve();
    }
    expect(await first).toMatchObject({ pending: false, imported: 1 });
  });

  it("coordinates webhook creation with imports so both paths create only one task and link", async () => {
    const { project } = await setup();
    serveIssues(1);
    const payload = {
      action: "opened",
      installation: { id: 1 },
      repository: {
        id: 2,
        owner: { login: "example" },
        name: "repo",
        full_name: "example/repo",
      },
      issue: {
        number: 1,
        title: "Issue 1",
        body: "Description",
        html_url: "https://github.com/example/repo/issues/1",
        user: null,
      },
    };
    await Promise.all([
      importIssues(project.id),
      handleIssueOpened(payload),
      handleIssueOpened(payload),
    ]);
    expect(await db.query.taskTable.findMany()).toHaveLength(1);
    expect(await db.query.externalLinkTable.findMany()).toHaveLength(1);
    expect(mocks.comment).not.toHaveBeenCalled();
  });

  it("announces a webhook task only after its task and link commit", async () => {
    const { project } = await setup();
    mocks.publish.mockImplementation(async (name, payload) => {
      expect(name).toBe("task.created");
      expect(payload).toMatchObject({
        projectId: project.id,
        title: "Live issue",
        description: "Description",
        priority: "low",
        status: "to-do",
        number: 1,
        userId: "",
        source: "github",
        externalId: "77",
        actor: "github-webhook",
      });
      const task = await db.query.taskTable.findFirst({
        where: eq(schema.taskTable.id, payload.taskId!),
      });
      const link = await db.query.externalLinkTable.findFirst({
        where: eq(schema.externalLinkTable.taskId, payload.taskId!),
      });
      expect(task?.title).toBe("Live issue");
      expect(link?.externalId).toBe("77");
    });
    await handleIssueOpened({
      action: "opened",
      installation: { id: 1 },
      repository: {
        id: 2,
        owner: { login: "example" },
        name: "repo",
        full_name: "example/repo",
      },
      issue: {
        number: 77,
        title: "Live issue",
        body: "Description",
        html_url: "https://github.com/example/repo/issues/77",
        user: null,
      },
    });
    expect(mocks.publish).toHaveBeenCalledTimes(1);
  });

  it.each(["excluded", "paused"])(
    "imports pull request links for tasks with %s issue sync",
    async (scope) => {
      const { project, integration, config } = await setup();
      const [task] = await db
        .insert(schema.taskTable)
        .values({
          projectId: project.id,
          number: 1,
          title: "Existing task",
          status: "to-do",
        })
        .returning();
      if (scope === "excluded")
        await db
          .update(schema.integrationTable)
          .set({
            config: JSON.stringify({
              ...config,
              syncRules: {
                outgoing: { mode: "labels", match: "any", labels: ["missing"] },
                incoming: { mode: "all" },
              },
            }),
          })
          .where(eq(schema.integrationTable.id, integration.id));
      else
        await db.insert(schema.externalLinkTable).values({
          taskId: task!.id,
          integrationId: integration.id,
          resourceType: "issue",
          externalId: "1",
          url: "https://github.com/example/repo/issues/1",
          metadata: JSON.stringify({ syncFilterPaused: true }),
        });
      mocks.graphql.mockImplementation(async (query: string) => {
        if (query.includes("query ImportIssues(")) return issuePage([]);
        return {
          repository: {
            databaseId: 2,
            pullRequests: connection([
              {
                number: 5,
                title: `${project.slug.toUpperCase()}-1`,
                body: null,
                url: "https://github.com/example/repo/pull/5",
                state: "OPEN",
                createdAt: old,
                headRefName: `${project.slug.toLowerCase()}-1`,
                author: null,
              },
            ]),
          },
        };
      });
      expect((await importIssues(project.id)).pending).toBe(false);
      expect(
        await db.query.externalLinkTable.findFirst({
          where: eq(schema.externalLinkTable.resourceType, "pull_request"),
        }),
      ).toMatchObject({
        taskId: task!.id,
        integrationId: integration.id,
        externalId: "5",
      });
    },
  );

  it("continues all pull request pages and links only matching tasks in this project", async () => {
    const { project, integration } = await setup();
    mocks.graphql.mockImplementation(
      async (query: string, vars: { cursor: string | null }) => {
        if (query.includes("query ImportIssues(")) return issuePage([issue(1)]);
        const offset = Number(vars.cursor ?? 0);
        const end = Math.min(25, offset + 10);
        const pulls = Array.from({ length: end - offset }, (_, index) => ({
          number: offset + index + 1,
          title: `${project.slug.toUpperCase()}-1`,
          body: null,
          url: `https://github.com/example/repo/pull/${offset + index + 1}`,
          state: "OPEN",
          createdAt: old,
          headRefName: `${project.slug.toLowerCase()}-1`,
          author: null,
        }));
        return {
          repository: {
            databaseId: 2,
            pullRequests: connection(pulls, end < 25, String(end), 25),
          },
        };
      },
    );
    const result = await importIssues(project.id);
    expect(result.pending).toBe(false);
    expect(
      await db
        .select()
        .from(schema.externalLinkTable)
        .where(
          and(
            eq(schema.externalLinkTable.resourceType, "pull_request"),
            eq(schema.externalLinkTable.integrationId, integration.id),
          ),
        ),
    ).toHaveLength(25);
  });
});

it.each(["labels", "comments"])(
  "skips unlinked GitHub %s continuation data after a task moves away and back",
  async (phase) => {
    const { project, member } = await setup();
    const { project: destination } = await createProjectFixture({
      workspaceId: project.workspaceId,
    });
    let movedTaskId: string | undefined;
    mocks.graphql.mockImplementation(async (query: string) => {
      if (query.includes("query ImportIssues("))
        return issuePage([
          issue(
            1,
            phase === "labels"
              ? { labels: connection([label(1)], true, "first", 2) }
              : { comments: connection([comment(1)], true, "first", 2) },
          ),
        ]);
      if (
        query.includes(
          phase === "labels"
            ? "query ImportIssueLabels("
            : "query ImportIssueComments(",
        )
      ) {
        const task = await db.query.taskTable.findFirst({
          where: eq(schema.taskTable.projectId, project.id),
        });
        movedTaskId = task!.id;
        await moveTask({
          taskId: task!.id,
          destinationProjectId: destination.id,
          currentUserId: member.user.id,
        });
        await moveTask({
          taskId: task!.id,
          destinationProjectId: project.id,
          currentUserId: member.user.id,
        });
        return {
          repository: {
            databaseId: 2,
            issue:
              phase === "labels"
                ? {
                    labels: connection([
                      { name: "status:in-progress", color: "ffffff" },
                      label(2),
                    ]),
                  }
                : { comments: connection([comment(2)]) },
          },
        };
      }
      return emptyPulls();
    });
    expect(await importIssues(project.id)).toMatchObject({
      pending: false,
      skipped: 1,
    });
    const saved = await db.query.taskTable.findFirst({
      where: eq(schema.taskTable.id, movedTaskId!),
    });
    expect(saved?.status).toBe("to-do");
    expect(
      await db.query.externalLinkTable.findMany({
        where: eq(schema.externalLinkTable.taskId, movedTaskId!),
      }),
    ).toEqual([]);
    const labels = await db.query.labelTable.findMany({
      where: eq(schema.labelTable.taskId, movedTaskId!),
    });
    expect(labels.some((row) => row.name === "label-2")).toBe(false);
    const comments = await db.query.activityTable.findMany({
      where: eq(schema.activityTable.taskId, movedTaskId!),
    });
    expect(comments.some((row) => row.externalUrl === comment(2).url)).toBe(
      false,
    );
  },
);