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.

7.7 KB
Raw Blame
/**
 * Integration test for the bundled plugin template. Renders the real
 * `templates/plugin/` tree into a temp dir and verifies the output looks
 * right end-to-end. Catches missing variables, broken paths, and bad
 * substitution in a way the renderer's unit tests can't.
 */
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { PLUGIN_PEER_RANGES } from '../src/lib/plugin-peer-ranges'
import { render, type TemplateVars } from '../src/lib/template'

const TEMPLATE_SRC = path.resolve(__dirname, '../templates/plugin')

const SAMPLE_VARS: TemplateVars = {
  name: 'brands',
  plugin_name: 'brands',
  ruby_name: 'spree_brands',
  module_name: 'SpreeBrands',
  npm_scope: '@acme',
  npm_package_name: '@acme/brands',
  npm_dashboard_package: '@acme/brands-dashboard',
  author_name: 'Jane Developer',
  author_email: '[email protected]',
  license: 'MIT',
  year: '2026',
  ...PLUGIN_PEER_RANGES,
}

function workspaceVersion(dir: string): string {
  return JSON.parse(fs.readFileSync(path.resolve(__dirname, `../../${dir}/package.json`), 'utf8'))
    .version
}

describe('bundled plugin template', () => {
  const tempDirs: string[] = []

  function tempDir(): string {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'spree-cli-plugin-test-'))
    tempDirs.push(dir)
    return dir
  }

  afterEach(() => {
    for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true })
    tempDirs.length = 0
  })

  it('renders into a working structure with all halves', () => {
    const dst = path.join(tempDir(), 'out')
    render({ src: TEMPLATE_SRC, dst, vars: SAMPLE_VARS })

    expect(fs.existsSync(path.join(dst, 'package.json'))).toBe(true)
    expect(fs.existsSync(path.join(dst, 'pnpm-workspace.yaml'))).toBe(true)
    expect(fs.existsSync(path.join(dst, 'README.md'))).toBe(true)
    expect(fs.existsSync(path.join(dst, 'LICENSE'))).toBe(true)
    expect(fs.existsSync(path.join(dst, '.gitignore'))).toBe(true)

    expect(fs.existsSync(path.join(dst, 'packages/dashboard/package.json'))).toBe(true)
    expect(fs.existsSync(path.join(dst, 'packages/dashboard/src/index.tsx'))).toBe(true)
    expect(fs.existsSync(path.join(dst, 'packages/dashboard/src/client.ts'))).toBe(true)
    expect(fs.existsSync(path.join(dst, 'packages/dashboard/src/types.ts'))).toBe(true)

    expect(fs.existsSync(path.join(dst, 'packages/dashboard/src/pages/brands-list.tsx'))).toBe(true)
    expect(fs.existsSync(path.join(dst, 'packages/dashboard/src/routes/brands.index.tsx'))).toBe(
      true,
    )
    expect(
      fs.existsSync(path.join(dst, 'packages/dashboard/src/slots/product-brands-card.tsx')),
    ).toBe(true)
    expect(fs.existsSync(path.join(dst, 'packages/dashboard/src/locales/en.json'))).toBe(true)
  })

  it('renders the root package.json with substituted name', () => {
    const dst = path.join(tempDir(), 'out')
    render({ src: TEMPLATE_SRC, dst, vars: SAMPLE_VARS })

    const pkg = JSON.parse(fs.readFileSync(path.join(dst, 'package.json'), 'utf8'))
    expect(pkg.name).toBe('@acme/brands')
    expect(pkg.author).toBe('Jane Developer <[email protected]>')
    expect(pkg.license).toBe('MIT')
  })

  it('renders the dashboard package.json with the dashboard sub-name', () => {
    const dst = path.join(tempDir(), 'out')
    render({ src: TEMPLATE_SRC, dst, vars: SAMPLE_VARS })

    const pkg = JSON.parse(
      fs.readFileSync(path.join(dst, 'packages/dashboard/package.json'), 'utf8'),
    )
    expect(pkg.name).toBe('@acme/brands-dashboard')
  })

  // Hand-written ranges drifted to versions no published package matched. The
  // range covers the whole major, since the CLI can release before the
  // workspace version reaches npm.
  it('peer-depends on the admin SDK and dashboard packages released with this CLI', () => {
    const majorOf = (dir: string) => `^${workspaceVersion(dir).split('.')[0]}.0.0-0`
    const dst = path.join(tempDir(), 'out')
    render({ src: TEMPLATE_SRC, dst, vars: SAMPLE_VARS })

    const pkg = JSON.parse(
      fs.readFileSync(path.join(dst, 'packages/dashboard/package.json'), 'utf8'),
    )
    expect(pkg.peerDependencies).toMatchObject({
      '@spree/admin-sdk': majorOf('admin-sdk'),
      '@spree/dashboard-core': majorOf('dashboard-core'),
      '@spree/dashboard-ui': majorOf('dashboard-ui'),
    })
  })

  it('makes the dashboard package buildable and safe from tree-shaking', () => {
    const dst = path.join(tempDir(), 'out')
    render({ src: TEMPLATE_SRC, dst, vars: SAMPLE_VARS })

    const pkg = JSON.parse(
      fs.readFileSync(path.join(dst, 'packages/dashboard/package.json'), 'utf8'),
    )
    // The root `pnpm build` runs `pnpm -r build`, which fails when no package has one.
    expect(pkg.scripts.build).toBeDefined()
    // The entry registers the plugin at import time; bundlers must keep it.
    expect(pkg.sideEffects).toContain(pkg.exports['.'])
  })

  it('substitutes module names + paths consistently in the dashboard entry', () => {
    const dst = path.join(tempDir(), 'out')
    render({ src: TEMPLATE_SRC, dst, vars: SAMPLE_VARS })

    const index = fs.readFileSync(path.join(dst, 'packages/dashboard/src/index.tsx'), 'utf8')
    expect(index).toContain("from './client'")
    expect(index).toContain("from './slots/product-brands-card'")
    expect(index).toContain('SpreeBrandsClient')
    expect(index).toContain('SpreeBrandsCard')
    expect(index).toContain("path: '/brands'")
    expect(index).toContain('admin.brands_plugin')

    // Routes ship as file routes: full final path literal + marker, and the
    // route file wires the page component from pages/.
    const routeFile = fs.readFileSync(
      path.join(dst, 'packages/dashboard/src/routes/brands.index.tsx'),
      'utf8',
    )
    expect(routeFile).toContain("createFileRoute('/_authenticated/$storeId/brands/')")
    expect(routeFile).toContain("from '../pages/brands-list'")
    const pkg = JSON.parse(
      fs.readFileSync(path.join(dst, 'packages/dashboard/package.json'), 'utf8'),
    )
    expect(pkg.spree.dashboard.routes).toBe('./src/routes')
  })

  it('substitutes the README', () => {
    const dst = path.join(tempDir(), 'out')
    render({ src: TEMPLATE_SRC, dst, vars: SAMPLE_VARS })

    const readme = fs.readFileSync(path.join(dst, 'README.md'), 'utf8')
    expect(readme).toContain('# brands')
    expect(readme).toContain('@acme/brands-dashboard')
    expect(readme).toContain("gem 'spree_brands'")
    expect(readme).toContain('Jane Developer')
    expect(readme).not.toContain('{{')
    // The CLI generates no engine/ directory (yet), so the README must not describe one.
    expect(readme).not.toContain('engine/')
  })

  it('skips the dashboard subtree when asked', () => {
    const dst = path.join(tempDir(), 'out')
    render({
      src: TEMPLATE_SRC,
      dst,
      vars: SAMPLE_VARS,
      skip: (rel) => rel === 'packages/dashboard' || rel.startsWith('packages/dashboard/'),
    })

    expect(fs.existsSync(path.join(dst, 'package.json'))).toBe(true)
    expect(fs.existsSync(path.join(dst, 'packages/dashboard'))).toBe(false)
  })

  it('leaves no `{{...}}` tokens in any rendered file', () => {
    const dst = path.join(tempDir(), 'out')
    render({ src: TEMPLATE_SRC, dst, vars: SAMPLE_VARS })

    const stragglers: string[] = []
    function walk(dir: string) {
      for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
        const p = path.join(dir, e.name)
        if (e.isDirectory()) walk(p)
        else {
          const content = fs.readFileSync(p, 'utf8')
          if (/\{\{[a-zA-Z_]/.test(content)) {
            stragglers.push(
              `${path.relative(dst, p)}: ${content.match(/\{\{[a-zA-Z_][a-zA-Z0-9_]*\}\}/)?.[0]}`,
            )
          }
        }
      }
    }
    walk(dst)

    expect(stragglers).toEqual([])
  })
})