mirror of
https://github.com/arshiaecho/obsidian-granola-api-sync.git
synced 2026-07-22 08:31:27 +00:00
61 lines
2.5 KiB
JavaScript
61 lines
2.5 KiB
JavaScript
// Parity + unit test for the pure formatting helpers.
|
|
// Bundles src/main.ts with an obsidian shim, feeds it a real captured API
|
|
// response, and (when a reference dir is given) diffs against the Python
|
|
// service's output byte-for-byte.
|
|
import { execFileSync } from "child_process";
|
|
import { readFileSync, writeFileSync, mkdirSync } from "fs";
|
|
import { dirname, join } from "path";
|
|
import { fileURLToPath } from "url";
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const root = join(here, "..");
|
|
|
|
// build a test bundle with obsidian stubbed out
|
|
writeFileSync(join(here, "obsidian-shim.mjs"),
|
|
"export const Plugin=class{};export const PluginSettingTab=class{};" +
|
|
"export const Notice=class{};export const Setting=class{};" +
|
|
"export const TFile=class{};export const TFolder=class{};" +
|
|
"export const normalizePath=(p)=>p;export const requestUrl=()=>{};\n");
|
|
execFileSync(
|
|
"npx",
|
|
[
|
|
"esbuild", "src/main.ts", "--bundle", "--format=esm",
|
|
"--outfile=test/_bundle.mjs",
|
|
"--alias:obsidian=./test/obsidian-shim.mjs",
|
|
],
|
|
{ cwd: root, stdio: "pipe" }
|
|
);
|
|
|
|
const { buildNoteMd, buildTranscriptMd, sanitizeFilename } = await import(
|
|
join(here, "_bundle.mjs")
|
|
);
|
|
|
|
// --- unit checks ---
|
|
const assert = (cond, msg) => {
|
|
if (!cond) { console.error(`FAIL: ${msg}`); process.exit(1); }
|
|
console.log(`ok: ${msg}`);
|
|
};
|
|
|
|
assert(sanitizeFilename("A / B: C?") === "A __ B C", "sanitize strips illegal chars");
|
|
assert(sanitizeFilename("...") === "Untitled Granola Note", "sanitize fallback");
|
|
|
|
// --- parity check against captured real API response ---
|
|
const fixturePath = process.argv[2];
|
|
if (fixturePath) {
|
|
// fixture: a raw GET /v1/notes/{id}?include=transcript response body
|
|
const fixture = JSON.parse(readFileSync(fixturePath, "utf-8"));
|
|
const settings = {
|
|
apiKey: "", notesFolder: "inbox/granola",
|
|
syncTranscripts: true, transcriptsFolder: "inbox/granola/transcripts",
|
|
intervalMinutes: 5, daysBack: 7,
|
|
myEmail: "arshia@stratosagency.ai", myLabel: "Arshia",
|
|
showNotices: true,
|
|
};
|
|
const uuid = fixture.web_url.match(/[0-9a-f-]{36}/)[0];
|
|
const fname = sanitizeFilename(fixture.title);
|
|
mkdirSync(join(here, "out"), { recursive: true });
|
|
writeFileSync(join(here, "out", "note.md"), buildNoteMd(fixture, uuid, fname, settings));
|
|
writeFileSync(join(here, "out", "transcript.md"), buildTranscriptMd(fixture, uuid, fname, settings));
|
|
console.log("parity outputs written to test/out/");
|
|
}
|
|
console.log("ALL TESTS PASSED");
|