name: "🔌 SDK Compatibility Tests"

permissions:
  contents: read

on:
  workflow_call:

jobs:
  node-compat:
    name: "Node.js ${{ matrix.node }} (${{ matrix.os }})"
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [warp-ubuntu-latest-x64-4x]
        node: ["20.20", "22.23", "24.18", "26.4"]

    steps:
      - name: ⬇️ Checkout repo
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: ⎔ Setup pnpm
        uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5.0.0
        with:
          version: 10.33.2

      - name: ⎔ Setup node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: ${{ matrix.node }}
          cache: "pnpm"

      - name: 📥 Download deps
        run: pnpm install --frozen-lockfile

      - name: 📀 Generate Prisma Client
        run: pnpm run generate

      - name: 🔨 Build SDK dependencies
        shell: bash
        run: pnpm run build --filter '@trigger.dev/sdk^...'

      - name: 🔨 Build SDK
        shell: bash
        run: pnpm run build --filter '@trigger.dev/sdk'

      - name: 🧪 Run SDK Compatibility Tests
        shell: bash
        run: pnpm --filter @internal/sdk-compat-tests test

  bun-compat:
    name: "Bun Runtime"
    runs-on: warp-ubuntu-latest-x64-4x
    steps:
      - name: ⬇️ Checkout repo
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: ⎔ Setup pnpm
        uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5.0.0
        with:
          version: 10.33.2

      - name: ⎔ Setup node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 22.23.1
          cache: "pnpm"

      - name: 🥟 Setup Bun
        uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
        with:
          bun-version: latest

      - name: 📥 Download deps
        run: pnpm install --frozen-lockfile

      - name: 📀 Generate Prisma Client
        run: pnpm run generate

      - name: 🔨 Build SDK dependencies
        run: pnpm run build --filter @trigger.dev/sdk^...

      - name: 🔨 Build SDK
        run: pnpm run build --filter @trigger.dev/sdk

      - name: 🧪 Run Bun Compatibility Test
        working-directory: internal-packages/sdk-compat-tests/src/fixtures/bun
        run: bun run test.ts

  deno-compat:
    name: "Deno Runtime"
    runs-on: warp-ubuntu-latest-x64-4x
    steps:
      - name: ⬇️ Checkout repo
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: ⎔ Setup pnpm
        uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5.0.0
        with:
          version: 10.33.2

      - name: ⎔ Setup node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 22.23.1
          cache: "pnpm"

      - name: 🦕 Setup Deno
        uses: denoland/setup-deno@667a34cdef165d8d2b2e98dde39547c9daac7282 # v2.0.4
        with:
          deno-version: v2.x

      - name: 📥 Download deps
        run: pnpm install --frozen-lockfile

      - name: 📀 Generate Prisma Client
        run: pnpm run generate

      - name: 🔨 Build SDK dependencies
        run: pnpm run build --filter @trigger.dev/sdk^...

      - name: 🔨 Build SDK
        run: pnpm run build --filter @trigger.dev/sdk

      - name: 🔗 Link node_modules for Deno fixture
        working-directory: internal-packages/sdk-compat-tests/src/fixtures/deno
        run: ln -s ../../../../../node_modules node_modules

      - name: 🧪 Run Deno Compatibility Test
        working-directory: internal-packages/sdk-compat-tests/src/fixtures/deno
        run: deno run --allow-read --allow-env --allow-sys test.ts

  cloudflare-compat:
    name: "Cloudflare Workers"
    runs-on: warp-ubuntu-latest-x64-4x
    steps:
      - name: ⬇️ Checkout repo
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: ⎔ Setup pnpm
        uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5.0.0
        with:
          version: 10.33.2

      - name: ⎔ Setup node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 22.23.1
          cache: "pnpm"

      - name: 📥 Download deps
        run: pnpm install --frozen-lockfile

      - name: 📀 Generate Prisma Client
        run: pnpm run generate

      - name: 🔨 Build SDK dependencies
        run: pnpm run build --filter @trigger.dev/sdk^...

      - name: 🔨 Build SDK
        run: pnpm run build --filter @trigger.dev/sdk

      - name: 📥 Install Cloudflare fixture deps
        working-directory: internal-packages/sdk-compat-tests/src/fixtures/cloudflare-worker
        run: pnpm install

      - name: 🧪 Run Cloudflare Workers Compatibility Test (dry-run)
        working-directory: internal-packages/sdk-compat-tests/src/fixtures/cloudflare-worker
        run: npx wrangler deploy --dry-run --outdir dist
