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

@@ -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);