|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os validate` names a mapping whose `connectorSource.connector` is not a |
| 5 | + * declared rest or openapi connector, and says when the name is a datasource. |
| 6 | + * |
| 7 | + * ## The defect, measured before the rule |
| 8 | + * |
| 9 | + * A mapping pointing its pull at the datasource `legacy_db` validated at exit |
| 10 | + * 0 with no finding of any kind. The boot then scheduled the `job` that pulls |
| 11 | + * it, and every tick wrote a failed job run (`connector_not_registered`) that |
| 12 | + * nothing else reported. The rule (`mapping-connector-source-unknown`) is a |
| 13 | + * reference-integrity member, so `os validate` runs it on the whole stack. |
| 14 | + * |
| 15 | + * ## The control |
| 16 | + * |
| 17 | + * The same stack's second mapping names the declared `rest` connector |
| 18 | + * `crm_api`: no finding at its path. One spawn carries both legs. |
| 19 | + * |
| 20 | + * ## The provider pair |
| 21 | + * |
| 22 | + * `@objectstack/lint` cannot import `@objectstack/service-automation`, so the |
| 23 | + * rule keeps its own copy of the providers a pull reads through. This package |
| 24 | + * depends on both, so the parity case below goes red the day the pull's pair |
| 25 | + * changes and the lint copy does not. |
| 26 | + * |
| 27 | + * The CLI runs through `bin/run-dev.js` (the source entry), and |
| 28 | + * `@objectstack/lint` resolves through `exports` to `dist/`, so the lint |
| 29 | + * package must be built for this file to see the rule. |
| 30 | + */ |
| 31 | + |
| 32 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 33 | +import { execFile } from 'node:child_process'; |
| 34 | +import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from 'node:fs'; |
| 35 | +import { tmpdir } from 'node:os'; |
| 36 | +import { join, resolve } from 'node:path'; |
| 37 | +import { fileURLToPath } from 'node:url'; |
| 38 | +import { childEnv } from './helpers/serve-process.js'; |
| 39 | +import { defineStackSource, linkSpec } from './helpers/define-stack-fixture.js'; |
| 40 | +import { CONNECTOR_SOURCE_PROVIDERS } from '@objectstack/lint'; |
| 41 | +import { CONNECTOR_PULL_PROVIDERS } from '@objectstack/service-automation'; |
| 42 | + |
| 43 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 44 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 45 | +/** A cold spawn of the CLI source entry runs well past vitest's 5 s default. */ |
| 46 | +const SPAWN_TIMEOUT_MS = 120_000; |
| 47 | +const RULE = 'mapping-connector-source-unknown'; |
| 48 | + |
| 49 | +interface Run { |
| 50 | + code: number; |
| 51 | + stdout: string; |
| 52 | + stderr: string; |
| 53 | +} |
| 54 | + |
| 55 | +function runCli(args: string[], cwd: string): Promise<Run> { |
| 56 | + return new Promise((resolvePromise) => { |
| 57 | + execFile( |
| 58 | + process.execPath, |
| 59 | + [CLI, ...args], |
| 60 | + { cwd, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 61 | + (err, stdout, stderr) => { |
| 62 | + resolvePromise({ |
| 63 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 64 | + stdout: String(stdout), |
| 65 | + stderr: String(stderr), |
| 66 | + }); |
| 67 | + }, |
| 68 | + ); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +const pull = (name: string, connector: string) => ({ |
| 73 | + name, |
| 74 | + label: name, |
| 75 | + sourceFormat: 'json', |
| 76 | + targetObject: 'fx_contact', |
| 77 | + mode: 'upsert', |
| 78 | + upsertKey: ['external_id'], |
| 79 | + fieldMapping: [ |
| 80 | + { source: 'id', target: 'external_id', transform: 'none' }, |
| 81 | + { source: 'name', target: 'name', transform: 'none' }, |
| 82 | + ], |
| 83 | + connectorSource: { connector, action: 'request' }, |
| 84 | +}); |
| 85 | + |
| 86 | +const STACK = { |
| 87 | + manifest: { id: 'com.example.fx-connector-ref', name: 'fx', version: '0.1.0', type: 'app', namespace: 'fx' }, |
| 88 | + objects: [ |
| 89 | + { |
| 90 | + name: 'fx_contact', |
| 91 | + label: 'Contact', |
| 92 | + sharingModel: 'private', |
| 93 | + fields: { name: { type: 'text', label: 'Name' }, external_id: { type: 'text', label: 'External ID' } }, |
| 94 | + }, |
| 95 | + ], |
| 96 | + datasources: [{ name: 'legacy_db', driver: 'sqlite', config: { filename: 'legacy.db' } }], |
| 97 | + connectors: [{ name: 'crm_api', label: 'CRM', type: 'api', provider: 'rest' }], |
| 98 | + mappings: [pull('fx_contacts_legacy', 'legacy_db'), pull('fx_contacts_crm', 'crm_api')], |
| 99 | +}; |
| 100 | + |
| 101 | +let root = ''; |
| 102 | + |
| 103 | +beforeAll(() => { |
| 104 | + root = mkdtempSync(join(tmpdir(), 'os-validate-connector-source-ref-')); |
| 105 | + mkdirSync(join(root, 'app'), { recursive: true }); |
| 106 | + writeFileSync(join(root, 'app', 'objectstack.config.ts'), defineStackSource(STACK)); |
| 107 | + linkSpec(join(root, 'app')); |
| 108 | +}); |
| 109 | + |
| 110 | +afterAll(() => { |
| 111 | + if (root) rmSync(root, { recursive: true, force: true }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('`os validate` resolves a mapping\'s connectorSource.connector', () => { |
| 115 | + it('names the datasource, and passes the mapping naming a declared rest connector', async () => { |
| 116 | + const run = await runCli(['validate', '--json'], join(root, 'app')); |
| 117 | + let payload: Record<string, any>; |
| 118 | + try { |
| 119 | + payload = JSON.parse(run.stdout) as Record<string, any>; |
| 120 | + } catch { |
| 121 | + throw new Error(`stdout was not one JSON document (exit ${run.code})\n${run.stdout}\n${run.stderr}`); |
| 122 | + } |
| 123 | + const all = JSON.stringify(payload); |
| 124 | + const findings = [...(payload.errors ?? []), ...(payload.warnings ?? []), ...(payload.issues ?? [])] as Array< |
| 125 | + { rule?: string; path?: string; message?: string; severity?: string } |
| 126 | + >; |
| 127 | + const hits = findings.filter((f) => f.rule === RULE); |
| 128 | + expect(hits.map((f) => f.path), all).toEqual(['mappings[0].connectorSource.connector']); |
| 129 | + expect(hits[0].message).toContain('"legacy_db" is a datasource'); |
| 130 | + // An advisory: the stack still validates (the connector universe reaches past this stack). |
| 131 | + expect(run.code, `${run.stdout}\n${run.stderr}`).toBe(0); |
| 132 | + }, SPAWN_TIMEOUT_MS); |
| 133 | +}); |
| 134 | + |
| 135 | +describe('the lint rule\'s provider pair is the connector pull\'s', () => { |
| 136 | + it('CONNECTOR_SOURCE_PROVIDERS (lint) equals CONNECTOR_PULL_PROVIDERS (service-automation)', () => { |
| 137 | + expect([...CONNECTOR_SOURCE_PROVIDERS]).toEqual([...CONNECTOR_PULL_PROVIDERS]); |
| 138 | + }); |
| 139 | +}); |
0 commit comments