展示:listBindingsForBranch 过滤 source='backfill',分公司管理「绑定关系」弹层不再显示迁移回填行 —— 含 main_branch_code='*' 的「全部(不区分主系统分公司)」;没有真绑定就显示未绑定空态。 登录:删除 mainSystemSupplierAuth.ts(两个只读辅助)与 /token、/unified-login、/api/mobile/login 的 403 supplier_not_provisioned 分支 ⇒ 登录失败一律 400 invalid_credentials;修正 index.ts 过期启动日志文案。同步测试与设计文档。
162 lines
6.5 KiB
TypeScript
162 lines
6.5 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { readFileSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
/**
|
|
* 「未绑接入编码却拉到货」的**我方侧收紧**(2026-10-13)。
|
|
*
|
|
* 事故:开发者「接入编码明明没给主系统绑定,订单却被分配过来了」(真机样本
|
|
* `ORD_API_20261013_SUP35143228_0001`)。两层根因:
|
|
* ① 主系统侧:`supplier_integration_client_suppliers` 的"凭据→供应商"授权**不要求**填
|
|
* `subsystem_code`(实测本地主系统 5 条授权里 2 条为 NULL)⇒ 空码也能换 Token 拉货
|
|
* (已记入 `docs/给主系统AI的对接改造清单.md` §2.13「P0-空码放行」待对方裁决);
|
|
* ② 我方侧:`migrations/20261031` 的**迁移回填**会建 `main_branch_code='*'` 的 active 通配绑定,
|
|
* 本地据此**主动去拉**。
|
|
*
|
|
* 本文件钉住 ② 的封堵:自动拉单只认「主系统明确把我们的接入编码绑到了该供应商」的绑定。
|
|
*/
|
|
const query = vi.hoisted(() => vi.fn());
|
|
const fetchPreps = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock('../src/db', () => ({
|
|
pool: { query: (...args: unknown[]) => query(...args) },
|
|
}));
|
|
vi.mock('../src/services/mainSystemPreparations', () => ({
|
|
fetchMainSystemPreparations: (...args: unknown[]) => fetchPreps(...args),
|
|
}));
|
|
|
|
import { listAutoPullableBindingsForBranch } from '../src/services/branchAccessCode';
|
|
import { pullPreparationsForBranch } from '../src/services/mainSystemBranchPull';
|
|
|
|
const pullSrc = readFileSync(
|
|
path.join(__dirname, '../src/services/mainSystemBranchPull.ts'),
|
|
'utf8'
|
|
);
|
|
const accessSrc = readFileSync(
|
|
path.join(__dirname, '../src/services/branchAccessCode.ts'),
|
|
'utf8'
|
|
);
|
|
const mainConfigSrc = readFileSync(
|
|
path.join(__dirname, '../src/services/mainSystem/config.ts'),
|
|
'utf8'
|
|
);
|
|
const indexSrc = readFileSync(path.join(__dirname, '../src/index.ts'), 'utf8');
|
|
|
|
type Row = {
|
|
branch_code: string;
|
|
access_code: string;
|
|
main_branch_code: string;
|
|
main_supplier_code: string;
|
|
status: string;
|
|
source: string;
|
|
bound_at: string;
|
|
unbound_at: string | null;
|
|
};
|
|
|
|
const row = (over: Partial<Row> = {}): Row => ({
|
|
branch_code: 'BR003',
|
|
access_code: 'SCSTSHD6XVX4',
|
|
main_branch_code: 'BR002',
|
|
main_supplier_code: 'SUP41472132',
|
|
status: 'active',
|
|
source: 'main_push',
|
|
bound_at: '2026-10-10T09:50:31.130Z',
|
|
unbound_at: null,
|
|
...over,
|
|
});
|
|
|
|
function wire(bindings: Row[], currentAccessCode: string | null) {
|
|
query.mockImplementation((sql: string) => {
|
|
const text = String(sql);
|
|
if (text.includes('FROM public.main_supplier_bindings')) {
|
|
return Promise.resolve({ rows: bindings, rowCount: bindings.length });
|
|
}
|
|
if (text.includes('access_code FROM public.branches')) {
|
|
return Promise.resolve({
|
|
rows: currentAccessCode === null ? [] : [{ access_code: currentAccessCode }],
|
|
rowCount: currentAccessCode === null ? 0 : 1,
|
|
});
|
|
}
|
|
throw new Error(`未预期的 SQL: ${text.slice(0, 60)}`);
|
|
});
|
|
}
|
|
|
|
describe('自动拉单只认「接入编码已绑给主系统」的绑定', () => {
|
|
beforeEach(() => {
|
|
query.mockReset();
|
|
fetchPreps.mockReset();
|
|
});
|
|
|
|
it('① 过滤:回填行 / 空接入编码 / 编码已换的残留行 一律不参与自动拉单', async () => {
|
|
wire(
|
|
[
|
|
row({ main_supplier_code: 'SUP_OK', source: 'main_push' }),
|
|
row({ main_supplier_code: 'SUP_BACKFILL', source: 'backfill' }),
|
|
row({ main_supplier_code: 'SUP_EMPTY', access_code: '' }),
|
|
row({ main_supplier_code: 'SUP_STALE', access_code: 'SC-OLD-CODE' }),
|
|
row({ main_supplier_code: 'SUP_MANUAL', source: 'manual' }),
|
|
],
|
|
'SCSTSHD6XVX4'
|
|
);
|
|
const pullable = await listAutoPullableBindingsForBranch('BR003');
|
|
expect(pullable.map((b) => b.mainSupplierCode)).toEqual(['SUP_OK', 'SUP_MANUAL']);
|
|
});
|
|
|
|
it('② 分公司没有接入编码时,任何绑定都不自动拉(无法自证归属)', async () => {
|
|
wire([row()], null);
|
|
expect(await listAutoPullableBindingsForBranch('BR003')).toEqual([]);
|
|
});
|
|
|
|
it('③ 回填行 ⇒ 拉取返回 not_bound,且**不发任何 HTTP**', async () => {
|
|
wire([row({ source: 'backfill', main_branch_code: '*' })], 'SCSTSHD6XVX4');
|
|
const out = await pullPreparationsForBranch('BR003', 'active');
|
|
expect(out.kind).toBe('not_bound');
|
|
if (out.kind === 'not_bound') {
|
|
expect(out.reason).toContain('接入编码已绑给主系统');
|
|
}
|
|
expect(fetchPreps).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('④ 正常绑定(主系统推送过接入编码)⇒ 照常拉取', async () => {
|
|
wire([row()], 'SCSTSHD6XVX4');
|
|
fetchPreps.mockResolvedValue({
|
|
kind: 'ok',
|
|
rows: [{ orderCode: 'ORD_WEB_X', skuCode: 'SKU1', supplierCode: 'SUP41472132' }],
|
|
});
|
|
const out = await pullPreparationsForBranch('BR003', 'active');
|
|
expect(out.kind).toBe('ok');
|
|
expect(fetchPreps).toHaveBeenCalledTimes(1);
|
|
expect(fetchPreps.mock.calls[0][0]).toBe('SUP41472132');
|
|
});
|
|
|
|
it('⑤ 源码守卫:拉取与展示**都**排除回填行(没绑定就不拉、也不展示)', () => {
|
|
expect(pullSrc).toContain('listAutoPullableBindingsForBranch');
|
|
expect(accessSrc).toContain('export async function listAutoPullableBindingsForBranch');
|
|
expect(accessSrc).toContain("=== 'backfill'");
|
|
// 展示路径:回填行(迁移造的过渡绑定,用户没绑过)不展示
|
|
expect(accessSrc).toContain('export async function listBindingsForBranch');
|
|
expect(accessSrc).toMatch(/listBindingsForBranch[\s\S]{0,400}source <> 'backfill'/);
|
|
});
|
|
|
|
it('⑤b 展示查询在 SQL 里就排除 source=backfill(本地那两条通配回填行不再显示)', async () => {
|
|
const seen: string[] = [];
|
|
query.mockImplementation((sql: string) => {
|
|
seen.push(String(sql));
|
|
return Promise.resolve({ rows: [], rowCount: 0 });
|
|
});
|
|
const { listBindingsForBranch } = await import('../src/services/branchAccessCode');
|
|
await listBindingsForBranch('BR003');
|
|
expect(seen.join('\n')).toContain("source <> 'backfill'");
|
|
});
|
|
});
|
|
|
|
describe('环境隔离自检(事故后补:本地 dev 指向线上 + 自动拉单 ⇒ 可读可写线上)', () => {
|
|
it('⑥ 非本机地址 + 自动拉单/回写开启时告警一次;本机地址不告警', () => {
|
|
expect(mainConfigSrc).toContain('export function warnIfRemoteMainSystemWithWriteback');
|
|
expect(mainConfigSrc).toContain("host === 'localhost'");
|
|
expect(mainConfigSrc).toContain('环境隔离告警');
|
|
// 进程启动时自检
|
|
expect(indexSrc).toContain('warnIfRemoteMainSystemWithWriteback');
|
|
});
|
|
});
|