Files
accounted/lib/import/__tests__/sie-import.undone-slot.pg.test.ts
T
Jakob WennbergandClaude Sonnet 5 ec27228a8e style: remove em/en dashes repo-wide, add CLAUDE.md rule against them (#890)
Em dashes (—) and en dashes (–) had spread across comments, docs, tests,
and a few UI strings, reading as AI-generated boilerplate rather than
house style. Replaced each with punctuation matching its context: colon
for explanatory clauses, comma for asides, plain hyphen for numeric/legal
ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for
paired-dash asides. messages/en.json and messages/sv.json were fixed by
hand together to keep sv/en in sync.

Left untouched where the dash is the functional subject rather than
decorative punctuation: date-range-parser.ts's separator regex,
charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE
encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the
agent system-prompt files that already instruct against em dashes, and
a golden iXBRL test fixture compared byte-for-byte.

Also fixes two bugs surfaced along the way: an off-by-one in
ApiKeysPanel's scope-label split (a leftover from an earlier partial
pass), and a charset-repair test that had lost the literal en-dash it
exists to verify.

Regenerated the agent atom seed migration (skills:generate) since 27
SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes,
with an explicit carve-out for the functional-dash cases above.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 15:58:06 +02:00

112 lines
3.5 KiB
TypeScript

import { randomUUID } from 'node:crypto'
import { describe, expect, it } from 'vitest'
import { getPool } from '@/tests/pg/setup'
import { seedCompany } from '@/tests/pg/fixtures'
// Migration 20260529120000_sie_imports_undone_release_slot.sql extends
// the partial unique index sie_imports_company_id_file_hash_active_idx
// to also exclude 'undone'. Without this, undo_sie_import marks a row
// 'undone' but the slot stays held: the caller cannot re-import the
// same file.
async function insertSIEImport(params: {
companyId: string
userId: string
fileHash: string
status: 'pending' | 'mapped' | 'completed' | 'failed' | 'replaced' | 'undone'
fiscalPeriodId?: string
}): Promise<string> {
const id = randomUUID()
await getPool().query(
`INSERT INTO public.sie_imports
(id, user_id, company_id, filename, file_hash, sie_type,
fiscal_year_start, fiscal_year_end, accounts_count, transactions_count,
status, fiscal_period_id, imported_at)
VALUES ($1, $2, $3, 'undone-test.se', $4, 4,
'2026-01-01', '2026-12-31', 0, 0,
$5, $6, $7)`,
[
id,
params.userId,
params.companyId,
params.fileHash,
params.status,
params.fiscalPeriodId ?? null,
params.status === 'completed' ? new Date().toISOString() : null,
],
)
return id
}
describe('sie_imports partial unique index: undone status releases the slot', () => {
it('still blocks a duplicate active row (regression guard)', async () => {
const { companyId, userId, fiscalPeriodId } = await seedCompany()
const hash = `hash-${randomUUID()}`
await insertSIEImport({ companyId, userId, fileHash: hash, status: 'completed', fiscalPeriodId })
await expect(
insertSIEImport({ companyId, userId, fileHash: hash, status: 'pending', fiscalPeriodId }),
).rejects.toThrow(/sie_imports_company_id_file_hash_active_idx/)
})
it('allows a new pending row once the prior is undone', async () => {
const { companyId, userId, fiscalPeriodId } = await seedCompany()
const hash = `hash-${randomUUID()}`
const priorId = await insertSIEImport({
companyId,
userId,
fileHash: hash,
status: 'completed',
fiscalPeriodId,
})
// Mimic undo_sie_import's terminal write (we don't run the RPC here:
// the RPC also detaches docs + deletes JEs which need richer setup).
await getPool().query(
`UPDATE public.sie_imports SET status = 'undone', replaced_at = now() WHERE id = $1`,
[priorId],
)
const newId = await insertSIEImport({
companyId,
userId,
fileHash: hash,
status: 'pending',
fiscalPeriodId,
})
expect(newId).toBeTruthy()
})
it('allows a re-import after a 0-entry vacuous import is backfilled to failed', async () => {
// Mirrors the Lookma AB recovery path: 0-entry 'completed' rows are
// backfilled to 'failed' by the same migration; the partial index
// already excludes 'failed', so a fresh re-import succeeds.
const { companyId, userId, fiscalPeriodId } = await seedCompany()
const hash = `hash-${randomUUID()}`
const stuckId = await insertSIEImport({
companyId,
userId,
fileHash: hash,
status: 'completed',
fiscalPeriodId,
})
await getPool().query(
`UPDATE public.sie_imports SET status = 'failed' WHERE id = $1`,
[stuckId],
)
const newId = await insertSIEImport({
companyId,
userId,
fileHash: hash,
status: 'pending',
fiscalPeriodId,
})
expect(newId).toBeTruthy()
})
})