Files
accounted/lib/extensions/__tests__/sectors.test.ts
T
Mattsson c187fabf92 feat(shopify): Shopify order/refund feed into the transactions inbox (#1474)
* feat(shopify): Shopify order/refund feed into the transactions inbox

New extensions/general/shopify feed extension, modeled on the WooCommerce
feed: connect a Shopify store with Dev Dashboard custom-app client
credentials (client credentials grant, ~24h tokens, never stored), then a
nightly cron + manual sync imports paid orders and refunds via the GraphQL
Admin API (pinned 2026-07) into the transactions inbox on clearing account
1584. Feed-only: nothing auto-books. Zero PII fields are queried, keeping
the app outside Shopify's protected customer data program.

- shopify_connections migration (RLS, revoke-never-delete, encrypted
  client id/secret) + shopify_sync capability and bank_sync-mirrored
  backfill
- frozen external_id scheme shopify_{shop_domain}_order|refund_{id},
  scoped on the shop domain so reconnects never re-import
- cursor sync on updated_at windows with 24h overlap, lock-date drop at
  map time, ingest-failure cursor floor, deadline stop-and-resume,
  revoked-credential flip
- /import card + settings panel, sv/en i18n, cron 03:15 in vercel.json +
  regenerated Docker crontabs, logo, events, panel registry
- 65 unit tests + pg-real RLS test; extensions.schema.json enum also
  gains the missing stripe entry (pre-existing drift)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(shopify): review findings from PR 1474

- token exchange: a 429 that survives every retry is throttling, not a
  credential failure; stop remapping retryable 4xx to 401 so sustained
  throttling can no longer flip the connection to revoked and delete the
  stored credentials (CodeRabbit critical)
- order sync: advance a scanned-through watermark (run start, capped by
  the failure floor) after a fully-listed window, so empty first runs and
  quiet stores rotate to the back of the cron's oldest-first selection
  instead of permanently occupying the 50-connection batch (CodeRabbit
  major, starvation)
- add handler-level tests for the orders cron route (auth 401, disabled
  503, unconfigured no-op, query failure, capability skip, happy path,
  per-connection failure isolation, revoked marking)
- add 401 tests for /sync, /transaction-sync and /disconnect; pin the
  cursor floor rule with a two-order page; stub the encryption key via
  vi.stubEnv
- note in the panel description (sv/en) that orders can mix VAT rates and
  must be split at booking (Swedish review advisory)
- DECISIONS.md: wrap underscore identifiers in backticks (MD037)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-09 12:44:08 +02:00

113 lines
3.3 KiB
TypeScript

import { resolve, join } from 'path'
import { readdirSync, readFileSync } from 'fs'
/**
* Build EXTENSION_DEFINITIONS from manifest.json files so the test
* is independent of extensions.config.json.
*/
function buildDefinitionsFromManifests(): Record<string, unknown[]> {
const extensionsDir = resolve(__dirname, '../../../extensions')
const result: Record<string, unknown[]> = {}
function walk(dir: string) {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const fullPath = join(dir, entry.name)
if (entry.isDirectory()) {
walk(fullPath)
} else if (entry.name === 'manifest.json') {
const manifest = JSON.parse(readFileSync(fullPath, 'utf-8'))
const sector: string = manifest.sector
if (!result[sector]) result[sector] = []
result[sector].push({
slug: manifest.id,
sector: manifest.sector,
...manifest.definition,
})
}
}
}
walk(extensionsDir)
return result
}
vi.mock('@/lib/extensions/_generated/sector-definitions', () => ({
EXTENSION_DEFINITIONS: buildDefinitionsFromManifests(),
}))
import {
SECTORS,
getSector,
getExtensionDefinition,
getAllExtensions,
getExtensionsBySector,
} from '../sectors'
describe('sectors registry', () => {
it('should have 1 sector', () => {
expect(SECTORS.length).toBe(1)
})
it('should have 17 total extensions', () => {
expect(getAllExtensions().length).toBe(17)
})
it('should have unique slugs within each sector', () => {
for (const sector of SECTORS) {
const slugs = sector.extensions.map(e => e.slug)
const uniqueSlugs = new Set(slugs)
expect(uniqueSlugs.size).toBe(slugs.length)
}
})
it('should have at least one extension per sector', () => {
for (const sector of SECTORS) {
expect(sector.extensions.length).toBeGreaterThan(0)
}
})
it('getSector returns correct sector', () => {
const sector = getSector('general')
expect(sector).toBeDefined()
expect(sector!.slug).toBe('general')
expect(sector!.name).toBe('Generella verktyg')
})
it('getSector returns undefined for unknown slug', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sector = getSector('invalid' as any)
expect(sector).toBeUndefined()
})
it('getExtensionDefinition returns correct extension', () => {
const ext = getExtensionDefinition('general', 'mcp-server')
expect(ext).toBeDefined()
expect(ext!.slug).toBe('mcp-server')
expect(ext!.name).toBe('MCP-server (API)')
expect(ext!.sector).toBe('general')
})
it('getExtensionDefinition returns undefined for unknown extension', () => {
const ext = getExtensionDefinition('general', 'nonexistent')
expect(ext).toBeUndefined()
})
it('getExtensionsBySector returns extensions for a sector', () => {
const extensions = getExtensionsBySector('general')
expect(extensions.length).toBe(17)
})
it('all extensions have required fields', () => {
for (const ext of getAllExtensions()) {
expect(ext.slug).toBeTruthy()
expect(ext.name).toBeTruthy()
expect(ext.sector).toBeTruthy()
expect(ext.category).toBeTruthy()
expect(ext.description).toBeTruthy()
expect(ext.longDescription).toBeTruthy()
expect(ext.icon).toBeTruthy()
expect(ext.dataPattern).toBeTruthy()
}
})
})