import { describe, expect, test } from 'bun:test'
import { readFileSync } from 'node:fs'

import { creditsHtml, infoPlist, workspaceVersion } from '../tools/package'

describe('release packaging', () => {
  test('reads the workspace version, not a member or dependency version', () => {
    const toml = '[package]\nversion = "9.9.9"\n\n[workspace.package]\nedition = "2024"\nversion = "1.2.3"\n\n[workspace.dependencies]\nserde = { version = "1" }\n'
    expect(workspaceVersion(toml)).toBe('1.2.3')
    expect(() => workspaceVersion('[package]\nversion = "1.0.0"\n')).toThrow()
    expect(workspaceVersion(readFileSync('Cargo.toml', 'utf8'))).toMatch(/^\d+\.\d+\.\d+$/)
  })

  test('Info.plist names the universal executable, icon and version', () => {
    const plist = infoPlist('1.2.3', '42', 2026)
    for (const [key, value] of [
      ['CFBundleExecutable', 'fmm'],
      ['CFBundleIconFile', 'AppIcon'],
      ['CFBundleShortVersionString', '1.2.3'],
      ['CFBundleVersion', '42'],
      ['NSHumanReadableCopyright', '© 2026 werry'],
      ['CFBundleIdentifier', 'com.werry.factoriomodmanager'],
      ['LSMinimumSystemVersion', '11.0'],
    ]) {
      expect(plist).toContain(`<key>${key}</key>\n  <string>${value}</string>`)
    }
    expect(plist).toContain('<key>NSHighResolutionCapable</key>\n  <true/>')
  })

  test('About panel credits link the author and the repository', () => {
    const html = creditsHtml()
    expect(html).toContain('<a href="https://gitgud.io/werry">werry</a>')
    expect(html).toContain('<a href="https://gitgud.io/werry/factoriomodmanager">gitgud.io/werry/factoriomodmanager</a>')
  })
})

describe('release publishing', () => {
  test('parses SSH and HTTPS origins to the API host and project path', async () => {
    const { parseRemote } = await import('../tools/release')
    for (const url of [
      'git@gitgud.io:werry/factoriomodmanager.git',
      'git@ssh.gitgud.io:werry/factoriomodmanager.git',
      'ssh://git@ssh.gitgud.io:22/werry/factoriomodmanager.git',
      'https://gitgud.io/werry/factoriomodmanager.git',
      'https://oauth2:secret@gitgud.io/werry/factoriomodmanager/',
    ]) {
      expect(parseRemote(url)).toEqual({ host: 'gitgud.io', project: 'werry/factoriomodmanager' })
    }
    expect(parseRemote('git@gitgud.io:group/sub/project.git').project).toBe('group/sub/project')
    expect(() => parseRemote('/local/path')).toThrow()
  })

  test('notes cover only the platforms that were built', async () => {
    const { releaseNotes } = await import('../tools/release')
    const all = releaseNotes('1.2.3', ['a-macos-universal.dmg', 'a-windows-x86_64.exe', 'a-linux-x86_64.AppImage', 'a-arch-x86_64.pkg.tar.zst'])
    expect(all).toContain('Privacy & Security')
    expect(all).toContain('Windows 10/11')
    expect(all).toContain('glibc 2.35+')
    expect(all).toContain('sudo pacman -U')
    const mac = releaseNotes('1.2.3', ['a-macos-universal.dmg'])
    expect(mac).not.toContain('Windows')
    expect(mac).not.toContain('Linux')
  })

  test('fallback notes contain installation instructions and no generated changelog', async () => {
    const { releaseNotes } = await import('../tools/release')
    const notes = releaseNotes('1.2.3', ['a-macos-universal.dmg'])
    expect(notes).toContain('Factorio Mod Manager 1.2.3.')
    expect(notes).toContain('## Install')
    expect(notes).not.toContain('## Changes')
  })
})
