-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b12a5ab
commit 5e6b82a
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { WaveTracker } from "./WaveTracker"; | ||
|
||
describe("WaveTracker", () => { | ||
describe("addAndCheck", () => { | ||
it("returns false for first wave hash", () => { | ||
// Arrange | ||
const waveTracker = new WaveTracker(); | ||
|
||
// Act | ||
const result = waveTracker.addAndCheck({ a: [] }); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("returns false for a second, new wave hash", () => { | ||
// Arrange | ||
const waveTracker = new WaveTracker(); | ||
waveTracker.addAndCheck({ a: [] }); | ||
|
||
// Act | ||
const result = waveTracker.addAndCheck({ b: [] }); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("returns false for a second, duplicate wave hash", () => { | ||
// Arrange | ||
const waveTracker = new WaveTracker(); | ||
waveTracker.addAndCheck({ a: [] }); | ||
|
||
// Act | ||
const result = waveTracker.addAndCheck({ a: [] }); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("returns true for a duplicate number wave at the threshold", () => { | ||
// Arrange | ||
const waveTracker = new WaveTracker(); | ||
|
||
for (let i = 0; i < 25; i += 1) { | ||
waveTracker.addAndCheck({ a: [] }); | ||
} | ||
|
||
// Act | ||
const result = waveTracker.addAndCheck({ a: [] }); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("returns false for a duplicate number wave interrupted before at the threshold", () => { | ||
// Arrange | ||
const waveTracker = new WaveTracker(); | ||
|
||
for (let i = 0; i < 10; i += 1) { | ||
waveTracker.addAndCheck({ a: [] }); | ||
} | ||
waveTracker.addAndCheck({ b: [] }); | ||
|
||
for (let i = 0; i < 15; i += 1) { | ||
waveTracker.addAndCheck({ a: [] }); | ||
} | ||
|
||
// Act | ||
const result = waveTracker.addAndCheck({ a: [] }); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("returns true for a duplicate number wave resumed before at the threshold", () => { | ||
// Arrange | ||
const waveTracker = new WaveTracker(); | ||
|
||
for (let i = 0; i < 10; i += 1) { | ||
waveTracker.addAndCheck({ a: [] }); | ||
} | ||
waveTracker.addAndCheck({ b: [] }); | ||
|
||
for (let i = 0; i < 24; i += 1) { | ||
waveTracker.addAndCheck({ a: [] }); | ||
} | ||
|
||
// Act | ||
const result = waveTracker.addAndCheck({ a: [] }); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Mutation } from "automutate"; | ||
import { Dictionary } from "../../../shared/maps"; | ||
|
||
const createHash = (fileMutations: Dictionary<readonly Mutation[]>) => Object.keys(fileMutations).join(","); | ||
|
||
/** | ||
* How many waves in a row must be duplicates of previously seen waves to halt. | ||
*/ | ||
const threshold = 25; | ||
|
||
export class WaveTracker { | ||
/** | ||
* How many times each wave file names hash has been seen. | ||
*/ | ||
#previouslySeen = new Set<string>(); | ||
|
||
/** | ||
* How many waves in a row have now had repetitions greater than the duplicate threshold. | ||
*/ | ||
#repeatedCount = 0; | ||
|
||
/** | ||
* @param fileMutations A newly created wave of mutations. | ||
* @returns Whether the wave has reached the duplicate threshold. | ||
*/ | ||
addAndCheck(fileMutations: Dictionary<readonly Mutation[]>) { | ||
const hash = createHash(fileMutations); | ||
const previouslySeen = this.#previouslySeen.has(hash); | ||
|
||
this.#previouslySeen.add(hash); | ||
|
||
if (!previouslySeen) { | ||
this.#repeatedCount = 0; | ||
return false; | ||
} | ||
|
||
this.#repeatedCount += 1; | ||
return this.#repeatedCount === threshold; | ||
} | ||
} |