Skip to content

Commit

Permalink
Clocks can't go to 0?
Browse files Browse the repository at this point in the history
Fixes #89
  • Loading branch information
cwegrzyn committed May 24, 2024
1 parent 4f2cc33 commit 38c8926
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/tracks/clock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export class ClockIndexer extends BaseIndexer<ClockFileAdapter> {
cache: CachedMetadata,
): ClockFileAdapter | undefined {
// TODO: we should use our Either support now to handle this
// TODO: customize track image gen
return ClockFileAdapter.create(cache.frontmatter).unwrap();
}
}
Expand Down
52 changes: 52 additions & 0 deletions src/tracks/clock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Clock, ClockInput } from "./clock";

const DEFAULT_CLOCK_INPUT = {
progress: 0,
segments: 6,
active: true,
};

function make_clock(input: Partial<ClockInput> = {}) {
return Clock.create({ ...DEFAULT_CLOCK_INPUT, ...input });
}

describe("Clock", () => {
describe("create", () => {
it("accepts clocks with 0 progress", () => {
expect(
Clock.create({ active: true, progress: 0, segments: 6 }).unwrap(),
).toEqual({
progress: 0,
segments: 6,
active: true,
});
});
});

describe("withProgress", () => {
it("returns a new clock with updated progress", () => {
const clock = make_clock({ progress: 1 }).unwrap();
const newClock = clock.withProgress(2);
expect(clock.progress).toBe(1);
expect(newClock.progress).toBe(2);
});
it("constrains progress to be >= 0", () => {
expect(make_clock().unwrap().withProgress(-1).progress).toBe(0);
});

it("constrains progress to be < segments", () => {
expect(
make_clock({ segments: 6 }).unwrap().withProgress(7).progress,
).toBe(6);
});
});

describe("tick", () => {
it("returns a new clock with updated progress", () => {
const clock = make_clock({ progress: 1 }).unwrap();
const newClock = clock.tick(2);
expect(clock.progress).toBe(1);
expect(newClock.progress).toBe(3);
});
});
});
12 changes: 2 additions & 10 deletions src/tracks/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { zodResultToEither } from "../utils/zodutils";
export const clockValidator = z
.object({
/** Number of filled segments. */
progress: z.number().positive(),
progress: z.number().nonnegative(),

/** Number of total segments */
segments: z.number().positive(),
Expand Down Expand Up @@ -45,15 +45,7 @@ export class Clock implements ClockLike {
}

public tick(steps: number = 1): this {
if (!this.active) return this;

// TODO: should this give an error or something if we attempt to tick beyond the limit?
const newProgress = Math.max(
Math.min(this.segments, this.progress + steps),
0,
);
if (newProgress === this.progress) return this;
return new Clock(newProgress, this.segments, this.active) as this;
return this.withProgress(this.progress + steps);
}

public deactivate(): this {
Expand Down

0 comments on commit 38c8926

Please sign in to comment.