|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { DuckDBInstance } from "@duckdb/node-api"; |
| 6 | +import { runQuery } from "../../src/query/runQuery.js"; |
| 7 | + |
| 8 | +// A snapshot shaped like rindexer's export: block_timestamp is TIMESTAMP WITH |
| 9 | +// TIME ZONE. Written by this test process, not the worker, which has external |
| 10 | +// access switched off before any project SQL runs. |
| 11 | +async function timestampFixture(): Promise<string> { |
| 12 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "chainplot-tz-")); |
| 13 | + const parquet = path.join(dir, "events.parquet"); |
| 14 | + const instance = await DuckDBInstance.create(":memory:"); |
| 15 | + const conn = await instance.connect(); |
| 16 | + await conn.run( |
| 17 | + `COPY ( |
| 18 | + SELECT * FROM (VALUES |
| 19 | + (1, TIMESTAMPTZ '2026-09-16 00:26:20+00'), |
| 20 | + (2, TIMESTAMPTZ '2026-09-16 23:59:59+00') |
| 21 | + ) AS t(rindexer_id, block_timestamp) |
| 22 | + ) TO '${parquet.replaceAll("'", "''")}' (FORMAT PARQUET)`, |
| 23 | + ); |
| 24 | + return parquet; |
| 25 | +} |
| 26 | + |
| 27 | +// DuckDB formats, casts and buckets TIMESTAMPTZ in the session TimeZone, which |
| 28 | +// follows the machine unless pinned: the same query would publish "08:00" from |
| 29 | +// a laptop in Singapore and "00:00" from the producer container. The worker |
| 30 | +// pins UTC so a fork reproduces the published figures wherever it runs. |
| 31 | +describe("query session timezone", () => { |
| 32 | + // TimeZone is a setting DuckDB's ICU extension registers, so reading it |
| 33 | + // back also proves the bundled build links ICU: without it the worker's |
| 34 | + // SET would have failed before this query ran. (duckdb_extensions() is not |
| 35 | + // usable here: it scans the extension directory, which the worker's |
| 36 | + // external-access lockdown forbids.) |
| 37 | + it("is pinned to UTC by the ICU-backed setting", async () => { |
| 38 | + const parquet = await timestampFixture(); |
| 39 | + const result = await runQuery({ |
| 40 | + sql: "SELECT current_setting('TimeZone') AS tz FROM (SELECT 1)", |
| 41 | + tables: { events: parquet }, |
| 42 | + rawAmountColumns: [], |
| 43 | + rowLimit: 10, |
| 44 | + }); |
| 45 | + expect(result.rows).toEqual([["UTC"]]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("renders, buckets and casts a TIMESTAMPTZ column in UTC", async () => { |
| 49 | + const parquet = await timestampFixture(); |
| 50 | + const result = await runQuery({ |
| 51 | + sql: `SELECT typeof(block_timestamp) AS t, |
| 52 | + strftime(block_timestamp, '%Y-%m-%d %H:%M:%S') AS rendered, |
| 53 | + strftime(date_trunc('day', block_timestamp), '%Y-%m-%d') AS day, |
| 54 | + hour(block_timestamp) AS h, |
| 55 | + strftime(block_timestamp::TIMESTAMP, '%H:%M') AS cast_naive |
| 56 | + FROM events ORDER BY rindexer_id`, |
| 57 | + tables: { events: parquet }, |
| 58 | + rawAmountColumns: [], |
| 59 | + rowLimit: 10, |
| 60 | + }); |
| 61 | + expect(result.rows).toEqual([ |
| 62 | + // hour() is BIGINT, which the worker serializes as a decimal string. |
| 63 | + ["TIMESTAMP WITH TIME ZONE", "2026-09-16 00:26:20", "2026-09-16", "0", "00:26"], |
| 64 | + ["TIMESTAMP WITH TIME ZONE", "2026-09-16 23:59:59", "2026-09-16", "23", "23:59"], |
| 65 | + ]); |
| 66 | + }); |
| 67 | +}); |
0 commit comments