Files
accounted/lib/pending-operations/schemas/__tests__/article.test.ts
T
Jakob Wennberg d012d40b18 feat(mcp): currency param on create_article and update_article (#1184)
Fixes #1168. articles.currency exists in the DB and REST API, but the
staged-operation schemas and the two MCP tools had no currency param,
so agent-created articles were always SEK and an agent asked to create
an EUR-priced article could not.

- CreateArticleParamsSchema/UpdateArticleParamsSchema accept an
  optional ISO 4217 code (normalized to upper case; empty/null =
  unset). The currencies-table FK stays the allow-list: a 23503 on the
  currency FK maps to a clear 400 instead of a raw 500.
- commitCreateArticle inserts currency ?? 'SEK'; the sparse update
  executor passes it through only when staged.
- gnubok_create_article / gnubok_update_article expose the param.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-25 12:59:32 +02:00

37 lines
1.5 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { CreateArticleParamsSchema, UpdateArticleParamsSchema } from '../article'
const base = { name: 'EU-konsulting', price_excl_vat: 950 }
describe('CreateArticleParamsSchema currency', () => {
it('accepts and normalizes an ISO code to upper case', () => {
const parsed = CreateArticleParamsSchema.parse({ ...base, currency: 'eur' })
expect(parsed.currency).toBe('EUR')
})
it('treats empty string and null as unset (commit defaults to SEK)', () => {
expect(CreateArticleParamsSchema.parse({ ...base, currency: '' }).currency).toBeUndefined()
expect(CreateArticleParamsSchema.parse({ ...base, currency: null }).currency).toBeUndefined()
expect(CreateArticleParamsSchema.parse(base).currency).toBeUndefined()
})
it('rejects non-ISO shapes', () => {
expect(() => CreateArticleParamsSchema.parse({ ...base, currency: 'EURO' })).toThrow()
expect(() => CreateArticleParamsSchema.parse({ ...base, currency: 'E1' })).toThrow()
})
})
describe('UpdateArticleParamsSchema currency', () => {
const id = { article_id: '3a9ac4d2-163a-4d43-8fa3-1b32827505fa' }
it('accepts a currency-only update', () => {
const parsed = UpdateArticleParamsSchema.parse({ ...id, currency: 'usd' })
expect(parsed.currency).toBe('USD')
})
it('leaves currency undefined when omitted (sparse update must not touch it)', () => {
const parsed = UpdateArticleParamsSchema.parse({ ...id, name: 'Nytt namn' })
expect(parsed.currency).toBeUndefined()
})
})