节点在线、应用在线、配置在线使用令牌查询
大石头 authored at 2021-12-16 19:49:30
25.04 KiB
Stardust
// 治理页纯逻辑单测(Vitest)。覆盖:四个 tab 归一、ClusterId→集群名映射、令牌掩码、
// ProjectUser/GalaxyProject 计数、projectId 透传、单 tab 请求失败隔离。
import { describe, it, expect } from 'vitest';
import {
  toRegistryRows,
  fetchRegistry,
  fetchServiceDetail,
  saveService,
  createService,
  servicePutBody,
  servicePostBody,
  emptyServiceEditModel,
  toServiceEditModel,
  toServiceMembers,
  validateServiceEdit,
  buildClusterMap,
  certDomainMatches,
  matchCert,
  toGatewayRoutes,
  fetchGateway,
  maskToken,
  buildScopeMap,
  toMcpTokens,
  toMcpAudits,
  fetchMcp,
  fetchMcpServers,
  buildMcpServerQueryParams,
  toMembers,
  toProjectInfo,
  countCsv,
  pickProject,
  buildTiles,
  fetchPlatform,
} from './mesh.logic';
import type { Req } from '../../../apps/stardust/src/views/Stardust/appsModel.ts';

/** 桩请求:按 url 返回预设 data;throwOn 时指定 url 抛错。记录最后一次 params 用于断言透传。
 *  map 值可以是数组(列表接口)或单个对象(如 /Platform/StarServer 返回单设置对象);
 *  stubReq 统一再包一层 { data: map[url] },所以这里的值就是响应里的 data 本体。 */
function stubReq(map: Record<string, unknown>, throwOn?: string[]): Req {
  return async (opt) => {
    const url = String(opt.url ?? '');
    if (throwOn?.includes(url)) throw new Error(`stub 500:${url}`);
    if (url in map) return { data: map[url] };
    return { data: [] };
  };
}

/** 记录型桩:断言透传 params 用(记录最后一次请求 params) */
function recordReq(data: unknown = []): { req: Req; lastParams: () => Record<string, unknown> | undefined } {
  let last: Record<string, unknown> | undefined;
  const req: Req = async (opt) => {
    last = (opt.params ?? undefined) as Record<string, unknown> | undefined;
    return { data };
  };
  return { req, lastParams: () => last };
}

describe('注册中心 · toRegistryRows 字段归一与防御', () => {
  it('实例数=Providers,消费者数=Consumers,健康反映 Enable;DisplayName 优先', () => {
    const rows = toRegistryRows([
      { Id: 1, Name: 'order', DisplayName: '订单服务', Category: 'biz', Providers: 8, Consumers: 3, Enable: true, HealthCheck: true },
      { Id: 2, Name: 'offline', Providers: 0, Consumers: 0, Enable: false, HealthCheck: false },
    ]);
    expect(rows).toHaveLength(2);
    expect(rows[0]).toMatchObject({ id: 1, name: '订单服务', serviceName: 'order', category: 'biz', instances: 8, consumers: 3, enable: true, healthCheck: true });
    expect(rows[1].name).toBe('offline'); // 无 DisplayName 兜底 Name
    expect(rows[1].enable).toBe(false);
  });

  it('空数组与缺字段不抛错(数字缺省 0,布尔缺省 false)', () => {
    expect(toRegistryRows([])).toEqual([]);
    const rows = toRegistryRows([{ Id: 9 }]);
    expect(rows[0]).toMatchObject({ instances: 0, consumers: 0, enable: false, healthCheck: false, name: '' });
  });
});

describe('网关 · ClusterId→集群名映射与 TLS 判定', () => {
  it('buildClusterMap:Id→Name,跳过 id<=0', () => {
    const m = buildClusterMap([{ Id: 11, Name: 'DeviceService' }, { Id: 22, Name: 'OrderService' }, { Id: 0, Name: 'Bad' }]);
    expect(m.get(11)).toBe('DeviceService');
    expect(m.get(22)).toBe('OrderService');
    expect(m.has(0)).toBe(false);
  });

  it('certDomainMatches:精确与通配', () => {
    expect(certDomainMatches('api.example.com', 'api.example.com')).toBe(true);
    expect(certDomainMatches('a.example.com', '*.example.com')).toBe(true);
    expect(certDomainMatches('example.com', '*.example.com')).toBe(true);
    expect(certDomainMatches('api.other.com', '*.example.com')).toBe(false);
    expect(certDomainMatches('', '*.example.com')).toBe(false);
  });

  it('matchCert:精确优先于通配;无启用证书 present=false', () => {
    const certs = [
      { Domain: 'api.example.com', Enable: true, AutoRenew: true },
      { Domain: '*.example.com', Enable: true, AutoRenew: false },
      { Domain: 'old.com', Enable: false, AutoRenew: true },
    ];
    const hit = matchCert('api.example.com', certs);
    expect(hit.present).toBe(true);
    expect(hit.domain).toBe('api.example.com');
    expect(hit.autoRenew).toBe(true);
    const wild = matchCert('dev.example.com', certs);
    expect(wild.present).toBe(true);
    expect(wild.domain).toBe('*.example.com');
    expect(matchCert('nope.com', certs).present).toBe(false);
  });

  it('toGatewayRoutes:Methods 空/有值、ClusterId 查不到集群名留空、TLS 按域名证书', () => {
    const clusterMap = buildClusterMap([{ Id: 11, Name: 'DeviceService' }]);
    const certs = [{ Domain: 'iot.example.com', Enable: true, AutoRenew: true }];
    const rows = toGatewayRoutes(
      [
        { Id: 1, Name: 'iot-api', Domain: 'iot.example.com', Path: '/api/device/**', Methods: 'GET,POST', ClusterId: 11, Enable: true, IsStaticRoute: false },
        { Id: 2, Name: 'web', Domain: 'spa.example.com', Path: '/', Methods: '', ClusterId: 999, Enable: true, IsStaticRoute: true },
      ],
      clusterMap,
      certs,
    );
    expect(rows[0].clusterName).toBe('DeviceService');
    expect(rows[0].methods).toBe('GET,POST');
    expect(rows[0].tls.present).toBe(true);
    expect(rows[1].clusterName).toBe(''); // 999 查不到 → 组件显示待接入
    expect(rows[1].methods).toBe('');
    expect(rows[1].isStatic).toBe(true);
    expect(rows[1].tls.present).toBe(false);
  });
});

describe('AI/MCP · 令牌掩码与授权范围', () => {
  it('maskToken:保留下划线前缀后打码;无前缀打码前 4 位;空→—', () => {
    expect(maskToken('sdmcp_abcdef123456')).toBe('sdmcp_****');
    expect(maskToken('sk-abcdef')).toBe('sk-a****');
    expect(maskToken('abcd')).toBe('****');
    expect(maskToken('')).toBe('—');
    expect(maskToken(null)).toBe('—');
  });

  it('buildScopeMap:IsAll→全部资源,否则去重资源类型', () => {
    const m = buildScopeMap([
      { TokenId: 1, ResourceType: 'Project', IsAll: true },
      { TokenId: 2, ResourceType: 'Node', IsAll: false },
      { TokenId: 2, ResourceType: 'App', IsAll: false },
      { TokenId: 2, ResourceType: 'App', IsAll: false }, // 重复去重
    ]);
    expect(m.get(1)).toBe('全部资源');
    expect(m.get(2)).toBe('节点 + 应用');
  });

  it('toMcpTokens:掩码、过期时间、调用次数;无过期→永不过期;无 scope→—', () => {
    const scopeMap = buildScopeMap([{ TokenId: 1, ResourceType: 'App', IsAll: false }]);
    const rows = toMcpTokens(
      [
        { Id: 1, Name: 'claude-ops', Token: 'sdmcp_zzzz', Enable: true, CallCount: 42, LastTime: '2026-10-29 10:00:00' },
        { Id: 2, Name: 'ci-bot', Token: 'sdmcp_yyyy', Enable: false, ExpireTime: '2026-12-31 23:59:59', CallCount: 0 },
      ],
      scopeMap,
    );
    expect(rows[0]).toMatchObject({ name: 'claude-ops', masked: 'sdmcp_****', enable: true, callCount: 42, scopeText: '应用', expireText: '永不过期' });
    expect(rows[0].lastText).toContain('2026-10-29');
    expect(rows[1].expireText).toContain('2026-12-31');
    expect(rows[1].scopeText).toBe('—');
  });

  it('toMcpAudits:工具名+动作名合并、成功/失败点、倒序截断', () => {
    const rows = toMcpAudits(
      [
        { Id: 1, ToolName: 'list_actions', Success: true, CallerIp: '10.0.0.5', CreateTime: '2026-10-29 09:00:00' },
        { Id: 2, ToolName: 'invoke_action', ActionName: 'node_send_command', Success: false, ErrorMessage: '被安全策略拦截', CreateTime: '2026-10-29 11:00:00' },
      ],
    );
    expect(rows[0].tool).toBe('invoke_action · node_send_command');
    expect(rows[0].ok).toBe(false);
    expect(rows[0].detail).toBe('被安全策略拦截');
    expect(rows[1].detail).toBe('10.0.0.5');
    // 最新的 11:00 排第一
    expect(rows[0].time).toContain('11:00');
  });
});

describe('平台设置 · 计数与磁贴归一', () => {
  it('countCsv:逗号分隔计数,空串为 0', () => {
    expect(countCsv('1.1.1.1, 2.2.2.2')).toBe(2);
    expect(countCsv('')).toBe(0);
    expect(countCsv(null)).toBe(0);
  });

  it('pickProject:按 projectId 精确匹配,否则首行', () => {
    const rows = [{ Id: 1, Name: 'A' }, { Id: 2, Name: 'B' }];
    expect(pickProject(rows, 2)?.Name).toBe('B');
    expect(pickProject(rows, 99)?.Name).toBe('A'); // 找不到回退首行
    expect(pickProject(rows, null)?.Name).toBe('A');
  });

  it('buildTiles:成员数=ProjectUser 行数,项目数=GalaxyProject 行数,节点/应用/防火墙取 GalaxyProject;数据保留/运行时/级联取 StarServer;缺的磁贴 pending 且写明原因', () => {
    const tiles = buildTiles(
      [{ Id: 7, Name: 'prod', Nodes: 5, Apps: 12, WhiteIPs: '10.0.0.1,10.0.0.2', BlackIPs: '' }],
      [{ UserId: 1 }, { UserId: 2 }, { UserId: 3 }],
      { DataRetention: 3, DataRetention2: 30, DataRetention3: 300, DotNetSyncPeriod: 43200, UplinkServer: '' },
      7,
    );
    const byKey = Object.fromEntries(tiles.map((t) => [t.key, t]));
    expect(byKey.members.detail).toBe('3 位成员 · 1 个项目 · 5 节点 / 12 应用');
    expect(byKey.firewall.detail).toBe('白名单 2 条 · 黑名单 0 条');
    expect(byKey.retention.detail).toBe('明细 3 天 · 小时 30 天 · 历史 300 天');
    expect(byKey.dotnet.detail).toBe('每 12h 同步官方版本');
    expect(byKey.cascade.detail).toBe('未配置上级服务器');
    // 取不到真实值的磁贴标 pending,detail 不再只写「待接入」,而是诚实说明
    expect(byKey.sso.status).toBe('pending');
    expect(byKey.ddns.status).toBe('pending');
    expect(byKey.audit.status).toBe('pending');
    expect(byKey.sso.detail).toBe('控制台暂未开放配置入口,请到后端管理后台配置');
    expect(byKey.ddns.detail).toBe('控制台暂未开放配置入口,请到后端管理后台配置');
    expect(byKey.audit.detail).toBe('MCP 调用审计见「AI / MCP」页签');
  });

  it('buildTiles:StarServerSetting 为空时真实磁贴退化为 0,pending 磁贴带诚实说明', () => {
    const tiles = buildTiles([], [], undefined, null);
    const byKey = Object.fromEntries(tiles.map((t) => [t.key, t]));
    expect(byKey.members.detail).toBe('0 位成员 · 0 个项目 · 0 节点 / 0 应用');
    expect(byKey.dotnet.detail).toBe('自动同步已禁用');
    expect(byKey.retention.detail).toBe('明细 0 天 · 小时 0 天 · 历史 0 天');
    expect(byKey.sso.detail).not.toBe('待接入');
  });
});

describe('projectId 透传(请求注入断言)', () => {
  it('四个 fetch 函数在 projectId 非空时把它放进 params', async () => {
    const seen: Record<string, Record<string, unknown>> = {};
    const req: Req = async (opt) => {
      seen[String(opt.url)] = { ...(opt.params as Record<string, unknown>) };
      return { data: [] };
    };
    await fetchRegistry(req, 42);
    await fetchGateway(req, 42);
    await fetchMcp(req, 42);
    await fetchPlatform(req, 42);
    expect(seen['/Registry/Service'].projectId).toBe(42);
    expect(seen['/Gateway/GatewayRoute'].projectId).toBe(42);
    expect(seen['/Platform/McpToken'].projectId).toBe(42);
    expect(seen['/Platform/GalaxyProject'].projectId).toBe(42);
  });

  it('projectId 为空(全部项目)时 params 不带 projectId', async () => {
    const seen: Record<string, Record<string, unknown>> = {};
    const req: Req = async (opt) => {
      seen[String(opt.url)] = { ...(opt.params as Record<string, unknown>) };
      return { data: [] };
    };
    await fetchRegistry(req, null);
    expect(seen['/Registry/Service'].projectId).toBeUndefined();
  });
});

describe('单 tab 请求失败隔离', () => {
  it('网关 tab 失败只返回自身 error,不影响其它 tab 的成功取数', async () => {
    // 各 tab 独立 fetch:让 /Gateway/* 抛错,注册中心/MCP/平台正常
    const gw = await fetchGateway(stubReq({}, ['/Gateway/GatewayRoute', '/Gateway/GatewayCluster', '/Gateway/GatewayCert']), 1);
    expect(gw.rows).toEqual([]);
    expect(gw.error).not.toBe(''); // 失败透传真实错误信息,不静默空态

    const reg = await fetchRegistry(stubReq({ '/Registry/Service': [{ Id: 1, Name: 'ok', Providers: 1 }] }), 1);
    expect(reg.error).toBe('');
    expect(reg.rows).toHaveLength(1);

    const mcp = await fetchMcp(stubReq({ '/Platform/McpToken': [], '/Platform/McpTokenResource': [], '/Platform/McpAudit': [] }), 1);
    expect(mcp.error).toBe('');
    expect(mcp.tokens).toEqual([]);

    const plat = await fetchPlatform(
      stubReq({
        '/Platform/GalaxyProject': [],
        '/Platform/ProjectUser': [],
        '/Platform/StarServer': { DotNetSyncPeriod: 0 },
      }),
      1,
    );
    expect(plat.error).toBe('');
    expect(plat.tiles.length).toBe(8);
  });
});

describe('MCP 服务器 · fetchMcpServers', () => {
  it('后端请求失败时返回空 rows + 通用失败文案,不抛错', async () => {
    const req: Req = async () => {
      throw new Error('Request failed with status 404');
    };
    const r = await fetchMcpServers(req, 1);
    expect(r.rows).toEqual([]);
    expect(r.error).toBe('MCP 服务器列表加载失败');
  });

  it('接口返回列表时正常归一(名称/URL/状态/工具数)', async () => {
    const req: Req = async () => ({
      data: [
        { Id: 1, Name: 'filesystem', Url: 'http://localhost:8100', Enable: true, ToolCount: 7 },
        { Id: 2, Name: 'git', Address: 'http://localhost:8200', Enable: false },
      ],
    });
    const r = await fetchMcpServers(req, 1);
    expect(r.error).toBe('');
    expect(r.rows).toHaveLength(2);
    expect(r.rows[0]).toMatchObject({ name: 'filesystem', url: 'http://localhost:8100', enable: true, tools: 7 });
    expect(r.rows[1].url).toBe('http://localhost:8200'); // Address 别名兜底
    expect(r.rows[1].tools).toBe(0);
  });

  it('opts 查询:enable/dtStart/dtEnd/Q 透传 + PageSize 500(CARD-PLT-03 定稿裁定 1)', async () => {
    const { req, lastParams } = recordReq([{ Id: 1, Name: 'fs', Enable: true }]);
    const r = await fetchMcpServers(req, { projectId: 7, enable: false, dtStart: '2026-11-01', dtEnd: '2026-11-09', Q: 'node' });
    expect(r.error).toBe('');
    expect(lastParams()).toEqual({ PageSize: 500, projectId: 7, enable: false, dtStart: '2026-11-01', dtEnd: '2026-11-09', Q: 'node' });
    expect(r.rows).toHaveLength(1);
  });

  it('projectId>0 才带参(定稿裁定 3);空串/空白条件不下发', async () => {
    const { req, lastParams } = recordReq([]);
    const r = await fetchMcpServers(req, { projectId: 0, enable: undefined, dtStart: '', dtEnd: ' ', Q: '' });
    expect(r.error).toBe('');
    expect(lastParams()).toEqual({ PageSize: 500 });
    expect('projectId' in (lastParams() ?? {})).toBe(false);
  });

  it('buildMcpServerQueryParams:enable true/false 都透传,undefined 不发', () => {
    expect(buildMcpServerQueryParams({ enable: true })).toEqual({ enable: true });
    expect(buildMcpServerQueryParams({ enable: false })).toEqual({ enable: false });
    expect(buildMcpServerQueryParams({ enable: undefined, projectId: 5 })).toEqual({ projectId: 5 });
  });

  it('旧 number 签名兼容:projectId 透传', async () => {
    const { req, lastParams } = recordReq([]);
    const r = await fetchMcpServers(req, 3);
    expect(r.error).toBe('');
    expect(lastParams()).toEqual({ PageSize: 500, projectId: 3 });
  });
});

describe('成员管理 · toMembers 归一与项目信息', () => {
  it('toMembers:有 Name 用用户名,无名字段退化为 #UserId;Role/Kind 归一;缺省角色为「成员」', () => {
    const rows = toMembers([
      { Id: 11, UserId: 101, Name: '何炳宏', Role: '管理员', CreateTime: '2026-08-01 09:30:00' },
      { Id: 12, UserId: 202, Kind: '开发', CreateTime: '2026-09-15 14:00:00' },
      { Id: 13, UserId: 303 },
    ]);
    expect(rows[0]).toMatchObject({ id: 11, userId: 101, name: '何炳宏', role: '管理员' });
    expect(rows[0].joinedText).toContain('2026-08-01');
    expect(rows[1].name).toBe('#202'); // ProjectUser 不 join User 表 → 诚实降级
    expect(rows[1].role).toBe('开发');
    expect(rows[2].role).toBe('成员');
    expect(rows[2].joinedText).toBe('—');
    expect(rows[2].enable).toBe(true);
  });

  it('toMembers:UserName 优先,Enable=false 记为停用', () => {
    const rows = toMembers([
      { Id: 1, UserId: 2, UserName: '何炳宏', Name: '忽略', Enable: false },
    ]);
    expect(rows[0].name).toBe('何炳宏');
    expect(rows[0].enable).toBe(false);
  });

  it('toMembers:空数组返回空,不抛错', () => {
    expect(toMembers([])).toEqual([]);
  });

  it('toProjectInfo:按 projectId 精确选项目,环境/创建时间归一', () => {
    const info = toProjectInfo(
      [
        { Id: 1, Name: 'dev', Environment: '测试', CreateTime: '2026-01-01 10:00:00' },
        { Id: 2, Name: 'prod' },
      ],
      2,
    );
    expect(info).not.toBeNull();
    expect(info?.name).toBe('prod');
    expect(info?.environment).toBe('—'); // 缺字段诚实显示 —
    expect(info?.createdText).toBe('—');
  });

  it('toProjectInfo:projectId 不匹配回退首行;空数组返回 null', () => {
    const fallback = toProjectInfo([{ Id: 9, Name: 'only' }], 999);
    expect(fallback?.name).toBe('only');
    expect(toProjectInfo([], null)).toBeNull();
  });

  it('fetchPlatform:成员列表与项目信息随 tiles 一起返回', async () => {
    const plat = await fetchPlatform(
      stubReq({
        '/Platform/GalaxyProject': [{ Id: 7, Name: 'prod', Environment: '生产', CreateTime: '2026-01-01 00:00:00' }],
        '/Platform/ProjectUser': [{ Id: 1, UserId: 101, Name: '何炳宏', Role: '管理员' }, { Id: 2, UserId: 202, Kind: '开发' }],
        '/Platform/StarServer': {},
      }),
      7,
    );
    expect(plat.error).toBe('');
    expect(plat.members).toHaveLength(2);
    expect(plat.members[0].name).toBe('何炳宏');
    expect(plat.members[1].name).toBe('#202');
    expect(plat.projectInfo?.name).toBe('prod');
    expect(plat.projectInfo?.environment).toBe('生产');
  });
});

// ==================== F13 服务详情(Service + 提供者/消费者 + 编辑) ====================

describe('F13 toServiceMembers / toServiceEditModel', () => {
  it('提供者行:Client 兜底 #AppId,带健康与版本', () => {
    const rows = toServiceMembers([
      { Id: 1, Client: 'StarWeb', Address: 'http://10.0.0.1:8080', Enable: true, Healthy: true, Version: 'v1.2', UpdateTime: '2026-11-03T10:00:00' },
      { Id: 2, AppId: 9, Address: '', Enable: false },
    ], 'provider');
    expect(rows[0]).toMatchObject({ client: 'StarWeb', address: 'http://10.0.0.1:8080', enable: true, healthy: true, version: 'v1.2' });
    expect(rows[1].client).toBe('#9');
    expect(rows[1].healthy).toBe(false); // provider 行缺 Healthy 归一为 false
    expect(rows[0].updateTimeText).toBe('2026-11-03 10:00');
  });

  it('消费者行没有健康/版本字段', () => {
    const rows = toServiceMembers([{ Id: 3, Client: 'Iot', Address: 'x', Enable: true }], 'consumer');
    expect(rows[0]).not.toHaveProperty('healthy');
    expect(rows[0]).not.toHaveProperty('version');
  });

  it('编辑表单归一大小写;UseScope 是布尔', () => {
    // 后端实体的 UseScope 是 Boolean;字符串 "prod"/"false" 一律归一为布尔
    const m = toServiceEditModel({ DisplayName: '网关', Category: 'Web', Enable: true, Extranet: false, Singleton: true, UseScope: true, Address: 'tcp://x', HealthCheck: true, Remark: 'r' });
    expect(m).toEqual({ name: '', displayName: '网关', category: 'Web', enable: true, extranet: false, singleton: true, useScope: true, address: 'tcp://x', healthCheck: true, remark: 'r' });
    // 字符串形态(历史数据/JSON)也归一为布尔,不透传字符串
    const m2 = toServiceEditModel({ DisplayName: 'x', UseScope: 'false' });
    expect(m2.useScope).toBe(false);
    const m3 = toServiceEditModel({ DisplayName: 'x', UseScope: 'true' });
    expect(m3.useScope).toBe(true);
  });

  it('PUT body 全字段且布尔字段全为 Boolean 类型;显示名必填校验', () => {
    const m = toServiceEditModel({ DisplayName: ' 网关 ', Category: 'Web', Enable: true, Extranet: false, Singleton: false, UseScope: true, Address: 'tcp://x ', HealthCheck: true, Remark: '' });
    const body = servicePutBody(m);
    expect(body).toEqual({ DisplayName: '网关', Category: 'Web', Enable: true, Extranet: false, Singleton: false, UseScope: true, Address: 'tcp://x', HealthCheck: true, Remark: '' });
    // Enable/Extranet/Singleton/UseScope/HealthCheck 必须全是 boolean,不得是字符串
    for (const k of ['Enable', 'Extranet', 'Singleton', 'UseScope', 'HealthCheck']) {
      expect(typeof body[k]).toBe('boolean');
    }
    expect(validateServiceEdit(m)).toBe('');
    expect(validateServiceEdit({ ...m, displayName: ' ' })).toContain('å¿…å¡«');
  });

  it('REG-09 新建默认值:仅单例默认勾选,其余字段空/false(Biz.cs:45-52)', () => {
    expect(emptyServiceEditModel()).toEqual({
      name: '', displayName: '', category: '', enable: false, extranet: false,
      singleton: true, useScope: false, address: '', healthCheck: false, remark: '',
    });
  });

  it('REG-09 POST body:PUT 字段 + Name;新建态校验先服务名后显示名', () => {
    const m = toServiceEditModel({ Name: ' star.web ', DisplayName: ' 星尘 ', Category: 'Web', Enable: true, Singleton: true, UseScope: false });
    const body = servicePostBody(m);
    expect(body).toMatchObject({ Name: 'star.web', DisplayName: '星尘' });
    expect(typeof body.Singleton).toBe('boolean');
    // 编辑态(默认):Name 不参与校验(主键语义不改)
    expect(validateServiceEdit({ ...m, name: '' })).toBe('');
    // 新建态:服务名必填(Biz.cs:39 Valid),空显示名次之
    expect(validateServiceEdit({ ...m, name: ' ' }, true)).toBe('服务名必填');
    expect(validateServiceEdit({ ...m, displayName: '' }, true)).toBe('服务显示名必填');
    expect(validateServiceEdit(m, true)).toBe('');
  });
});

describe('F13 fetchServiceDetail / saveService', () => {
  const mk = () => {
    const calls: unknown[] = [];
    const req: Req = async (opt) => {
      calls.push(opt);
      const url = String(opt.url ?? '');
      if (url === '/Registry/Service/7') return { code: 0, data: { Id: 7, Name: 'Star.Web', DisplayName: '星尘' } } as Awaited<ReturnType<Req>>;
      if (url === '/Registry/AppService') return { code: 0, data: [{ Id: 1, Client: 'StarWeb', Address: 'a', Enable: true, Healthy: true }] } as Awaited<ReturnType<Req>>;
      if (url === '/Registry/AppConsume') return { code: 0, data: [{ Id: 2, Client: 'Iot', Address: 'b', Enable: false }] } as Awaited<ReturnType<Req>>;
      throw new Error(`stub: ${url}`);
    };
    return { req, calls };
  };

  it('三请求并行:详情+提供者+消费者,serviceId 随 params', async () => {
    const { req, calls } = mk();
    const d = await fetchServiceDetail(req, 7);
    expect(d?.id).toBe(7);
    expect(d?.name).toBe('Star.Web');
    expect(d?.service?.displayName).toBe('星尘');
    expect(d?.providers).toHaveLength(1);
    expect(d?.consumers).toHaveLength(1);
    expect(calls[1]).toMatchObject({ url: '/Registry/AppService', params: { serviceId: 7 } });
    expect(calls[2]).toMatchObject({ url: '/Registry/AppConsume', params: { serviceId: 7 } });
  });

  it('serviceId<=0 / 整体失败 → null', async () => {
    const { req } = mk();
    expect(await fetchServiceDetail(req, 0)).toBeNull();
    const bad: Req = async () => {
      throw new Error('x');
    };
    expect(await fetchServiceDetail(bad, 7)).toBeNull();
  });

  it('saveService:PUT /Registry/Service/{id},成功/失败/缺 id', async () => {
    const calls: unknown[] = [];
    const req: Req = async (opt) => {
      calls.push(opt);
      return { code: 0, message: '操作成功' } as Awaited<ReturnType<Req>>;
    };
    const m = toServiceEditModel({ DisplayName: 'x' });
    expect((await saveService(req, 7, m)).ok).toBe(true);
    expect(calls[0]).toMatchObject({ url: '/Registry/Service/7', method: 'PUT' });
    const noId = async () => {
      throw new Error('stub');
    };
    expect((await saveService(noId as Req, 0, m)).ok).toBe(false);
  });

  it('REG-09 createService:POST /Registry/Service 带 Name,成功/失败/空名拦截', async () => {
    const calls: unknown[] = [];
    const req: Req = async (opt) => {
      calls.push(opt);
      return { code: 0, message: '操作成功' } as Awaited<ReturnType<Req>>;
    };
    const ok = await createService(req, emptyServiceEditModel());
    // 空名在本地拦截,不发请求
    expect(ok.ok).toBe(false);
    expect(calls).toHaveLength(0);
    const m = toServiceEditModel({ Name: 'star.web', DisplayName: '星尘', Singleton: true });
    const r = await createService(req, m);
    expect(r).toEqual({ ok: true, message: '服务已创建' });
    expect(calls[0]).toMatchObject({ url: '/Registry/Service', method: 'POST' });
    expect((calls[0] as { data: Record<string, unknown> }).data).toMatchObject({ Name: 'star.web', DisplayName: '星尘', Singleton: true });
    const bad: Req = async () => {
      throw new Error('后端校验失败');
    };
    const fail = await createService(bad, m);
    expect(fail).toEqual({ ok: false, message: '后端校验失败' });
  });
});