/** * earth-now.co * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai * File: apps/ingest/test/archive.test.ts * Purpose: Raw archive tests — payload + sha256 checksum written under ${dir}/${sourceId}/, deterministic timestamps */ import { createHash } from "node:crypto"; import { mkdtempSync, readFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, sep } from "node:path"; import { describe, expect, it } from "vitest"; import { archiveRaw } from "../src/archive"; describe("archiveRaw", () => { it("writes the raw payload and a sha256 sidecar under dir/sourceId/", async () => { const dir = mkdtempSync(join(tmpdir(), "earth-now-archive-")); const payload = "year month value\n2024 1 422.80\n"; const now = new Date("2026-08-09T12:00:00.000Z"); const result = await archiveRaw("noaa_gml_mlo", payload, { dir, now }); expect(result.rawPath).toBe(join(dir, "noaa_gml_mlo", "2026-08-09T12-00-00-000Z.raw")); expect(result.checksumPath).toBe(`${result.rawPath}.sha256`); expect(readFileSync(result.rawPath, "utf8")).toBe(payload); const expectedSha = createHash("sha256").update(payload, "utf8").digest("hex"); expect(result.sha256).toBe(expectedSha); expect(readFileSync(result.checksumPath, "utf8")).toBe( `${expectedSha} 2026-08-09T12-00-00-000Z.raw\n`, ); }); it("accepts binary payloads (Uint8Array)", async () => { const dir = mkdtempSync(join(tmpdir(), "earth-now-archive-")); const payload = new Uint8Array([0, 1, 2, 254, 255]); const result = await archiveRaw("usgs_fdsn", payload, { dir }); expect(readFileSync(result.rawPath)).toEqual(Buffer.from(payload)); expect(result.rawPath.split(sep)).toContain("usgs_fdsn"); }); it("rejects path-unsafe source ids", async () => { await expect(archiveRaw("../evil", "x", { dir: tmpdir() })).rejects.toThrow(/invalid sourceId/); }); });