Skip to content

Commit f425557

Browse files
committed
Use toTemporalInstant instead of hardcoding the datetime strings
1 parent 2189fd3 commit f425557

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

tests/interfaces/records/handlers/records-query.spec.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@ import { RecordsQueryHandler } from '../../../../src/interfaces/records/handlers
2222
import { StorageController } from '../../../../src/store/storage-controller.js';
2323
import { TestDataGenerator } from '../../../utils/test-data-generator.js';
2424
import { TestStubGenerator } from '../../../utils/test-stub-generator.js';
25+
import { toTemporalInstant } from '@js-temporal/polyfill';
2526

2627
import { constructRecordsWriteIndexes } from '../../../../src/interfaces/records/handlers/records-write.js';
2728
import { DataStream, DidResolver, Dwn, HdKey, KeyDerivationScheme, Records } from '../../../../src/index.js';
2829
import { DateSort, RecordsQuery } from '../../../../src/interfaces/records/messages/records-query.js';
2930

31+
declare global {
32+
interface Date {
33+
toTemporalInstant: typeof toTemporalInstant;
34+
}
35+
}
36+
Date.prototype.toTemporalInstant = toTemporalInstant;
37+
3038
chai.use(chaiAsPromised);
3139

3240
describe('RecordsQueryHandler.handle()', () => {
@@ -183,11 +191,12 @@ describe('RecordsQueryHandler.handle()', () => {
183191
expect(reply3.entries?.length).to.equal(0);
184192
});
185193

194+
186195
it('should be able to range query by `dateCreated`', async () => {
187196
// scenario: 3 records authored by alice, created on first of 2021, 2022, and 2023 respectively, only the first 2 records share the same schema
188-
const firstDayOf2021 = '2021-01-01T00:00:00.000000Z';
189-
const firstDayOf2022 = '2022-01-01T00:00:00.000000Z';
190-
const firstDayOf2023 = '2023-01-01T00:00:00.000000Z';
197+
const firstDayOf2021 = new Date(2021, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
198+
const firstDayOf2022 = new Date(2022, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
199+
const firstDayOf2023 = new Date(2023, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
191200
const alice = await DidKeyResolver.generate();
192201
const write1 = await TestDataGenerator.generateRecordsWrite({ requester: alice, dateCreated: firstDayOf2021, dateModified: firstDayOf2021 });
193202
const write2 = await TestDataGenerator.generateRecordsWrite({ requester: alice, dateCreated: firstDayOf2022, dateModified: firstDayOf2022 });
@@ -202,7 +211,7 @@ describe('RecordsQueryHandler.handle()', () => {
202211
expect(writeReply3.status.code).to.equal(202);
203212

204213
// testing `from` range
205-
const lastDayOf2021 = '2021-12-31T00:00:00.000000Z';
214+
const lastDayOf2021 = new Date(2021, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
206215
const recordsQuery1 = await TestDataGenerator.generateRecordsQuery({
207216
requester : alice,
208217
filter : { dateCreated: { from: lastDayOf2021 } },
@@ -214,7 +223,7 @@ describe('RecordsQueryHandler.handle()', () => {
214223
expect(reply1.entries![1].encodedData).to.equal(Encoder.bytesToBase64Url(write3.dataBytes!));
215224

216225
// testing `to` range
217-
const lastDayOf2022 = '2022-12-31T00:00:00.000000Z';
226+
const lastDayOf2022 = new Date(2022, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
218227
const recordsQuery2 = await TestDataGenerator.generateRecordsQuery({
219228
requester : alice,
220229
filter : { dateCreated: { to: lastDayOf2022 } },
@@ -226,7 +235,7 @@ describe('RecordsQueryHandler.handle()', () => {
226235
expect(reply2.entries![1].encodedData).to.equal(Encoder.bytesToBase64Url(write2.dataBytes!));
227236

228237
// testing `from` and `to` range
229-
const lastDayOf2023 = '2023-12-31T00:00:00.000000Z';
238+
const lastDayOf2023 = new Date(2023, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
230239
const recordsQuery3 = await TestDataGenerator.generateRecordsQuery({
231240
requester : alice,
232241
filter : { dateCreated: { from: lastDayOf2022, to: lastDayOf2023 } },
@@ -249,9 +258,9 @@ describe('RecordsQueryHandler.handle()', () => {
249258

250259
it('should be able use range and exact match queries at the same time', async () => {
251260
// scenario: 3 records authored by alice, created on first of 2021, 2022, and 2023 respectively, only the first 2 records share the same schema
252-
const firstDayOf2021 = '2021-01-01T00:00:00.000000Z';
253-
const firstDayOf2022 = '2022-01-01T00:00:00.000000Z';
254-
const firstDayOf2023 = '2023-01-01T00:00:00.000000Z';
261+
const firstDayOf2021 = new Date(2021, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
262+
const firstDayOf2022 = new Date(2022, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
263+
const firstDayOf2023 = new Date(2023, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
255264
const alice = await DidKeyResolver.generate();
256265
const schema = '2021And2022Schema';
257266
const write1 = await TestDataGenerator.generateRecordsWrite({
@@ -273,8 +282,8 @@ describe('RecordsQueryHandler.handle()', () => {
273282
expect(writeReply3.status.code).to.equal(202);
274283

275284
// testing range criterion with another exact match
276-
const lastDayOf2021 = '2021-12-31T00:00:00.000000Z';
277-
const lastDayOf2023 = '2023-12-31T00:00:00.000000Z';
285+
const lastDayOf2021 = new Date(2021, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
286+
const lastDayOf2023 = new Date(2023, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
278287
const recordsQuery5 = await TestDataGenerator.generateRecordsQuery({
279288
requester : alice,
280289
filter : {

0 commit comments

Comments
 (0)