zcode-builtin-map-formatting.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { readFile } from "node:fs/promises";
  2. import { describe, expect, it } from "vitest";
  3. import { compileModelOptionMap, tokenizeRestrictedCel } from "@zcode/model-option-map";
  4. describe("F98-01 Built-in 源 Map 结构排版", () => {
  5. it("所有嵌套对象分层展开,简单三目同行,简单单层输出 Map 保持紧凑", async () => {
  6. const document = JSON.parse(
  7. await readFile(
  8. new URL("../../../config/provider/zcode-builtin.json", import.meta.url),
  9. "utf8",
  10. ),
  11. );
  12. let checked = 0;
  13. for (const rules of Object.values(document.config.modelConfigRules) as Array<
  14. Array<{
  15. config: { optionSpecs?: Record<string, { map?: string }> };
  16. }>
  17. >) {
  18. for (const rule of rules) {
  19. for (const [name, option] of Object.entries(rule.config.optionSpecs ?? {})) {
  20. const map = option.map;
  21. if (!map) continue;
  22. compileModelOptionMap(map, name as "reasoningLevel" | "maxOutputTokens");
  23. const tokens = tokenizeRestrictedCel(map);
  24. let depth = 0;
  25. let nested = false;
  26. for (const token of tokens) {
  27. if (token.value === "{") nested ||= ++depth > 1;
  28. if (token.value === "}") depth--;
  29. }
  30. if (nested) {
  31. checked++;
  32. for (const token of tokens.filter((t) => t.value === "{")) {
  33. expect(map.slice(token.end), map).toMatch(/^\n +\S/);
  34. }
  35. for (const line of map.split("\n"))
  36. expect(line.match(/^ */)![0].length % 2, map).toBe(0);
  37. }
  38. // 简单三目不拆行;结构分支由对象展开体现,不把规则变成一个 formatter。
  39. tokens.forEach((token, index) => {
  40. if (token.value !== "?" || tokens[index + 2]?.value !== ":") return;
  41. if (!["string", "identifier", "number"].includes(tokens[index + 1]!.kind)) return;
  42. expect(map.slice(token.offset, tokens[index + 3]!.end), map).not.toContain("\n");
  43. });
  44. if (name === "maxOutputTokens") expect(map).not.toContain("\n");
  45. }
  46. }
  47. }
  48. expect(checked).toBeGreaterThan(20);
  49. });
  50. });