import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; import { JSON_EDITOR_MAX_BYTES, importEventReader, importHttpErrorDetail, jsonEditorFits, useGraphFileTransfer } from "../src/graphFileUpload.ts"; test("280 MB and larger files select file transfer without reading content", () => { for (const size of [287527160, 1024 ** 3, 10 * 1024 ** 3]) { const file = { size, text() { throw new Error("large file must not be read"); } }; assert.equal(useGraphFileTransfer(file), true); } assert.equal(useGraphFileTransfer({ size: JSON_EDITOR_MAX_BYTES }), false); }); test("editor limits account for UTF-8 bytes, not only characters", () => { assert.equal(jsonEditorFits("a".repeat(JSON_EDITOR_MAX_BYTES)), true); assert.equal(jsonEditorFits("a".repeat(JSON_EDITOR_MAX_BYTES + 1)), false); assert.equal(jsonEditorFits("中".repeat(800000)), false); }); test("HTTP errors are readable for both JSON and streaming text responses", () => { const detail = { message: "磁盘空间不足", errors: ["未接收文件"] }; assert.deepEqual(importHttpErrorDetail({ response: { data: { detail } } }), detail); assert.deepEqual(importHttpErrorDetail({ response: { data: JSON.stringify({ detail }) } }), detail); assert.equal(importHttpErrorDetail({ response: { data: 'proxy error' } }), undefined); assert.equal(importHttpErrorDetail({ response: { data: 'x'.repeat(65537) } }), undefined); assert.equal(importHttpErrorDetail(null), undefined); }); test("partial progress lines parse once and result is not confused with progress", () => { const events: string[] = []; const read = importEventReader((item) => events.push(item.event)); const progress = JSON.stringify({ event: "progress", data: { phase: "校验", nodes: 1 } }) + "\n"; const result = JSON.stringify({ event: "result", data: { project: { project_id: "test" } } }) + "\n"; read(progress.slice(0, 12)); assert.deepEqual(events, []); read(progress); read(progress + result.slice(0, 6)); read(progress + result); read(progress + result); assert.deepEqual(events, ["progress", "result"]); }); test("wizard bypasses full text parsing for large files and disables formatting", () => { const source = readFileSync(new URL("../src/ProjectLanding.tsx", import.meta.url), "utf8"); const start = source.indexOf("const loadJsonFile ="); const load = source.slice(start, source.indexOf("const formatJson", start)); assert.ok(load.indexOf("if (useGraphFileTransfer(file))") < load.indexOf("await file.text()")); assert.ok(load.indexOf("setGraphFile(file)") < load.indexOf("await file.text()")); assert.doesNotMatch(load, /JSON\.stringify/); assert.match(source, /disabled=\{Boolean\(graphFile\) \|\| loadingFile\}/); assert.match(source, /provisionGraphProjectFile\(metadata, graphFile/); });