fix: 修复真实验收中的企业范围与草稿复制问题

显式企业身份核验只访问指定企业,避免回退到全量查询。
复制只清空源单存在的发票与附件字段,避免通用模板拒绝未知属性。
补充隔离回归与指定测试企业验收记录,测试数据已清理。

验证:929 项测试通过,类型检查及版本一致性检查通过。
This commit is contained in:
gitea committed 2026-10-29 22:04:36 +08:00
1 parent 6249feee23
commit 613fe3476c
8 files changed
+108 -14

No files matched your search

+2
View File
@@ -36,3 +36,5 @@
`hose-use mcp [serve]` 在当前进程启动服务,不派生 Bun 或访问源码脚本;MCP 调用继续只通过 `hose_cli` 分发,流式终端回调不会被注入协议通道。
`update --dry-run` 返回源码或二进制更新计划,不请求发布源、不执行 git/依赖安装、不替换二进制或同步技能;`update check` 和发布历史查询保留只读查询行为。
`corps whoami --corp=<id>` 只在线核验指定企业,不再遍历全部本地名录;不带 `--corp` 时保留全企业视图,`--offline` 仍不发起网络请求。
@@ -0,0 +1,54 @@
import { afterEach, expect, test } from "bun:test";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { handleWhoami } from "./whoami.js";
import { saveCorpConfig } from "../../../core/config/loader.js";
import { CorpConfigSchema } from "../../../core/config/types.js";
import { CorpContext } from "../../../core/config/manager.js";
import { parseArgs } from "../../parser.js";
import type { CliRuntime } from "../../types.js";
const originalFetch = globalThis.fetch;
const originalEnv = { ...process.env };
const directories: string[] = [];
afterEach(() => {
globalThis.fetch = originalFetch;
process.env = { ...originalEnv };
for (const dir of directories.splice(0)) rmSync(dir, { recursive: true, force: true });
});
function fixture() {
const dir = mkdtempSync(join(tmpdir(), "hose-whoami-scope-"));
directories.push(dir);
process.env.HOSE_ENTERPRISES_DIR = dir;
process.env.HOSE_USER_ENTERPRISES_DIR = dir;
process.env.HOSE_CONFIG_FILE = join(dir, "config.json");
const corps = ["one", "two"].map(id => CorpConfigSchema.parse({ id, name: id, source: "manual", baseUrl: "https://tenant.invalid", credentials: { accessToken: "fake", corpId: id, staffId: `staff-${id}` } }));
for (const corp of corps) saveCorpConfig(corp, dir);
const requests: string[] = [];
globalThis.fetch = (async (input: Request) => {
requests.push(new URL(input.url).searchParams.get("corpId")!);
return Response.json({ value: { staff: { name: "Test Staff" } } });
}) as unknown as typeof fetch;
const runtime: CliRuntime = { corpContext: new CorpContext(corps, { stateful: false }), selectedCorp: corps[0]!, jsonMode: true };
return { runtime, requests };
}
test("显式企业只查询该企业;不带企业时保留全企业身份查看", async () => {
const f = fixture();
const selected = await handleWhoami(parseArgs(["corps", "whoami", "--corp=one", "--json"]), f.runtime);
expect(f.requests).toEqual(["one"]);
expect(JSON.parse(selected.stdout).corporations.map((c: { id: string }) => c.id)).toEqual(["one"]);
f.requests.length = 0;
await handleWhoami(parseArgs(["corps", "whoami", "--json"]), f.runtime);
expect(f.requests.sort()).toEqual(["one", "two"]);
});
test("无效企业不退回全量查询;离线模式不请求网络", async () => {
const f = fixture();
await expect(handleWhoami(parseArgs(["corps", "whoami", "--corp=missing"]), { ...f.runtime, selectedCorp: null })).rejects.toThrow("未找到企业");
const result = await handleWhoami(parseArgs(["corps", "whoami", "--corp=one", "--offline", "--json"]), f.runtime);
expect(JSON.parse(result.stdout).corporations).toHaveLength(1);
expect(f.requests).toEqual([]);
});
+6 -2
View File
@@ -1,6 +1,7 @@
import { loadCorpConfigs } from "../../../core/config/loader.js";
import { createSessionClient } from "../../../core/http/session-client.js";
import { booleanOption } from "../../parser.js";
import { booleanOption, stringOption } from "../../parser.js";
import { NotFoundError } from "../../../core/errors.js";
import { formatSourceLabel, printJson, printTable } from "../../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
@@ -46,7 +47,10 @@ async function mapLimit<T, R>(items: T[], limit: number, fn: (item: T) => Promis
export async function handleWhoami(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const offline = booleanOption(parsed.options, ["offline", "cached"]);
const corps = runtime.corpContext.listCorps();
const allCorps = runtime.corpContext.listCorps();
const explicitCorp = stringOption(parsed.options, ["corp"]);
if (explicitCorp && !runtime.selectedCorp) throw new NotFoundError(`未找到企业: ${explicitCorp}`);
const corps = explicitCorp ? allCorps.filter((corp) => corp.id === runtime.selectedCorp!.id) : allCorps;
const configs = new Map<string, any>();
for (const config of loadCorpConfigs()) {
configs.set(config.id, config);
+1
View File
@@ -65,6 +65,7 @@ src/domains/bill/
10. **纯接口复制 (`copyBillAsDraft`)**:
- 读取服务端最新的 `flow.form`;
- 清空发票绑定(`invoiceForm.invoices`)、明细与单据发票总额归零、剔除 `detailStatus`;
- 只重置源单实际包含的发票字段,不向通用模板注入 `u_发票总金额` 等租户自定义属性。
- 重算报销/支付金额,附件默认保留;
- `POST /api/flow/v1/flows` 创建新草稿(`name: freeflow.edit`),默认不提交。
11. **草稿删除 (`deleteBillDraft`)**:
+6 -6
View File
@@ -81,20 +81,20 @@ export function buildBillCopyForm(
amountSum += moneyNumber(ft.amount);
// 复制不携带发票绑定;发票总额归零
ft.invoice = "0";
ft.invoiceForm = { type: "exist", invoices: [] };
ft.u_发票总金额 = zeroMoney(ft.u_发票总金额);
if (ft.invoice !== undefined) ft.invoice = "0";
if (ft.invoiceForm !== undefined) ft.invoiceForm = { type: "exist", invoices: [] };
if (ft.u_发票总金额 !== undefined) ft.u_发票总金额 = zeroMoney(ft.u_发票总金额);
delete ft.detailStatus;
if (!keepAttachments) {
if (!keepAttachments && ft.attachments !== undefined) {
ft.attachments = [];
}
}
form.u_发票总金额 = zeroMoney(form.u_发票总金额);
if (form.u_发票总金额 !== undefined) form.u_发票总金额 = zeroMoney(form.u_发票总金额);
form.expenseMoney = { ...(form.expenseMoney ?? {}), standard: amountSum.toFixed(2) };
form.payMoney = { ...(form.payMoney ?? {}), standard: (amountSum - moneyNumber(form.writtenOffMoney)).toFixed(2) };
if (!keepAttachments) {
if (!keepAttachments && form.attachments !== undefined) {
form.attachments = [];
}
+11
View File
@@ -61,6 +61,17 @@ function fixture(options: {
}
describe("bill API write contracts", () => {
test("复制通用模板不注入源单没有的发票或租户自定义字段", async () => {
const { client, writes } = fixture();
await copyBillAsDraft(client, "corp", "f", { keepAttachments: false });
expect(writes).toHaveLength(1);
expect(writes[0].json.form).not.toHaveProperty("u_发票总金额");
expect(writes[0].json.form.details[0].feeTypeForm).not.toHaveProperty("u_发票总金额");
expect(writes[0].json.form.details[0].feeTypeForm).not.toHaveProperty("invoice");
expect(writes[0].json.form.details[0].feeTypeForm).not.toHaveProperty("invoiceForm");
expect(writes[0].json.form).not.toHaveProperty("attachments");
expect(writes[0].json.form.details[0].feeTypeForm).not.toHaveProperty("attachments");
});
test("创建和预演均拒绝缺失明细规格、必填明细与过长表头,不发起创建", async () => {
for (const options of [{ missingDetailSpec: true }, { detailRequired: "u_项目" }, { titleMaxLength: 1 }]) {
const { client, writes } = fixture(options);