11import { describe , expect , it } from 'vitest'
2+ import { Temporal } from 'temporal-polyfill'
23import { DefaultMap } from '../src/utils.js'
34import { hash } from '../src/hashing/index.js'
45
5- // Minimal mock that mimics Temporal objects: Symbol.toStringTag + toString()
6- // without requiring the temporal-polyfill dependency.
7- function createTemporalLike (
8- tag : string ,
9- value : string ,
10- ) : { toString : ( ) => string ; [ Symbol . toStringTag ] : string } {
11- return Object . create ( null , {
12- [ Symbol . toStringTag ] : { value : tag } ,
13- toString : { value : ( ) => value } ,
14- } )
15- }
16-
176describe ( `DefaultMap` , ( ) => {
187 it ( `should return default value for missing keys` , ( ) => {
198 const map = new DefaultMap ( ( ) => 0 )
@@ -183,9 +172,9 @@ describe(`hash`, () => {
183172 } )
184173
185174 it ( `should hash Temporal objects by value` , ( ) => {
186- const date1 = createTemporalLike ( ` Temporal.PlainDate` , `2024-01-15` )
187- const date2 = createTemporalLike ( ` Temporal.PlainDate` , `2024-01-15` )
188- const date3 = createTemporalLike ( ` Temporal.PlainDate` , `2024-06-15` )
175+ const date1 = Temporal . PlainDate . from ( `2024-01-15` )
176+ const date2 = Temporal . PlainDate . from ( `2024-01-15` )
177+ const date3 = Temporal . PlainDate . from ( `2024-06-15` )
189178
190179 const hash1 = hash ( date1 )
191180 const hash2 = hash ( date2 )
@@ -196,34 +185,22 @@ describe(`hash`, () => {
196185 expect ( hash1 ) . not . toBe ( hash3 ) // Different Temporal dates should have different hash
197186
198187 // Different Temporal types with overlapping string representations should differ
199- const plainDate = createTemporalLike ( `Temporal.PlainDate` , `2024-01-15` )
200- const plainDateTime = createTemporalLike (
201- `Temporal.PlainDateTime` ,
202- `2024-01-15T00:00:00` ,
203- )
188+ const plainDate = Temporal . PlainDate . from ( `2024-01-15` )
189+ const plainDateTime = Temporal . PlainDateTime . from ( `2024-01-15T00:00:00` )
204190
205191 expect ( hash ( plainDate ) ) . not . toBe ( hash ( plainDateTime ) )
206192
207193 // Other Temporal types should also hash correctly
208- const time1 = createTemporalLike ( ` Temporal.PlainTime` , `10:30:00` )
209- const time2 = createTemporalLike ( ` Temporal.PlainTime` , `10:30:00` )
210- const time3 = createTemporalLike ( ` Temporal.PlainTime` , `14:00:00` )
194+ const time1 = Temporal . PlainTime . from ( `10:30:00` )
195+ const time2 = Temporal . PlainTime . from ( `10:30:00` )
196+ const time3 = Temporal . PlainTime . from ( `14:00:00` )
211197
212198 expect ( hash ( time1 ) ) . toBe ( hash ( time2 ) )
213199 expect ( hash ( time1 ) ) . not . toBe ( hash ( time3 ) )
214200
215- const instant1 = createTemporalLike (
216- `Temporal.Instant` ,
217- `2024-01-15T00:00:00Z` ,
218- )
219- const instant2 = createTemporalLike (
220- `Temporal.Instant` ,
221- `2024-01-15T00:00:00Z` ,
222- )
223- const instant3 = createTemporalLike (
224- `Temporal.Instant` ,
225- `2024-06-15T00:00:00Z` ,
226- )
201+ const instant1 = Temporal . Instant . from ( `2024-01-15T00:00:00Z` )
202+ const instant2 = Temporal . Instant . from ( `2024-01-15T00:00:00Z` )
203+ const instant3 = Temporal . Instant . from ( `2024-06-15T00:00:00Z` )
227204
228205 expect ( hash ( instant1 ) ) . toBe ( hash ( instant2 ) )
229206 expect ( hash ( instant1 ) ) . not . toBe ( hash ( instant3 ) )
0 commit comments