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.

1.4 KB
Raw Blame
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { describe, expect, it } from 'vitest'
import { parse } from 'yaml'
import { UNIT_SYSTEM_WEIGHT_UNITS, UNIT_SYSTEMS, WeightUnit } from '../src'

// Every `unit_system` the Admin API reference declares, wherever it appears.
function publishedUnitSystems(node: unknown, found = new Set<string>()): Set<string> {
  if (Array.isArray(node)) {
    for (const item of node) publishedUnitSystems(item, found)
  } else if (node && typeof node === 'object') {
    for (const [key, value] of Object.entries(node)) {
      const values = (value as { enum?: unknown })?.enum
      if (key === 'unit_system' && Array.isArray(values)) {
        for (const system of values) found.add(String(system))
      }
      publishedUnitSystems(value, found)
    }
  }
  return found
}

describe('unit systems', () => {
  // Parsing the multi-megabyte reference outlasts the default five seconds on CI runners.
  it('match the unit systems the Admin API accepts', () => {
    const reference = parse(
      readFileSync(resolve(__dirname, '../../../docs/api-reference/admin.yaml'), 'utf-8'),
    )
    expect([...publishedUnitSystems(reference)].sort()).toEqual([...UNIT_SYSTEMS].sort())
  }, 30_000)

  it('offer every weight unit in exactly one system', () => {
    expect(Object.values(UNIT_SYSTEM_WEIGHT_UNITS).flat().sort()).toEqual(
      Object.values(WeightUnit).sort(),
    )
  })
})