Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
32.5 KB
Raw Blame
import { createDenTypeId } from "@omnirush-ee/utils/typeid"
import { generateConnectLinkKeyPair, verifyConnectLinkToken } from "@omnirush/connect-link/node"
import { afterAll, beforeAll, beforeEach, expect, mock, test } from "bun:test"
import { Hono } from "hono"
import { mkdtempSync, writeFileSync } from "node:fs"
import os from "node:os"
import path from "node:path"

type InstallExperienceDependencies = import("../src/routes/org/install-links.js").InstallExperienceDependencies

function seedRequiredEnv() {
  process.env.DATABASE_URL = process.env.DATABASE_URL ?? "mysql://root:[email protected]:3306/omnirush_test"
  process.env.DEN_DB_ENCRYPTION_KEY = process.env.DEN_DB_ENCRYPTION_KEY ?? "x".repeat(32)
  process.env.BETTER_AUTH_SECRET = process.env.BETTER_AUTH_SECRET ?? "y".repeat(32)
  process.env.BETTER_AUTH_URL = process.env.BETTER_AUTH_URL ?? "http://127.0.0.1:8790"
  process.env.DEN_INSTALL_LINKS_GATING_ENABLED = "true"
}

const userId = createDenTypeId("user")
const memberId = createDenTypeId("member")
const organizationId = createDenTypeId("organization")
const installLinkId = createDenTypeId("installLink")
const insertedRows: unknown[] = []
const revokedRows: unknown[] = []
const officialWindowsDesktopUrl = "https://github.com/omnirush-ai/omnirush-gui/releases/download/v9.9.9/omnirush-enterprise-win-x64-9.9.9.exe"
const officialWindowsCloudDesktopUrl = "https://github.com/omnirush-ai/omnirush-gui/releases/download/v9.9.9/omnirush-cloud-win-x64-9.9.9.exe"
const connectKeyPair = generateConnectLinkKeyPair()
const connectKeyId = "owc-route-test"

function defaultOrganizationMetadata(): Record<string, unknown> {
  return {
    brandAppName: "Acme Work",
    brandLogoUrl: "https://assets.northwind.test/wordmark.svg",
    brandIconUrl: "https://assets.northwind.test/icon.png",
  }
}

let role = "member"
let isOwner = false
let installLinksCapabilityOverride: boolean | null = null
let failInstallLinkInsert = false
let sessionCreatedAt = new Date()
let organizationMetadata = defaultOrganizationMetadata()

mock.module("../src/auth.js", () => ({
  DEN_MCP_FIRST_PARTY_CLIENT_ID: "omnirush-desktop",
  DEN_MCP_FIRST_PARTY_RESOURCES: [],
  DEN_MCP_LEGACY_PARENT_RESOURCES: [],
  DEN_MCP_OAUTH_RESOURCE: "http://127.0.0.1:8790/mcp/agent",
  DEN_MCP_OAUTH_VALID_AUDIENCES: [],
  DEN_MCP_OPAQUE_ACCESS_TOKEN_PREFIX: "ow_mcp_at_",
  DEN_MCP_ORG_ID_CLAIM: "org_id",
  DEN_MCP_RESOURCE: "http://127.0.0.1:8790/mcp",
  DEN_MCP_RESOURCE_CLAIM: "resource",
  DEN_MCP_RESOURCES: [],
  DEN_MCP_TOKEN_USE_CLAIM: "token_use",
  auth: {
    api: {
      setActiveOrganization: () => Promise.resolve(),
    },
  },
  getRawBetterAuthMutationDenial: () => null,
  normalizeMcpOAuthResource: () => null,
}))

mock.module("../src/db.js", () => ({
  db: {
    insert: (_table: unknown) => ({
      values: (values: unknown) => {
        if (failInstallLinkInsert && isRecord(values) && typeof values.tokenHash === "string") {
          return Promise.reject(new Error("install link storage unavailable"))
        }
        insertedRows.push(values)
        return Promise.resolve()
      },
    }),
    select: (selection: unknown) => {
      const rows = isRecord(selection) && "installLink" in selection && "organization" in selection
        ? [{
            installLink: { id: installLinkId, organizationId },
            organization: {
              id: organizationId,
              name: "Acme Robotics",
              slug: "acme-robotics",
              logo: null,
              metadata: organizationMetadata,
            },
          }]
        : []
      const where = (_condition: unknown) => ({
        limit: (_count: number) => Promise.resolve(rows),
      })
      return {
        from: (_table: unknown) => ({
          where,
          innerJoin: (_joinedTable: unknown, _condition: unknown) => ({ where }),
        }),
      }
    },
    update: (_table: unknown) => ({
      set: (values: unknown) => ({
        where: (_condition: unknown) => {
          revokedRows.push(values)
          return Promise.resolve()
        },
      }),
    }),
    delete: (_table: unknown) => ({
      where: (_condition: unknown) => Promise.resolve(),
    }),
  },
}))

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null
}

function insertedInstallLinks() {
  return insertedRows.filter((row) => isRecord(row) && typeof row.tokenHash === "string")
}

function organizationContextMetadata() {
  if (installLinksCapabilityOverride === null) {
    return organizationMetadata
  }

  const capabilities = isRecord(organizationMetadata.capabilities) ? organizationMetadata.capabilities : {}
  return {
    ...organizationMetadata,
    capabilities: { ...capabilities, installLinks: installLinksCapabilityOverride },
  }
}

mock.module("../src/orgs.js", () => ({
  OrganizationEmailDomainRestrictionError: class OrganizationEmailDomainRestrictionError extends Error {},
  acceptInvitationForUser: () => Promise.resolve(null),
  createOrganizationForUser: () => Promise.resolve(null),
  getOrganizationContextForUser: (input: { organizationId: string; userId: string }) => Promise.resolve(
    input.organizationId === organizationId && input.userId === userId
      ? {
          organization: {
            id: organizationId,
            name: "Acme Robotics",
            slug: "acme-robotics",
            logo: null,
            metadata: organizationContextMetadata(),
          },
          currentMember: {
            id: memberId,
            userId,
            role,
            isOwner,
            createdAt: new Date(),
          },
          members: [],
          invitations: [],
          roles: [],
          teams: [],
          currentMemberTeams: [],
        }
      : null,
  ),
  getInvitationPreview: () => Promise.resolve(null),
  getSingletonOrganization: () => Promise.resolve(null),
  getSingletonSsoStatus: () => Promise.resolve(null),
  listTeamsForMember: () => Promise.resolve([]),
  ensureUserOrgAccess: () => Promise.resolve(organizationId),
  isEmailAllowedForOrganization: () => true,
  listAssignableRoles: () => Promise.resolve([]),
  normalizeAllowedEmailDomains: () => null,
  reconcilePendingInvitationsForUser: () => Promise.resolve(),
  removeOrganizationMember: () => Promise.resolve(null),
  resolveUserOrganizations: (input: { activeOrganizationId: string | null; userId: string }) => {
    const hasActiveMembership = input.userId === userId && input.activeOrganizationId === organizationId
    return Promise.resolve(hasActiveMembership
      ? {
          orgs: [{ id: organizationId, name: "Acme Robotics", slug: "acme-robotics", logo: null }],
          activeOrgId: organizationId,
          activeOrgSlug: "acme-robotics",
        }
      : { orgs: [], activeOrgId: null, activeOrgSlug: null })
  },
  seedDefaultOrganizationRoles: () => Promise.resolve(),
  serializeMemberFacingOrganizationMetadata: (metadata: unknown) => metadata,
  setSessionActiveOrganization: () => Promise.resolve(),
  updateOrganizationSettings: () => Promise.resolve(null),
  validateOrganizationMemberRemovalForHook: () => Promise.resolve({ ok: true }),
  validateOrganizationMemberRoleUpdate: () => Promise.resolve({ ok: true }),
}))

let installLinkModule: typeof import("../src/routes/org/install-links.js")
let installLinkMintingModule: typeof import("../src/install-links.js")
let envModule: typeof import("../src/env.js")

beforeAll(async () => {
  seedRequiredEnv()
  envModule = await import("../src/env.js")
  installLinkMintingModule = await import("../src/install-links.js")
  installLinkModule = await import("../src/routes/org/install-links.js")
  mock.restore()
})

beforeEach(() => {
  envModule.env.installLinksGatingEnabled = true
  envModule.env.connectLink = null
  envModule.env.devMode = true
  envModule.env.installerArtifactsDir = undefined
  envModule.env.installerReleaseRepo = "omnirush-ai/omnirush-gui"
  envModule.env.installerReleaseTag = "v9.9.9"
  envModule.env.installerReleaseTagExplicit = true
  envModule.env.orgMode = "single_org"
  insertedRows.length = 0
  revokedRows.length = 0
  role = "member"
  isOwner = false
  installLinksCapabilityOverride = null
  failInstallLinkInsert = false
  sessionCreatedAt = new Date()
  organizationMetadata = defaultOrganizationMetadata()
})

afterAll(() => {
  mock.restore()
})

function createApp(options: {
  configuredArtifact?: { filePath: string; size: number }
  artifactFileNames?: string[]
  grantOverrides?: Partial<Pick<InstallExperienceDependencies, "mintConnectGrant" | "previewConnectGrant" | "inspectConnectGrant" | "consumeConnectGrant">>
  authenticated?: boolean
  activeOrganizationId?: string
} = {}) {
  const app = new Hono()
  app.use("*", async (c, next) => {
    if (options.authenticated !== false) {
      c.set("user", {
        id: userId,
        email: "[email protected]",
        emailVerified: true,
        name: "Riley",
        image: null,
        createdAt: new Date(),
        updatedAt: new Date(),
      })
      c.set("session", {
        id: createDenTypeId("session"),
        activeOrganizationId: options.activeOrganizationId ?? organizationId,
        createdAt: sessionCreatedAt,
      })
    }
    await next()
  })
  const shouldResolveConfiguredArtifact = options.configuredArtifact !== undefined || options.artifactFileNames !== undefined
  const overrides: Partial<InstallExperienceDependencies> = {
    ...(shouldResolveConfiguredArtifact
      ? {
          resolveConfiguredArtifact: (fileName: string) => {
            options.artifactFileNames?.push(fileName)
            return Promise.resolve(options.configuredArtifact ?? null)
          },
        }
      : {}),
    ...options.grantOverrides,
  }
  installLinkModule.registerOrgInstallLinkRoutes(app, overrides)
  return app
}

function mint(app: Hono, input: { rotate?: boolean } = {}) {
  return app.request(`http://den.local/v1/orgs/${organizationId}/install-links`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify(input),
  })
}

test("members can mint non-rotating install links without revoking earlier links", async () => {
  const app = createApp()
  const first = await mint(app)
  const second = await mint(app, { rotate: false })

  expect(first.status).toBe(200)
  expect(second.status).toBe(200)
  expect(insertedInstallLinks()).toHaveLength(2)
  expect(revokedRows).toHaveLength(0)
})

test("ordinary members cannot rotate organization install links", async () => {
  const response = await mint(createApp(), { rotate: true })

  expect(response.status).toBe(403)
  await expect(response.json()).resolves.toMatchObject({
    error: "forbidden",
    message: "Only workspace owners and admins can rotate install links.",
  })
  expect(insertedInstallLinks()).toHaveLength(0)
  expect(revokedRows).toHaveLength(0)
})

test("admins with a fresh session can explicitly rotate install links", async () => {
  role = "admin"
  const response = await mint(createApp(), { rotate: true })

  expect(response.status).toBe(200)
  expect(revokedRows).toHaveLength(1)
  expect(insertedInstallLinks()).toHaveLength(1)
})

test("explicit rotation still requires a fresh privileged session", async () => {
  role = "admin"
  sessionCreatedAt = new Date(Date.now() - 16 * 60 * 1000)
  const response = await mint(createApp(), { rotate: true })

  expect(response.status).toBe(403)
  await expect(response.json()).resolves.toMatchObject({ error: "reauth", reason: "fresh_auth_required" })
  expect(insertedInstallLinks()).toHaveLength(0)
  expect(revokedRows).toHaveLength(0)
})

test("explicit installLinks false kill switch refuses member install links", async () => {
  installLinksCapabilityOverride = false
  const response = await mint(createApp())

  expect(response.status).toBe(403)
  await expect(response.json()).resolves.toEqual({ error: "capability_disabled", capability: "installLinks" })
  expect(insertedInstallLinks()).toHaveLength(0)
})

test("deprecated deployment gate stays inert when installLinks metadata is absent", async () => {
  envModule.env.installLinksGatingEnabled = true
  organizationMetadata = defaultOrganizationMetadata()
  installLinksCapabilityOverride = null
  const response = await mint(createApp())

  expect(response.status).toBe(200)
  expect(insertedInstallLinks()).toHaveLength(1)
})

test("invitation downloads mint the same org install page without storing the raw token", async () => {
  const downloadUrl = await installLinkMintingModule.resolveInvitationDownloadUrl({
    organizationId,
    createdByUserId: userId,
    metadata: { capabilities: { installLinks: true } },
  })

  const url = new URL(downloadUrl)
  const token = url.searchParams.get("token")
  const rows = insertedInstallLinks()

  expect(url.pathname).toBe("/install")
  expect(url.origin).toBe(new URL(process.env.BETTER_AUTH_URL ?? "http://127.0.0.1:8790").origin)
  expect(token).toBeTruthy()
  expect(rows).toHaveLength(1)
  expect(rows[0]).not.toHaveProperty("token")
  expect(rows[0]).not.toHaveProperty("installPageUrl")
  expect(isRecord(rows[0]) ? rows[0].tokenHash : null).toBe(installLinkMintingModule.hashInstallLinkToken(token ?? ""))
  expect(revokedRows).toHaveLength(0)
})

test("invitation downloads keep the generic URL when install links are disabled", async () => {
  const downloadUrl = await installLinkMintingModule.resolveInvitationDownloadUrl({
    organizationId,
    createdByUserId: userId,
    metadata: { capabilities: { installLinks: false } },
  })

  expect(downloadUrl).toBe("https://github.com/omnirush-ai/omnirush-gui/releases/latest")
  expect(insertedInstallLinks()).toHaveLength(0)
})

test("invitation delivery can fall back when install-link storage fails", async () => {
  failInstallLinkInsert = true

  const downloadUrl = await installLinkMintingModule.resolveInvitationDownloadUrl({
    organizationId,
    createdByUserId: userId,
    metadata: { capabilities: { installLinks: true } },
  })

  expect(downloadUrl).toBe("https://github.com/omnirush-ai/omnirush-gui/releases/latest")
  expect(insertedInstallLinks()).toHaveLength(0)
})

test("members cannot mint an install link for another organization", async () => {
  const otherOrganizationId = createDenTypeId("organization")
  const response = await createApp().request(`http://den.local/v1/orgs/${otherOrganizationId}/install-links`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ rotate: false }),
  })

  expect(response.status).toBe(404)
  await expect(response.json()).resolves.toEqual({ error: "organization_not_found" })
  expect(insertedInstallLinks()).toHaveLength(0)
})

test("authenticated install config resolves branding, distribution, and approved version from the active organization without minting a token", async () => {
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: ["0.18.4", "0.18.6", "0.18.5"],
  }

  const response = await createApp().request("http://127.0.0.1:8790/v1/me/install-config")

  expect(response.status).toBe(200)
  await expect(response.json()).resolves.toMatchObject({
    appName: "Acme Work",
    clientName: "Acme Robotics",
    logoUrl: "https://assets.northwind.test/wordmark.svg",
    iconUrl: "https://assets.northwind.test/icon.png",
    desktopVersion: "0.18.6",
    distribution: "enterprise",
  })
  expect(insertedInstallLinks()).toHaveLength(0)
})

test("authenticated install config requires an active organization membership", async () => {
  const otherOrganizationId = createDenTypeId("organization")
  const unauthenticated = await createApp({ authenticated: false }).request("http://den.local/v1/me/install-config")
  const nonMember = await createApp({ activeOrganizationId: otherOrganizationId }).request("http://den.local/v1/me/install-config")

  expect(unauthenticated.status).toBe(401)
  expect(nonMember.status).toBe(404)
  expect(insertedInstallLinks()).toHaveLength(0)
})

test("authenticated installer download uses active-organization version and distribution without a token", async () => {
  envModule.env.orgMode = "multi_org"
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: ["0.18.4", "0.18.6"],
  }

  const response = await createApp().request("http://den.local/v1/me/install/win-x64", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe(
    "https://github.com/omnirush-ai/omnirush-gui/releases/download/v0.18.6/omnirush-cloud-win-x64-0.18.6.exe",
  )
  expect(response.headers.get("location")).not.toContain("token=")
  expect(insertedInstallLinks()).toHaveLength(0)
})

test("zero-config downloads redirect the browser to the enterprise desktop release", async () => {
  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe(officialWindowsDesktopUrl)
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test("Cloud downloads resolve the matching version without forwarding the install token", async () => {
  envModule.env.orgMode = "multi_org"
  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe(officialWindowsCloudDesktopUrl)
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test("mounted artifact lookup uses the resolved enterprise desktop filename", async () => {
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: ["0.17.26", "0.17.27"],
  }
  const artifactFileNames: string[] = []
  const installer = Buffer.from("signed-enterprise-windows-installer", "utf8")
  const artifactPath = path.join(mkdtempSync(path.join(os.tmpdir(), "omnirush-install-route-")), "installer.exe")
  writeFileSync(artifactPath, installer)

  const response = await createApp({
    artifactFileNames,
    configuredArtifact: { filePath: artifactPath, size: installer.byteLength },
  }).request("http://den.local/v1/install/win-x64?token=opaque-token")

  expect(response.status).toBe(200)
  expect(artifactFileNames).toEqual(["omnirush-enterprise-win-x64-0.17.27.exe"])
  expect(response.headers.get("content-type")).toBe("application/vnd.microsoft.portable-executable")
  expect(response.headers.get("content-disposition")).toContain("omnirush-enterprise-win-x64-0.17.27.exe")
  expect(response.headers.get("content-disposition")).not.toContain("opaque-token")
  expect(Buffer.from(await response.arrayBuffer())).toEqual(installer)
})

test("hosted Cloud mounted artifact lookup uses the resolved Cloud desktop filename", async () => {
  envModule.env.orgMode = "multi_org"
  const artifactFileNames: string[] = []
  const installer = Buffer.from("signed-cloud-mac-installer", "utf8")
  const artifactPath = path.join(mkdtempSync(path.join(os.tmpdir(), "omnirush-install-route-")), "installer.dmg")
  writeFileSync(artifactPath, installer)

  const response = await createApp({
    artifactFileNames,
    configuredArtifact: { filePath: artifactPath, size: installer.byteLength },
  }).request("http://den.local/v1/install/mac-arm64?token=opaque-token")

  expect(response.status).toBe(200)
  expect(artifactFileNames).toEqual(["omnirush-cloud-mac-arm64-9.9.9.dmg"])
  expect(response.headers.get("content-type")).toBe("application/x-apple-diskimage")
  expect(response.headers.get("content-disposition")).toContain("omnirush-cloud-mac-arm64-9.9.9.dmg")
  expect(response.headers.get("content-disposition")).not.toContain("opaque-token")
  expect(Buffer.from(await response.arrayBuffer())).toEqual(installer)
})

test("unordered organization allowed desktop versions select the maximum direct release URL", async () => {
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: ["0.18.5", "0.18.4", "0.18.6"],
  }

  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe("https://github.com/omnirush-ai/omnirush-gui/releases/download/v0.18.6/omnirush-enterprise-win-x64-0.18.6.exe")
  expect(response.headers.get("location")).not.toContain("v9.9.9")
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test("older allowed desktop versions keep their matching release tag", async () => {
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: ["0.17.26", "0.17.25", "0.17.27"],
  }

  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe("https://github.com/omnirush-ai/omnirush-gui/releases/download/v0.17.27/omnirush-enterprise-win-x64-0.17.27.exe")
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test("unrestricted organizations use Den's configured desktop release tag", async () => {
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: [],
  }

  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe(officialWindowsDesktopUrl)
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test("unrestricted organizations use Den's release version when the tag was not set explicitly", async () => {
  envModule.env.installerReleaseTagExplicit = false
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: [],
  }

  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe(officialWindowsDesktopUrl)
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test("organization version pins stay tag-pinned even without an explicit installer tag", async () => {
  envModule.env.installerReleaseTagExplicit = false
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: ["0.18.4", "0.18.6"],
  }

  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe("https://github.com/omnirush-ai/omnirush-gui/releases/download/v0.18.6/omnirush-enterprise-win-x64-0.18.6.exe")
  expect(response.headers.get("location")).not.toContain("/releases/latest/")
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test("custom release repos never use the latest-release URL", async () => {
  envModule.env.installerReleaseTagExplicit = false
  envModule.env.installerReleaseRepo = "acme/omnirush"
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: [],
  }

  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe("https://github.com/acme/omnirush/releases/download/v9.9.9/omnirush-enterprise-win-x64-9.9.9.exe")
  expect(response.headers.get("location")).not.toContain("/releases/latest/")
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test("install token organization policy applies to member and admin downloads", async () => {
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: ["0.18.4", "0.18.6"],
  }
  const expectedUrl = "https://github.com/omnirush-ai/omnirush-gui/releases/download/v0.18.6/omnirush-enterprise-win-x64-0.18.6.exe"

  for (const nextRole of ["member", "admin"]) {
    role = nextRole
    const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
      redirect: "manual",
    })

    expect(response.status).toBe(302)
    expect(response.headers.get("location")).toBe(expectedUrl)
  }
})

test("explicit configured desktop release tags are used verbatim", async () => {
  envModule.env.installerReleaseTagExplicit = true
  envModule.env.installerReleaseTag = "v0.17.27"
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: [],
  }

  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe("https://github.com/omnirush-ai/omnirush-gui/releases/download/v0.17.27/omnirush-enterprise-win-x64-0.17.27.exe")
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test("custom desktop release repos use the organization-approved version", async () => {
  envModule.env.installerReleaseRepo = "acme/omnirush"
  organizationMetadata = {
    ...defaultOrganizationMetadata(),
    allowedDesktopVersions: ["0.17.26", "0.17.27"],
  }

  const response = await createApp().request("http://den.local/v1/install/win-x64?token=opaque-token", {
    redirect: "manual",
  })

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe("https://github.com/acme/omnirush/releases/download/v0.17.27/omnirush-enterprise-win-x64-0.17.27.exe")
  expect(response.headers.get("location")).not.toContain("opaque-token")
})

test.each([
  { platform: "mac-arm64", assetName: "omnirush-enterprise-mac-arm64-9.9.9.dmg" },
  { platform: "mac-x64", assetName: "omnirush-enterprise-mac-x64-9.9.9.dmg" },
  { platform: "win-x64", assetName: "omnirush-enterprise-win-x64-9.9.9.exe" },
  { platform: "linux-x64", assetName: "omnirush-enterprise-linux-x86_64-9.9.9.AppImage" },
  { platform: "linux-arm64", assetName: "omnirush-enterprise-linux-arm64-9.9.9.AppImage" },
])(
  "zero-config $platform downloads redirect to the enterprise desktop asset without forwarding the token",
  async ({ platform, assetName }) => {
    const directUrl = `https://github.com/omnirush-ai/omnirush-gui/releases/download/v9.9.9/${assetName}`
    const response = await createApp().request(
      `http://den.local/v1/install/${platform}?token=opaque-token`,
      { redirect: "manual" },
    )

    expect(response.status).toBe(302)
    expect(response.headers.get("location")).toBe(directUrl)
    expect(response.headers.get("location")).not.toContain("opaque-token")
  },
)

test("zero-config install config mints a short-lived exchange without storing the raw code", async () => {
  const response = await createApp().request("http://127.0.0.1:8790/v1/install-config?token=opaque-token")

  expect(response.status).toBe(200)
  const body = await response.json()
  expect(body.connectUrl).toStartWith("omnirush://connect?code=")
  expect(body.activationUrl).toStartWith("http://127.0.0.1:8790/activate?code=")
  expect(body.requireSignin).toBe(true)
  expect(body.desktopVersion).toBe("9.9.9")
  expect(body.distribution).toBe("enterprise")
  expect(Date.parse(body.connectExpiresAt)).toBeGreaterThan(Date.now())
  expect(body.activationExpiresAt).toBe(body.connectExpiresAt)

  const url = new URL(body.connectUrl)
  const code = url.searchParams.get("code") ?? ""
  expect(new URL(body.activationUrl).searchParams.get("code")).toBe(code)
  expect(code.length).toBeGreaterThanOrEqual(24)
  expect(url.searchParams.get("apiBaseUrl")).toBe("http://127.0.0.1:8790")

  const grant = insertedRows.find((row) => isRecord(row) && typeof row.codeHash === "string")
  expect(grant).toMatchObject({
    installLinkId,
    consumedAt: null,
    consumedNonce: null,
    claims: {
      org: { name: "Acme Robotics" },
      brand: {
        appName: "Acme Work",
        logoUrl: "https://assets.northwind.test/wordmark.svg",
        iconUrl: "https://assets.northwind.test/icon.png",
      },
      requireSignin: true,
    },
  })
  expect(grant).not.toHaveProperty("code")
  expect(JSON.stringify(grant)).not.toContain(code)
})

test("hosted multi-org install config selects Cloud without enterprise activation", async () => {
  envModule.env.orgMode = "multi_org"
  const response = await createApp().request("http://127.0.0.1:8790/v1/install-config?token=opaque-token")

  expect(response.status).toBe(200)
  const body = await response.json()
  expect(body.requireSignin).toBe(true)
  expect(body.desktopVersion).toBe("9.9.9")
  expect(body.distribution).toBe("cloud")
})

test("keyless preview is read-only and exchange consumes the grant once", async () => {
  const code = "abcdefghijklmnopqrstuvwxyz123456"
  const claims = {
    iss: "http://127.0.0.1:8790",
    aud: "omnirush-desktop-connect",
    iat: Math.floor(Date.now() / 1000),
    exp: Math.floor(Date.now() / 1000) + 300,
    jti: "grant-jti-123456",
    v: 1,
    org: { name: "Acme Robotics" },
    brand: { appName: "OmniRush.ai", logoUrl: null, iconUrl: null },
    den: { baseUrl: "http://127.0.0.1:8790", apiBaseUrl: "http://127.0.0.1:8790" },
    requireSignin: true,
  }
  let consumed = false
  const app = createApp({
    grantOverrides: {
      previewConnectGrant: () => Promise.resolve(consumed
        ? { ok: false, code: "replayed" }
        : { ok: true, claims }),
      consumeConnectGrant: () => {
        if (consumed) return Promise.resolve({ ok: false, code: "replayed" })
        consumed = true
        return Promise.resolve({ ok: true, claims })
      },
      inspectConnectGrant: () => Promise.resolve({
        ok: true,
        status: consumed ? "connected" : "pending",
        claims,
        expiresAt: new Date((claims.exp + 1) * 1000),
      }),
    },
  })
  const request = (mode: "preview" | "exchange") => app.request(`http://den.local/v1/install-connect/${mode}`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ code }),
  })

  const firstPreview = await request("preview")
  expect(firstPreview.status).toBe(200)
  expect(consumed).toBe(false)
  const pending = await app.request("http://den.local/v1/install-connect/status", {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ code }),
  })
  expect(pending.status).toBe(200)
  await expect(pending.json()).resolves.toMatchObject({ status: "pending", claims })
  const exchange = await request("exchange")
  expect(exchange.status).toBe(200)
  expect(consumed).toBe(true)
  const connected = await app.request("http://den.local/v1/install-connect/status", {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ code }),
  })
  expect(connected.status).toBe(200)
  await expect(connected.json()).resolves.toMatchObject({ status: "connected", claims })
  const replay = await request("exchange")
  expect(replay.status).toBe(409)
  await expect(replay.json()).resolves.toEqual({ error: "connect_grant_replayed" })
})

test("signed handoffs use the same direct enterprise desktop route", async () => {
  envModule.env.connectLink = { privateKeyPem: "unused by download route", kid: "test" }
  const response = await createApp().request(
    "http://den.local/v1/install/win-x64?token=opaque-token",
    { redirect: "manual" },
  )

  expect(response.status).toBe(302)
  expect(response.headers.get("location")).toBe(officialWindowsDesktopUrl)
})

test("install config includes a fresh signed organization handoff while preserving normal sign-in", async () => {
  envModule.env.connectLink = { privateKeyPem: connectKeyPair.privateKeyPem, kid: connectKeyId }
  const response = await createApp().request("http://127.0.0.1:8790/v1/install-config?token=opaque-token")

  expect(response.status).toBe(200)
  const body = await response.json()
  expect(body.connectUrl).toStartWith("omnirush://connect?token=")
  expect(body.activationUrl).toStartWith("http://127.0.0.1:8790/activate?code=")
  expect(body.requireSignin).toBe(true)
  expect(body.desktopVersion).toBe("9.9.9")
  expect(body.distribution).toBe("enterprise")

  const token = new URL(body.connectUrl).searchParams.get("token") ?? ""
  const verified = verifyConnectLinkToken({
    token,
    publicKeys: { [connectKeyId]: connectKeyPair.publicKeyPem },
    allowInsecureLoopback: true,
  })
  expect(verified.ok).toBe(true)
  if (!verified.ok) throw new Error("expected install handoff token to verify")
  expect(verified.claims.org.name).toBe("Acme Robotics")
  expect(verified.claims.brand).toEqual({
    appName: "Acme Work",
    logoUrl: "https://assets.northwind.test/wordmark.svg",
    iconUrl: "https://assets.northwind.test/icon.png",
  })
  expect(verified.claims.den.baseUrl).toBe(process.env.BETTER_AUTH_URL)
  expect(verified.claims.den.apiBaseUrl).toBe("http://127.0.0.1:8790")
  expect(verified.claims.requireSignin).toBe(true)
})