From faef3775114cfa161dbc45471828d2384d742791 Mon Sep 17 00:00:00 2001 From: Zade Viggers Date: Sun, 8 Oct 2023 12:26:22 +1300 Subject: [PATCH] Fix nanoseconds --- package.json | 2 +- src/miniseed.ts | 9 +++-- tests/serialise.test.ts | 2 +- tests/vaid-files.test.ts | 87 ++++++++++++++++++++++++++++++++++++++++ tests/vaid.test.ts | 38 ------------------ 5 files changed, 94 insertions(+), 44 deletions(-) create mode 100644 tests/vaid-files.test.ts delete mode 100644 tests/vaid.test.ts diff --git a/package.json b/package.json index 4e2b965..07fd92a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "miniseed", - "version": "0.1.5", + "version": "0.1.6", "type": "module", "scripts": { "build": "vite build", diff --git a/src/miniseed.ts b/src/miniseed.ts index 0e02bf0..d5ec66f 100644 --- a/src/miniseed.ts +++ b/src/miniseed.ts @@ -56,16 +56,17 @@ export function serialiseToMiniSEEDBuffer( let metadata = { ...metadataDefaults, ..._metadata }; if (metadata.startTime instanceof Date) { - // From https://stackoverflow.com/a/8619946 + if (isNaN(metadata.startTime.getTime())) + throw new Error("Invalid Date provided for metadata.timestamp"); metadata.startTime = { year: metadata.startTime.getFullYear(), dayOfYear: getDayOfYear(metadata.startTime), hour: metadata.startTime.getHours(), minute: metadata.startTime.getMinutes(), second: metadata.startTime.getSeconds(), - // Date's don't do nanoseconds :( - // One day we may get Temporal and all will be well - nanoSecond: 0, + // This isn't super precise. + // One day we may get Temporal and all will be well. + nanoSecond: metadata.startTime.getMilliseconds() * 1000000, }; } diff --git a/tests/serialise.test.ts b/tests/serialise.test.ts index e408eb1..b8368e7 100644 --- a/tests/serialise.test.ts +++ b/tests/serialise.test.ts @@ -62,7 +62,7 @@ describe("Size", () => { }); }); -describe("Serialisation", () => { +describe("Basic serialisation", () => { it("Works with basic text", () => { const serialised = serialiseToMiniSEEDBuffer("beans", { sourceIdentifier: "", diff --git a/tests/vaid-files.test.ts b/tests/vaid-files.test.ts new file mode 100644 index 0000000..6302d66 --- /dev/null +++ b/tests/vaid-files.test.ts @@ -0,0 +1,87 @@ +import { PromiseWithChild, exec as _exec } from "node:child_process"; +import { promisify } from "node:util"; +import { writeFile, mkdir, rm } from "node:fs/promises"; +import { join } from "node:path"; +import { describe, it, expect, beforeAll, afterAll } from "vitest"; +import { serialiseToMiniSEEDUint8Array } from "../src/miniseed"; + +const exec = promisify(_exec); + +const tempFolder = join(__dirname, "../test-temp/"); + +beforeAll(async () => { + await mkdir(tempFolder); +}); + +afterAll(async () => { + await rm(tempFolder, { recursive: true }); +}); + +async function checkData(data: Uint8Array, name: string) { + const file = join(tempFolder, `${name}.mseed`); + await writeFile(file, data); + let error = false; + let child: PromiseWithChild<{ stdout: string; stderr: string }>["child"]; + const promise = exec(`mseed3-validator ${file}`); + child = promise.child; + try { + const { stderr } = await promise; + expect(stderr).toBeFalsy(); + } catch (err) { + error = true; + const stdout = child.stdout?.read(); + const stderr = child.stderr?.read(); + console.error( + name + + ": " + + err + + (stdout + ? "\nstdout: " + new TextDecoder().decode(stdout) + : "") + + (stdout ? "\nstderr: " + new TextDecoder().decode(stderr) : "") + ); + } + expect(error).toBe(false); +} + +describe("Basic data validity", () => { + it("Works with simple data", async () => { + const data = [1, 2, 3, 4, 5, 6, 7, 8]; + const serialised = serialiseToMiniSEEDUint8Array(data, { + sourceIdentifier: "https://zade.viggers.net/example", + startTime: new Date(), + }); + await checkData(serialised, "1-2-3"); + }); +}); + +describe("Date validity", () => { + it("Works with a 0 date", async () => { + const serialised = serialiseToMiniSEEDUint8Array([], { + sourceIdentifier: "https://zade.viggers.net/example", + startTime: new Date(0), + }); + await checkData(serialised, "date-0-auto"); + }); + // it("Works with a manually set 0 date", async () => { + // const serialised = serialiseToMiniSEEDUint8Array([], { + // sourceIdentifier: "https://zade.viggers.net/example", + // startTime: { + // dayOfYear: 0, + // year: 0, + // hour: 0, + // minute: 0, + // second: 0, + // nanoSecond: 0, + // }, + // }); + // await checkData(serialised, "date-0-manual"); + // }); + it("Works with a new date right now", async () => { + const serialised = serialiseToMiniSEEDUint8Array([], { + sourceIdentifier: "https://zade.viggers.net/example", + startTime: new Date(), + }); + await checkData(serialised, "date-utc"); + }); +}); diff --git a/tests/vaid.test.ts b/tests/vaid.test.ts deleted file mode 100644 index c2455f0..0000000 --- a/tests/vaid.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { exec as _exec } from "node:child_process"; -import { promisify } from "node:util"; -import { writeFile, mkdir, rm } from "node:fs/promises"; -import { join } from "node:path"; -import { describe, it, expect, beforeAll, afterAll } from "vitest"; -import { serialiseToMiniSEEDUint8Array } from "../src/miniseed"; - -const exec = promisify(_exec); - -const tempFolder = join(__dirname, "../test-temp/"); - -beforeAll(async () => { - await mkdir(tempFolder); -}); - -afterAll(async () => { - await rm(tempFolder, { recursive: true }); -}); - -describe("Generated data validity", () => { - it("mseed3-validator", async () => { - const data = [1, 2, 3, 4, 5, 6, 7, 8]; - const serialised = serialiseToMiniSEEDUint8Array(data, { - startTime: new Date(), - sourceIdentifier: "https://zade.viggers.net/example", - }); - - const file = join(tempFolder, "obspy-1.mseed"); - await writeFile(file, serialised); - - try { - const { stderr, stdout } = await exec(`mseed3-validator ${file}`); - expect(stderr).toBeFalsy(); - } catch (err) { - throw err; - } - }); -});