Skip to content

Commit

Permalink
Fixed unit tests and added new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanbliss committed Sep 10, 2024
1 parent bb4e30a commit 61a275e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/live-share-media/src/test/GroupPlaybackPosition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import {
GroupTransportState,
ITransportState,
} from "../internals/GroupTransportState";
import { TestLiveShareHost, TimeInterval } from "@microsoft/live-share";
import { TestLiveShareHost } from "@microsoft/live-share";
import {
IRuntimeSignaler,
LiveShareRuntime,
MockLiveShareRuntime,
} from "@microsoft/live-share/internal";
import { IMediaPlayerState } from "../LiveMediaSessionCoordinator";
import { GroupPlaybackTrack } from "../internals/GroupPlaybackTrack";
import { PriorityTimeInterval } from "../internals/PriorityTimeInterval";

function createTransportUpdate(
runtime: IRuntimeSignaler,
Expand Down Expand Up @@ -103,7 +104,7 @@ async function getObjects(updateInterval: number = 10000) {
async function getPlayBackPosition(
liveRuntime: MockLiveShareRuntime,
runtime1: IRuntimeSignaler,
updateInterval = new TimeInterval(10)
updateInterval = new PriorityTimeInterval(10, () => 1)
) {
const track1 = {
trackIdentifier: "track1",
Expand Down Expand Up @@ -288,7 +289,7 @@ describe("GroupPlaybackPosition", () => {
});

it("should enumerate client positions", async () => {
const updateInterval = new TimeInterval(10);
const updateInterval = new PriorityTimeInterval(10, () => 1);
const { liveRuntime, runtime1, runtime2, dispose } = await getObjects();
const { playbackPosition } = await getPlayBackPosition(
liveRuntime,
Expand Down Expand Up @@ -327,7 +328,7 @@ describe("GroupPlaybackPosition", () => {
});

it("should ignore stale positions", async () => {
const updateInterval = new TimeInterval(10);
const updateInterval = new PriorityTimeInterval(10, () => 1);
const { liveRuntime, runtime1, runtime2, dispose } = await getObjects();
const { playbackPosition } = await getPlayBackPosition(
liveRuntime,
Expand Down Expand Up @@ -370,7 +371,7 @@ describe("GroupPlaybackPosition", () => {
const { playbackPosition } = await getPlayBackPosition(
liveRuntime,
runtime1,
new TimeInterval(1000)
new PriorityTimeInterval(1000, () => 1)
);

try {
Expand Down Expand Up @@ -405,7 +406,7 @@ describe("GroupPlaybackPosition", () => {
const { playbackPosition, transportState } = await getPlayBackPosition(
liveRuntime,
runtime1,
new TimeInterval(1000)
new PriorityTimeInterval(1000, () => 1)
);

try {
Expand Down Expand Up @@ -436,7 +437,7 @@ describe("GroupPlaybackPosition", () => {
const { playbackPosition, transportState } = await getPlayBackPosition(
liveRuntime,
runtime1,
new TimeInterval(1000)
new PriorityTimeInterval(1000, () => 1)
);

try {
Expand Down Expand Up @@ -478,7 +479,7 @@ describe("GroupPlaybackPosition", () => {
const { playbackPosition, transportState } = await getPlayBackPosition(
liveRuntime,
runtime1,
new TimeInterval(1000)
new PriorityTimeInterval(1000, () => 1)
);

try {
Expand Down Expand Up @@ -531,7 +532,7 @@ describe("GroupPlaybackPosition", () => {
const { playbackPosition, transportState } = await getPlayBackPosition(
liveRuntime,
runtime1,
new TimeInterval(1000)
new PriorityTimeInterval(1000, () => 1)
);

try {
Expand Down Expand Up @@ -574,7 +575,7 @@ describe("GroupPlaybackPosition", () => {
const { playbackPosition, transportState } = await getPlayBackPosition(
liveRuntime,
runtime1,
new TimeInterval(1000)
new PriorityTimeInterval(1000, () => 1)
);

try {
Expand Down Expand Up @@ -610,7 +611,7 @@ describe("GroupPlaybackPosition", () => {
const { playbackPosition, transportState } = await getPlayBackPosition(
liveRuntime,
runtime1,
new TimeInterval(1000)
new PriorityTimeInterval(1000, () => 1)
);

try {
Expand Down
77 changes: 77 additions & 0 deletions packages/live-share-media/src/test/PriorityTimeInterval.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Microsoft Live Share SDK License.
*/

import "mocha";
import { strict as assert } from "assert";
import { PriorityTimeInterval } from "../internals/PriorityTimeInterval";

describe("PriorityTimeInterval", () => {
it("Should scale properly with default values in constructor", () => {
const interval = new PriorityTimeInterval(1000, () => 2);
assert(interval.milliseconds === 2000);
assert(interval.seconds === 2);
});

it("Should not scale when starting with priority", () => {
const interval = new PriorityTimeInterval(1000, () => 2, true);
assert(interval.milliseconds === 1000);
assert(interval.seconds === 1);
});

it("Should not scale when user does not have priority but scaling is disabled", () => {
const interval = new PriorityTimeInterval(1000, () => 2, false, false);
assert(interval.milliseconds === 1000);
assert(interval.seconds === 1);
});

it("Should scale properly after changing values", () => {
const interval = new PriorityTimeInterval(1000, () => 2);
assert(interval.milliseconds === 2000);
assert(interval.seconds === 2);
// Changing settings will break ts checks, since we asserted above that milliseconds was 2000 and ts can't figure out relationship.
// We ignore ts for lines where this occurs.
interval.hasPriority = true;

// @ts-ignore-next
assert(interval.milliseconds === 1000);
// @ts-ignore-next
assert(interval.seconds === 1);

interval.hasPriority = false;

// @ts-ignore-next
assert(interval.milliseconds === 2000);
// @ts-ignore-next
assert(interval.seconds === 2);

interval.shouldPrioritize = false;

// @ts-ignore-next
assert(interval.milliseconds === 1000);
// @ts-ignore-next
assert(interval.seconds === 1);

interval.shouldPrioritize = true;

// @ts-ignore-next
assert(interval.milliseconds === 2000);
// @ts-ignore-next
assert(interval.seconds === 2);
});

it("Should dynamically scale depending on scaleBy func response", () => {
let scaleBy = 2;
const interval = new PriorityTimeInterval(1000, () => scaleBy);
assert(interval.milliseconds === 2000);
assert(interval.seconds === 2);
// Changing scaleBy will break ts checks, since we asserted above that milliseconds was 2000 and ts can't figure out relationship.
// We ignore ts for lines where this occurs.
scaleBy = 3;
// @ts-ignore-next
assert(interval.milliseconds === 3000);
// @ts-ignore-next
assert(interval.seconds === 3);
});
});

0 comments on commit 61a275e

Please sign in to comment.