|
| 1 | +import { Node } from "@dafthunk/types"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | + |
| 4 | +import { NodeContext } from "../types"; |
| 5 | +import { AddDateNode } from "./add-date-node"; |
| 6 | + |
| 7 | +describe("AddDateNode", () => { |
| 8 | + const nodeId = "date-add"; |
| 9 | + const node = new AddDateNode({ |
| 10 | + nodeId, |
| 11 | + } as unknown as Node); |
| 12 | + |
| 13 | + const createContext = (inputs: Record<string, any>): NodeContext => |
| 14 | + ({ |
| 15 | + nodeId: "test", |
| 16 | + inputs, |
| 17 | + workflowId: "test", |
| 18 | + organizationId: "test-org", |
| 19 | + env: {}, |
| 20 | + }) as unknown as NodeContext; |
| 21 | + |
| 22 | + describe("execute", () => { |
| 23 | + it("should add days", async () => { |
| 24 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 25 | + const result = await node.execute( |
| 26 | + createContext({ |
| 27 | + date: baseDate, |
| 28 | + amount: 5, |
| 29 | + unit: "days", |
| 30 | + }) |
| 31 | + ); |
| 32 | + |
| 33 | + expect(result.status).toBe("completed"); |
| 34 | + expect(result.outputs?.date).toBe("2023-12-30T10:30:00.000Z"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should add hours", async () => { |
| 38 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 39 | + const result = await node.execute( |
| 40 | + createContext({ |
| 41 | + date: baseDate, |
| 42 | + amount: 2, |
| 43 | + unit: "hours", |
| 44 | + }) |
| 45 | + ); |
| 46 | + |
| 47 | + expect(result.status).toBe("completed"); |
| 48 | + expect(result.outputs?.date).toBe("2023-12-25T12:30:00.000Z"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should add minutes", async () => { |
| 52 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 53 | + const result = await node.execute( |
| 54 | + createContext({ |
| 55 | + date: baseDate, |
| 56 | + amount: 45, |
| 57 | + unit: "minutes", |
| 58 | + }) |
| 59 | + ); |
| 60 | + |
| 61 | + expect(result.status).toBe("completed"); |
| 62 | + expect(result.outputs?.date).toBe("2023-12-25T11:15:00.000Z"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should add seconds", async () => { |
| 66 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 67 | + const result = await node.execute( |
| 68 | + createContext({ |
| 69 | + date: baseDate, |
| 70 | + amount: 30, |
| 71 | + unit: "seconds", |
| 72 | + }) |
| 73 | + ); |
| 74 | + |
| 75 | + expect(result.status).toBe("completed"); |
| 76 | + expect(result.outputs?.date).toBe("2023-12-25T10:30:30.000Z"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should add milliseconds", async () => { |
| 80 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 81 | + const result = await node.execute( |
| 82 | + createContext({ |
| 83 | + date: baseDate, |
| 84 | + amount: 500, |
| 85 | + unit: "milliseconds", |
| 86 | + }) |
| 87 | + ); |
| 88 | + |
| 89 | + expect(result.status).toBe("completed"); |
| 90 | + expect(result.outputs?.date).toBe("2023-12-25T10:30:00.500Z"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should add weeks", async () => { |
| 94 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 95 | + const result = await node.execute( |
| 96 | + createContext({ |
| 97 | + date: baseDate, |
| 98 | + amount: 2, |
| 99 | + unit: "weeks", |
| 100 | + }) |
| 101 | + ); |
| 102 | + |
| 103 | + expect(result.status).toBe("completed"); |
| 104 | + expect(result.outputs?.date).toBe("2024-01-08T10:30:00.000Z"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should add months", async () => { |
| 108 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 109 | + const result = await node.execute( |
| 110 | + createContext({ |
| 111 | + date: baseDate, |
| 112 | + amount: 1, |
| 113 | + unit: "months", |
| 114 | + }) |
| 115 | + ); |
| 116 | + |
| 117 | + expect(result.status).toBe("completed"); |
| 118 | + expect(result.outputs?.date).toBe("2024-01-25T10:30:00.000Z"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should add years", async () => { |
| 122 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 123 | + const result = await node.execute( |
| 124 | + createContext({ |
| 125 | + date: baseDate, |
| 126 | + amount: 1, |
| 127 | + unit: "years", |
| 128 | + }) |
| 129 | + ); |
| 130 | + |
| 131 | + expect(result.status).toBe("completed"); |
| 132 | + expect(result.outputs?.date).toBe("2024-12-25T10:30:00.000Z"); |
| 133 | + }); |
| 134 | + |
| 135 | + it("should subtract time with negative amount", async () => { |
| 136 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 137 | + const result = await node.execute( |
| 138 | + createContext({ |
| 139 | + date: baseDate, |
| 140 | + amount: -5, |
| 141 | + unit: "days", |
| 142 | + }) |
| 143 | + ); |
| 144 | + |
| 145 | + expect(result.status).toBe("completed"); |
| 146 | + expect(result.outputs?.date).toBe("2023-12-20T10:30:00.000Z"); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should handle invalid date input", async () => { |
| 150 | + const result = await node.execute( |
| 151 | + createContext({ |
| 152 | + date: "invalid-date", |
| 153 | + amount: 5, |
| 154 | + unit: "days", |
| 155 | + }) |
| 156 | + ); |
| 157 | + |
| 158 | + expect(result.status).toBe("completed"); |
| 159 | + expect(result.outputs?.date).toBeUndefined(); |
| 160 | + }); |
| 161 | + |
| 162 | + it("should handle invalid unit", async () => { |
| 163 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 164 | + const result = await node.execute( |
| 165 | + createContext({ |
| 166 | + date: baseDate, |
| 167 | + amount: 5, |
| 168 | + unit: "invalid-unit", |
| 169 | + }) |
| 170 | + ); |
| 171 | + |
| 172 | + expect(result.status).toBe("completed"); |
| 173 | + expect(result.outputs?.date).toBeUndefined(); |
| 174 | + }); |
| 175 | + |
| 176 | + it("should handle string amount", async () => { |
| 177 | + const baseDate = "2023-12-25T10:30:00.000Z"; |
| 178 | + const result = await node.execute( |
| 179 | + createContext({ |
| 180 | + date: baseDate, |
| 181 | + amount: "5", |
| 182 | + unit: "days", |
| 183 | + }) |
| 184 | + ); |
| 185 | + |
| 186 | + expect(result.status).toBe("completed"); |
| 187 | + expect(result.outputs?.date).toBe("2023-12-30T10:30:00.000Z"); |
| 188 | + }); |
| 189 | + |
| 190 | + it("should handle leap year correctly", async () => { |
| 191 | + const baseDate = "2024-02-28T10:30:00.000Z"; |
| 192 | + const result = await node.execute( |
| 193 | + createContext({ |
| 194 | + date: baseDate, |
| 195 | + amount: 1, |
| 196 | + unit: "days", |
| 197 | + }) |
| 198 | + ); |
| 199 | + |
| 200 | + expect(result.status).toBe("completed"); |
| 201 | + expect(result.outputs?.date).toBe("2024-02-29T10:30:00.000Z"); |
| 202 | + }); |
| 203 | + }); |
| 204 | +}); |
0 commit comments