Skip to content

Commit 4f6ef58

Browse files
Copilotbcho
andcommitted
Update test files to use Temporal types
Co-authored-by: bcho <1975118+bcho@users.noreply.github.com>
1 parent a4b7bbd commit 4f6ef58

7 files changed

Lines changed: 35 additions & 28 deletions

File tree

sdks/typescript/src/sqlite-connection.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,17 @@ function decodeColumnValue<V = any>(args: {
229229
}
230230

231231
if (columnTypeName === "datetime") {
232-
if (typeof value !== "number") {
233-
throw new Error(
234-
`Expected datetime column ${columnName} to be a number, got ${typeof value}`
235-
);
232+
if (typeof value === "string") {
233+
// Handle ISO string format
234+
return Temporal.Instant.from(value) as V;
235+
}
236+
if (typeof value === "number") {
237+
// Handle epoch milliseconds format
238+
return Temporal.Instant.fromEpochMilliseconds(value) as V;
236239
}
237-
return Temporal.Instant.fromEpochMilliseconds(value) as V;
240+
throw new Error(
241+
`Expected datetime column ${columnName} to be a string or number, got ${typeof value}`
242+
);
238243
}
239244

240245
// For other types, return as is

sdks/typescript/test/basic.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
vi,
99
} from "vitest";
1010
import { createTestAbsurd, randomName, type TestContext } from "./setup.js";
11-
import type { Absurd } from "../src/index.js";
11+
import { Temporal, type Absurd } from "../src/index.js";
1212
import { EventEmitter, once } from "events";
1313

1414
describe("Basic SDK Operations", () => {
@@ -170,7 +170,7 @@ describe("Basic SDK Operations", () => {
170170
const scheduledRun = await ctx.getRun(runID);
171171
expect(scheduledRun).toMatchObject({
172172
state: "sleeping",
173-
available_at: wakeAt,
173+
available_at: Temporal.Instant.fromEpochMilliseconds(wakeAt.getTime()),
174174
wake_event: null,
175175
});
176176

@@ -188,7 +188,7 @@ describe("Basic SDK Operations", () => {
188188
const resumedRun = await ctx.getRun(runID);
189189
expect(resumedRun).toMatchObject({
190190
state: "running",
191-
started_at: wakeAt,
191+
started_at: Temporal.Instant.fromEpochMilliseconds(wakeAt.getTime()),
192192
});
193193
});
194194

@@ -215,7 +215,7 @@ describe("Basic SDK Operations", () => {
215215
expect(running).toMatchObject({
216216
state: "running",
217217
claimed_by: "worker-a",
218-
claim_expires_at: new Date(baseTime.getTime() + 30 * 1000),
218+
claim_expires_at: Temporal.Instant.fromEpochMilliseconds(baseTime.getTime() + 30 * 1000),
219219
});
220220

221221
await ctx.setFakeNow(new Date(baseTime.getTime() + 5 * 60 * 1000));
@@ -274,7 +274,7 @@ describe("Basic SDK Operations", () => {
274274
const runRow = await ctx.getRun(runID);
275275
expect(runRow).toMatchObject({
276276
claimed_by: "worker-clean",
277-
claim_expires_at: new Date(base.getTime() + 60 * 1000),
277+
claim_expires_at: Temporal.Instant.fromEpochMilliseconds(base.getTime() + 60 * 1000),
278278
});
279279

280280
const beforeTTL = new Date(finishTime.getTime() + 30 * 60 * 1000);
@@ -481,7 +481,7 @@ describe("Basic SDK Operations", () => {
481481

482482
const getExpiresAt = async (runID: string) => {
483483
const run = await ctx.getRun(runID);
484-
return run?.claim_expires_at ? run.claim_expires_at.getTime() : 0;
484+
return run?.claim_expires_at ? run.claim_expires_at.epochMilliseconds : 0;
485485
};
486486

487487
absurd.workBatch("test-worker", claimTimeout);

sdks/typescript/test/events.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe("Event system", () => {
109109
wake_event: eventName,
110110
});
111111
const expectedWake = new Date(baseTime.getTime() + timeoutSeconds * 1000);
112-
expect(sleepingRun?.available_at?.getTime()).toBe(expectedWake.getTime());
112+
expect(sleepingRun?.available_at?.epochMilliseconds).toBe(expectedWake.getTime());
113113

114114
await ctx.setFakeNow(new Date(expectedWake.getTime() + 1000));
115115
await absurd.workBatch("worker1", 120, 1);

sdks/typescript/test/retry.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ describe("Retry and cancellation", () => {
312312

313313
await absurd.cancelTask(taskID);
314314
const second = await ctx.getTask(taskID);
315-
expect(second?.cancelled_at?.getTime()).toBe(
316-
first?.cancelled_at?.getTime(),
315+
expect(second?.cancelled_at?.epochMilliseconds).toBe(
316+
first?.cancelled_at?.epochMilliseconds,
317317
);
318318
});
319319

sdks/typescript/test/setup.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { join } from "node:path";
66
import { fileURLToPath } from "node:url";
77
import {
88
Absurd,
9+
Temporal,
910
type AbsurdHooks,
1011
type JsonValue,
1112
type SQLiteDatabase,
@@ -22,8 +23,8 @@ export interface TaskRow {
2223
retry_strategy: JsonValue | null;
2324
max_attempts: number | null;
2425
cancellation: JsonValue | null;
25-
enqueue_at: Date;
26-
first_started_at: Date | null;
26+
enqueue_at: Temporal.Instant;
27+
first_started_at: Temporal.Instant | null;
2728
state:
2829
| "pending"
2930
| "running"
@@ -34,7 +35,7 @@ export interface TaskRow {
3435
attempts: number;
3536
last_attempt_run: string | null;
3637
completed_payload: JsonValue | null;
37-
cancelled_at: Date | null;
38+
cancelled_at: Temporal.Instant | null;
3839
}
3940

4041
export interface RunRow {
@@ -49,16 +50,16 @@ export interface RunRow {
4950
| "failed"
5051
| "cancelled";
5152
claimed_by: string | null;
52-
claim_expires_at: Date | null;
53-
available_at: Date;
53+
claim_expires_at: Temporal.Instant | null;
54+
available_at: Temporal.Instant;
5455
wake_event: string | null;
5556
event_payload: JsonValue | null;
56-
started_at: Date | null;
57-
completed_at: Date | null;
58-
failed_at: Date | null;
57+
started_at: Temporal.Instant | null;
58+
completed_at: Temporal.Instant | null;
59+
failed_at: Temporal.Instant | null;
5960
result: JsonValue | null;
6061
failure_reason: JsonValue | null;
61-
created_at: Date;
62+
created_at: Temporal.Instant;
6263
}
6364

6465
interface SqliteFixture {

sdks/typescript/test/sqlite.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { tmpdir } from "node:os";
66

77
import { SQLiteConnection } from "../src/sqlite-connection";
88
import type { SQLiteDatabase } from "../src/sqlite-types";
9+
import { Temporal } from "../src/index";
910

1011
describe("SQLiteConnection", () => {
1112
it("rewrites postgres-style params and absurd schema names", async () => {
@@ -69,20 +70,20 @@ describe("SQLiteConnection", () => {
6970
db.close();
7071
});
7172

72-
it("decodes datetime columns into Date objects", async () => {
73+
it("decodes datetime columns into Temporal.Instant objects", async () => {
7374
const db = new sqlite(":memory:") as SQLiteDatabase;
7475
const conn = new SQLiteConnection(db);
7576
const now = Date.now();
7677

7778
await conn.exec("CREATE TABLE t_date (created_at DATETIME)");
7879
await conn.exec("INSERT INTO t_date (created_at) VALUES ($1)", [now]);
7980

80-
const { rows } = await conn.query<{ created_at: Date }>(
81+
const { rows } = await conn.query<{ created_at: Temporal.Instant }>(
8182
"SELECT created_at FROM t_date"
8283
);
8384

84-
expect(rows[0]?.created_at).toBeInstanceOf(Date);
85-
expect(rows[0]?.created_at.getTime()).toBe(now);
85+
expect(rows[0]?.created_at).toBeInstanceOf(Temporal.Instant);
86+
expect(rows[0]?.created_at.epochMilliseconds).toBe(now);
8687
db.close();
8788
});
8889

sdks/typescript/test/step.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ describe("Step functionality", () => {
205205
state: "sleeping",
206206
});
207207
const wakeTime = new Date(base.getTime() + durationSeconds * 1000);
208-
expect(sleepingRun?.available_at?.getTime()).toBe(wakeTime.getTime());
208+
expect(sleepingRun?.available_at?.epochMilliseconds).toBe(wakeTime.getTime());
209209

210210
const resumeTime = new Date(wakeTime.getTime() + 5 * 1000);
211211
vi.setSystemTime(resumeTime);

0 commit comments

Comments
 (0)