diff --git a/sdk/notificationhubs/notification-hubs/assets.json b/sdk/notificationhubs/notification-hubs/assets.json index d479d78b6467..88b76de25d81 100644 --- a/sdk/notificationhubs/notification-hubs/assets.json +++ b/sdk/notificationhubs/notification-hubs/assets.json @@ -2,5 +2,5 @@ "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "js", "TagPrefix": "js/notificationhubs/notification-hubs", - "Tag": "js/notificationhubs/notification-hubs_9b371a54ae" + "Tag": "js/notificationhubs/notification-hubs_40f2abdea1" } diff --git a/sdk/notificationhubs/notification-hubs/test/public/createOrUpdateRegistration.spec.ts b/sdk/notificationhubs/notification-hubs/test/public/createOrUpdateRegistration.spec.ts index b3ac38cf2daf..92454072bab4 100644 --- a/sdk/notificationhubs/notification-hubs/test/public/createOrUpdateRegistration.spec.ts +++ b/sdk/notificationhubs/notification-hubs/test/public/createOrUpdateRegistration.spec.ts @@ -21,7 +21,6 @@ describe("createRegistrationId()", () => { beforeEach(async (ctx) => { recorder = new Recorder(ctx); - await recorder.setMatcher("BodilessMatcher"); context = await createRecordedClientContext(recorder); registrationId = await createRegistrationId(context); diff --git a/sdk/notificationhubs/notification-hubs/test/public/getRegistration.spec.ts b/sdk/notificationhubs/notification-hubs/test/public/getRegistration.spec.ts index bb65b1a46fcb..b3cf117e2bfd 100644 --- a/sdk/notificationhubs/notification-hubs/test/public/getRegistration.spec.ts +++ b/sdk/notificationhubs/notification-hubs/test/public/getRegistration.spec.ts @@ -17,7 +17,6 @@ describe("getRegistration", () => { beforeEach(async (ctx) => { recorder = new Recorder(ctx); - await recorder.setMatcher("BodilessMatcher"); context = await createRecordedClientContext(recorder); let registration = createAppleRegistrationDescription({ diff --git a/sdk/notificationhubs/notification-hubs/test/public/listRegistrations.spec.ts b/sdk/notificationhubs/notification-hubs/test/public/listRegistrations.spec.ts index cfd6254fcdfa..aa00b68e4e96 100644 --- a/sdk/notificationhubs/notification-hubs/test/public/listRegistrations.spec.ts +++ b/sdk/notificationhubs/notification-hubs/test/public/listRegistrations.spec.ts @@ -17,7 +17,6 @@ describe("listRegistrations()", () => { beforeEach(async (ctx) => { recorder = new Recorder(ctx); - await recorder.setMatcher("BodilessMatcher"); context = await createRecordedClientContext(recorder); for (let i = 0; i < 3; i++) { diff --git a/sdk/notificationhubs/notification-hubs/test/public/listRegistrationsByTag.spec.ts b/sdk/notificationhubs/notification-hubs/test/public/listRegistrationsByTag.spec.ts index be7c40361a6a..636d301c7f10 100644 --- a/sdk/notificationhubs/notification-hubs/test/public/listRegistrationsByTag.spec.ts +++ b/sdk/notificationhubs/notification-hubs/test/public/listRegistrationsByTag.spec.ts @@ -21,7 +21,6 @@ describe("listRegistrationsByTag()", () => { beforeEach(async (ctx) => { recorder = new Recorder(ctx); - await recorder.setMatcher("BodilessMatcher"); context = await createRecordedClientContext(recorder); for (let i = 0; i < 3; i++) { diff --git a/sdk/notificationhubs/notification-hubs/test/public/utils/recordedClient.ts b/sdk/notificationhubs/notification-hubs/test/public/utils/recordedClient.ts index 4e11d2fe77a5..5052562b51ce 100644 --- a/sdk/notificationhubs/notification-hubs/test/public/utils/recordedClient.ts +++ b/sdk/notificationhubs/notification-hubs/test/public/utils/recordedClient.ts @@ -2,10 +2,10 @@ // Licensed under the MIT License. import type { Recorder, RecorderStartOptions } from "@azure-tools/test-recorder"; -import { env } from "@azure-tools/test-recorder"; +import { env, isPlaybackMode } from "@azure-tools/test-recorder"; import type { NotificationHubsClientContext } from "../../../src/api/index.js"; import { createClientContext } from "../../../src/api/index.js"; -import { isBrowser } from "@azure/core-util"; +import { vi } from "vitest"; const replaceableVariables: { [k: string]: string } = { // Used in record and playback modes @@ -32,6 +32,17 @@ const recorderOptions: RecorderStartOptions = { export async function createRecordedClientContext( recorder: Recorder, ): Promise { + // The following hardcoded timestamps are used to ensure deterministic playback in tests. + // [0]: "2024-04-16T22:06:17.401Z" is used for API responses that include milliseconds in the timestamp. + // [1]: "2024-04-16T22:06:17Z" is used for API responses that omit milliseconds. + // These values were chosen arbitrarily and do not correspond to any specific event; they simply provide a fixed reference time for playback mode. + const dummyTimeForPlayback = ["2024-04-16T22:06:17.401Z", "2024-04-16T22:06:17Z"]; + if (isPlaybackMode()) { + // In playback mode, we need to set the system time to a fixed value to ensure consistent results + // This is because the src code uses new Date().toISOString(), to set current time + vi.useFakeTimers(); + vi.setSystemTime(new Date(dummyTimeForPlayback[0])); // for the first request that is made in the test + } await recorder.start(recorderOptions); await recorder.addSanitizers( { @@ -41,27 +52,32 @@ export async function createRecordedClientContext( value: "class=REDACTED", }, ], + bodySanitizers: [ + { + // "Sanitizing the updated time in the response body", + regex: true, + target: "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z", + value: dummyTimeForPlayback[0], + }, + { + // "Sanitizing the updated time in the response body", + regex: true, + target: "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z", + value: dummyTimeForPlayback[1], + }, + ], + generalSanitizers: [ + // looks like the registration id is dynamic, redacting it instead + { + regex: true, + target: "registrations/(?.*?)?api-version=", + value: "registration-id-redacted", + groupForReplace: "secret", + }, + ], }, ["record", "playback"], ); - if (isBrowser) { - // there are timestamps in the body, so do not match body - await recorder.setMatcher("BodilessMatcher"); - await recorder.addSanitizers( - { - // looks like the registration id is dynamic, redacting it instead - generalSanitizers: [ - { - regex: true, - target: "registrations/(?.*?)?api-version=", - value: "registration-id-redacted", - groupForReplace: "secret", - }, - ], - }, - ["record", "playback"], - ); - } if (!env.NOTIFICATION_HUB_CONNECTION_STRING || !env.NOTIFICATION_HUB_NAME) { throw new Error(