multimodal-marker-png.test.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { crc32, inflateSync } from "node:zlib";
  4. import { renderMarkerPng } from "./multimodal-marker-png.mjs";
  5. import { buildCase, CASE_NAMES, hasOrderedMarkers } from "./multimodal-tool-result-p0.mjs";
  6. function decodeGrayscalePng(png) {
  7. assert.equal(png.subarray(0, 8).toString("hex"), "89504e470d0a1a0a");
  8. const chunks = [];
  9. for (let offset = 8; offset < png.length; ) {
  10. const size = png.readUInt32BE(offset);
  11. const body = png.subarray(offset + 4, offset + 8 + size);
  12. assert.equal(png.readUInt32BE(offset + 8 + size), crc32(body), "PNG chunk CRC");
  13. chunks.push({ type: body.subarray(0, 4).toString(), data: body.subarray(4) });
  14. offset += size + 12;
  15. assert.ok(offset <= png.length, "complete chunk");
  16. }
  17. assert.deepEqual(
  18. chunks.map(({ type }) => type),
  19. ["IHDR", "IDAT", "IEND"],
  20. );
  21. const header = chunks[0].data;
  22. assert.deepEqual([...header.subarray(8)], [8, 0, 0, 0, 0]);
  23. const width = header.readUInt32BE(0);
  24. const height = header.readUInt32BE(4);
  25. const pixels = inflateSync(chunks[1].data);
  26. assert.equal(pixels.length, (width + 1) * height);
  27. for (let y = 0; y < height; y++) assert.equal(pixels[y * (width + 1)], 0);
  28. return { width, height, pixels };
  29. }
  30. test("PNG stores visible marker pixels, preserves case and accommodates long markers", () => {
  31. const { width, height, pixels } = decodeGrayscalePng(renderMarkerPng("A_a012f"));
  32. assert.equal(height, 180);
  33. assert.ok(width >= 1000);
  34. // A 顶部留白、横线和左竖画:验证画的是字符,不是空图或仅含文本 metadata。
  35. const at = (x, y) => pixels[y * (width + 1) + 1 + x];
  36. assert.equal(at(30, 62), 255);
  37. assert.equal(at(38, 62), 0);
  38. assert.equal(at(30, 70), 0);
  39. assert.notDeepEqual(renderMarkerPng("A"), renderMarkerPng("a"));
  40. assert.notDeepEqual(renderMarkerPng("P0_VISUAL_012abc"), renderMarkerPng("P0_VISUAL_012abd"));
  41. const longest = decodeGrayscalePng(renderMarkerPng("W".repeat(128)));
  42. assert.ok(longest.width > 128 * 40, "last marker must not be clipped");
  43. });
  44. test("rejects unsupported marker characters and unbounded sizes", () => {
  45. for (const value of ["", "<svg>", "A B", "中文", "A".repeat(129), undefined]) {
  46. assert.throws(() => renderMarkerPng(value), /PNG marker/);
  47. }
  48. });
  49. test("all P0 tool-result shapes generate offline PNGs in their original marker order", async () => {
  50. let imageCount = 0;
  51. for (const name of CASE_NAMES) {
  52. const probe = await buildCase(name, "012abcdeff");
  53. const actual = [];
  54. for (const message of probe.messages) {
  55. for (const block of message.content) {
  56. if (block.type !== "tool_result") continue;
  57. for (const item of block.content) {
  58. if (item.type === "text") {
  59. actual.push(item.text);
  60. } else {
  61. assert.equal(item.type, "image");
  62. assert.equal(item.source.media_type, "image/png");
  63. const png = Buffer.from(item.source.data, "base64");
  64. decodeGrayscalePng(png);
  65. const marker = probe.expected.find((value) => renderMarkerPng(value).equals(png));
  66. assert.ok(marker, "image corresponds to an expected marker");
  67. actual.push(marker);
  68. imageCount++;
  69. }
  70. }
  71. }
  72. }
  73. assert.deepEqual(actual, probe.expected, name);
  74. assert.ok(hasOrderedMarkers(actual.join("\n"), probe.expected));
  75. }
  76. assert.equal(imageCount, 6);
  77. });