import { describe, expect, it } from 'vitest'
import { createReleaseManifestResponse } from '../release-manifest'

describe('createReleaseManifestResponse', () => {
  it('hides raw download URLs from anonymous viewers while keeping public release metadata visible', () => {
    const payload = createReleaseManifestResponse(
      {
        authenticated: false,
        canDownload: false,
        plan: null,
        role: null,
        accessStatus: null,
      },
      {
        RELEASE_MANIFEST_VERSION: '0.9.4',
        RELEASE_MANIFEST_CHANNEL: 'candidate',
        SUPPORT_EMAIL: 'ops@hypertwist.app',
        PUBLIC_DOCS_URL: 'https://docs.hypertwist.app',
        RELEASE_NOTES_URL: 'https://notes.hypertwist.app',
        MPL_SOURCE_URL: 'https://hypertwist.app/open-source/source.zip',
        OPEN_SOURCE_REPO_URL: 'https://git.scriptoriumai.io/scriptoriumadmin/hypertwist',
        VITE_PADDLE_CHECKOUT_URL_OPERATOR: 'https://buy.paddle.com/operator-live',
        VITE_PADDLE_CHECKOUT_URL_STUDIO: 'https://buy.paddle.com/studio-live',
        VITE_PLAN_PRICE_OPERATOR: '$19 / month',
        VITE_PLAN_PRICE_STUDIO: '$99 / month',
        WINDOWS_DOWNLOAD_URL: 'https://downloads.hypertwist.app/windows.exe',
        WINDOWS_RELEASE_BUILD_ID: 'win64-0094',
        WINDOWS_RELEASE_SHA256: 'abc123',
        WINDOWS_RELEASE_FILE_SIZE_BYTES: '1048576',
      },
    )

    expect(payload.manifest.viewer).toEqual({
      authenticated: false,
      canDownload: false,
      plan: null,
      role: null,
      accessStatus: null,
    })
    expect(payload.manifest.support_email).toBe('ops@hypertwist.app')
    expect(payload.manifest.public_docs_url).toBe('https://docs.hypertwist.app')
    expect(payload.manifest.release_notes_url).toBe('https://notes.hypertwist.app')
    expect(payload.manifest.commerce).toEqual({
      operator_checkout_url: 'https://buy.paddle.com/operator-live',
      studio_checkout_url: 'https://buy.paddle.com/studio-live',
      plan_price_operator: '$19 / month',
      plan_price_studio: '$99 / month',
    })

    expect(payload.manifest.platforms[0]).toMatchObject({
      platform_key: 'windows',
      configured: true,
      channel: 'candidate',
      version: '0.9.4',
      build_id: 'win64-0094',
      checksum_sha256: 'abc123',
      file_size_bytes: 1048576,
      download_available: false,
      download_url: null,
      validation_summary: {
        lane: 'Windows Unreal packaged validation',
        result: 'passed',
        smoke_map_count: 2,
      },
    })
  })

  it('exposes the configured download URL to entitled viewers and falls back to VITE-prefixed compatibility keys', () => {
    const payload = createReleaseManifestResponse(
      {
        authenticated: true,
        canDownload: true,
        plan: 'operator',
        role: 'operator',
        accessStatus: 'active',
      },
      {
        RELEASE_MANIFEST_VERSION: '1.0.0',
        VITE_WINDOWS_DOWNLOAD_URL: 'https://downloads.hypertwist.app/windows.exe',
        VITE_PADDLE_CHECKOUT_URL_OPERATOR: 'https://buy.paddle.com/operator-live',
      },
    )

    expect(payload.manifest.viewer).toEqual({
      authenticated: true,
      canDownload: true,
      plan: 'operator',
      role: 'operator',
      accessStatus: 'active',
    })
    expect(payload.manifest.platforms[0]).toMatchObject({
      platform_key: 'windows',
      configured: true,
      version: '1.0.0',
      download_available: true,
      download_url: 'https://downloads.hypertwist.app/windows.exe',
      validation_summary: {
        lane: 'Windows Unreal packaged validation',
        result: 'passed',
      },
    })
    expect(payload.manifest.commerce?.operator_checkout_url).toBe('https://buy.paddle.com/operator-live')
  })
})
