Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

4.3 KB
Raw Blame
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { Command } from 'commander'
import { afterEach, beforeEach, describe, expect, it, type Mock, vi } from 'vitest'
import { registerEjectCommand } from '../src/commands/eject'
import { dockerCompose, prepareDatabase } from '../src/docker'

const COMPOSE_DEV_STALE = `x-app: &app
  build:
    context: ./backend
    dockerfile: Dockerfile
  volumes:
    - .:/rails
    - bundle_cache:/usr/local/bundle

services:
  web:
    <<: *app
    command: ["bin/rails", "server", "-b", "0.0.0.0", "-p", "3000"]
  postgres:
    image: postgres:18-alpine
    healthcheck:
      test: pg_isready -U postgres
volumes:
  bundle_cache:
`

const COMPOSE_DEV_FIXED = COMPOSE_DEV_STALE.replace('- .:/rails', '- ./backend:/rails').replace(
  'pg_isready -U postgres',
  'pg_isready -h 127.0.0.1 -U postgres',
)

let projectDir: string

vi.mock('../src/context', () => ({
  detectProject: () => ({ mode: 'project', projectDir, port: 3000 }),
  findApiDir: (dir: string) =>
    ['server', 'backend'].find((name) => fs.existsSync(path.join(dir, name))) ?? 'server',
}))

vi.mock('../src/docker', () => ({
  dockerCompose: vi.fn().mockResolvedValue(undefined),
  prepareDatabase: vi.fn().mockResolvedValue(undefined),
}))

function makeProject(devComposeContent: string, apiDir = 'server'): string {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'spree-cli-eject-test-'))
  fs.mkdirSync(path.join(dir, apiDir))
  fs.writeFileSync(path.join(dir, 'docker-compose.yml'), 'services: {} # prebuilt-image compose\n')
  fs.writeFileSync(path.join(dir, 'docker-compose.dev.yml'), devComposeContent)
  return dir
}

async function runEject(): Promise<void> {
  const program = new Command()
  registerEjectCommand(program)
  await program.parseAsync(['eject'], { from: 'user' })
}

describe('spree eject', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  afterEach(() => {
    if (projectDir) {
      fs.rmSync(projectDir, { recursive: true, force: true })
      projectDir = ''
    }
  })

  it('repairs a stale .:/rails bind-mount in both compose files', async () => {
    projectDir = makeProject(COMPOSE_DEV_STALE)

    await runEject()

    const active = fs.readFileSync(path.join(projectDir, 'docker-compose.yml'), 'utf-8')
    const dev = fs.readFileSync(path.join(projectDir, 'docker-compose.dev.yml'), 'utf-8')
    expect(active).toContain('- ./server:/rails')
    expect(active).not.toContain('- .:/rails')
    expect(dev).toContain('- ./server:/rails')
    expect(dev).not.toContain('- .:/rails')
    // Named volumes are left untouched
    expect(active).toContain('- bundle_cache:/usr/local/bundle')
  })

  it('repairs the bind-mount against a legacy backend/ layout', async () => {
    projectDir = makeProject(COMPOSE_DEV_STALE, 'backend')

    await runEject()

    const active = fs.readFileSync(path.join(projectDir, 'docker-compose.yml'), 'utf-8')
    expect(active).toContain('- ./backend:/rails')
    expect(active).not.toContain('- .:/rails')
  })

  it('copies an already-correct dev compose verbatim', async () => {
    projectDir = makeProject(COMPOSE_DEV_FIXED)

    await runEject()

    const active = fs.readFileSync(path.join(projectDir, 'docker-compose.yml'), 'utf-8')
    const dev = fs.readFileSync(path.join(projectDir, 'docker-compose.dev.yml'), 'utf-8')
    expect(active).toBe(COMPOSE_DEV_FIXED)
    expect(dev).toBe(COMPOSE_DEV_FIXED)
  })

  it('switches the postgres healthcheck to TCP in both compose files', async () => {
    projectDir = makeProject(COMPOSE_DEV_STALE)

    await runEject()

    for (const file of ['docker-compose.yml', 'docker-compose.dev.yml']) {
      const content = fs.readFileSync(path.join(projectDir, file), 'utf-8')
      expect(content).toContain('test: pg_isready -h 127.0.0.1 -U postgres')
    }
  })

  it('prepares the development database before starting the app server', async () => {
    projectDir = makeProject(COMPOSE_DEV_STALE)

    await runEject()

    expect(prepareDatabase).toHaveBeenCalledWith(projectDir, { stdio: 'inherit' })
    expect(dockerCompose).toHaveBeenCalledWith(['up', '-d'], projectDir, { stdio: 'inherit' })
    // Puma exits when spree_development is missing, so the database must
    // exist before `up -d` starts it.
    expect((prepareDatabase as Mock).mock.invocationCallOrder[0]).toBeLessThan(
      (dockerCompose as Mock).mock.invocationCallOrder[0],
    )
  })
})