From 5c00d18dc1fc0492c5a694bbf0c22273d26cc982 Mon Sep 17 00:00:00 2001 From: jcowman2 Date: Thu, 27 Jun 2019 19:10:01 -0700 Subject: [PATCH] feat(common): Adds the peek() method to PKProvider Also finishes fixing unit tests for the Agent PK refactor and adds documentation re #104 --- src/agents/impl/agent-keys.ts | 13 ++- src/agents/impl/agent-utils.ts | 16 ++- src/agents/impl/instance-agents-impl.ts | 1 + src/common/impl/pk-provider-impl.ts | 4 + src/common/keys.ts | 18 ++++ src/common/type-utils.ts | 7 ++ test/unit/agents.test.ts | 20 ++-- test/unit/api-hooks.test.ts | 20 ++-- test/unit/config.test.ts | 127 ++++++++++++------------ test/unit/events.test.ts | 27 ++--- test/unit/game-api.test.ts | 35 ++++--- test/unit/keys.test.ts | 52 +++++++++- 12 files changed, 228 insertions(+), 112 deletions(-) diff --git a/src/agents/impl/agent-keys.ts b/src/agents/impl/agent-keys.ts index 8697c0a..74b2b02 100644 --- a/src/agents/impl/agent-keys.ts +++ b/src/agents/impl/agent-keys.ts @@ -1,8 +1,19 @@ +/* + * Contains the `Agent` primary key system. + * + * Copyright (c) Joseph R Cowman + * Licensed under MIT License (see https://github.com/regal/regal) + */ + import { buildPKProvider } from "../../common"; +/** The set of reserved `Agent` primary keys. */ export const AGENT_RESERVED_KEYS = { + /** Every `GameInstance` has this key. */ GAME_INSTANCE: 100, + /** Before an `Agent` is activated, it has this key. */ INACTIVE: 99 }; -export let STATIC_AGENT_PK_PROVIDER = buildPKProvider(AGENT_RESERVED_KEYS); +/** The `Agent` `PKProvider` for static agents. */ +export const STATIC_AGENT_PK_PROVIDER = buildPKProvider(AGENT_RESERVED_KEYS); diff --git a/src/agents/impl/agent-utils.ts b/src/agents/impl/agent-utils.ts index a03b48c..d0852f8 100644 --- a/src/agents/impl/agent-utils.ts +++ b/src/agents/impl/agent-utils.ts @@ -1,12 +1,22 @@ +/* + * Contains various utilities for the Agents component. + * + * Copyright (c) Joseph R Cowman + * Licensed under MIT License (see https://github.com/regal/regal) + */ + import { PK } from "../../common"; import { Agent } from "../agent"; import { AGENT_RESERVED_KEYS, STATIC_AGENT_PK_PROVIDER } from "./agent-keys"; +/** Shorthand for the `AGENT_RESERVED_KEYS.INACTIVE` primary key. */ export const getInactiveAgentPK = () => STATIC_AGENT_PK_PROVIDER.reserved(AGENT_RESERVED_KEYS.INACTIVE); -export const isAgentActive = (id: PK) => - !getInactiveAgentPK().equals(id); - +/** Shorthand for the `AGENT_RESERVED_KEYS.GAME_INSTANCE` primary key. */ export const getGameInstancePK = () => STATIC_AGENT_PK_PROVIDER.reserved(AGENT_RESERVED_KEYS.GAME_INSTANCE); + +/** Determines whether an `Agent` has been activated, given its primary key. */ +export const isAgentActive = (id: PK) => + !getInactiveAgentPK().equals(id); diff --git a/src/agents/impl/instance-agents-impl.ts b/src/agents/impl/instance-agents-impl.ts index 0914727..17d5096 100644 --- a/src/agents/impl/instance-agents-impl.ts +++ b/src/agents/impl/instance-agents-impl.ts @@ -40,6 +40,7 @@ export const buildInstanceAgents = ( /** Implementation of `InstanceAgentsInternal`. */ class InstanceAgentsImpl implements InstanceAgentsInternal { + /** The internal `Agent` `PKProvider`. */ private _pkProvider: PKProvider; constructor( diff --git a/src/common/impl/pk-provider-impl.ts b/src/common/impl/pk-provider-impl.ts index 1b1720f..2acbcca 100644 --- a/src/common/impl/pk-provider-impl.ts +++ b/src/common/impl/pk-provider-impl.ts @@ -94,4 +94,8 @@ export class PKProviderImpl implements PKProvider { this.lastPK = sortedPKs[sortedPKs.length - 1]; } + + public peek(): PK { + return this.lastPK.plus(1); + } } diff --git a/src/common/keys.ts b/src/common/keys.ts index 205013d..2b218c4 100644 --- a/src/common/keys.ts +++ b/src/common/keys.ts @@ -39,6 +39,14 @@ export interface PK { */ value(): string; + /** + * Returns the placement of this key in the list of all keys. + * For example, an index of 2 means this was the second key generated. + * + * This includes reserved keys; if a set of reserved keys was used + * to generate this key's `PKProvider`, the entry with the lowest value + * will have an index of 0. + */ index(): number; } @@ -82,7 +90,17 @@ export interface PKProvider { */ reserved(key: number): PK; + /** + * Resets the `PKProvider`'s internal `PK` generator back to its start value. + * Any reserved keys used to generate this `PKProvider` will remain. + */ reset(): void; + + /** + * Provides the `PK` that would be generated by `PKProvider.next()` without + * incrementing the internal key generator. + */ + peek(): PK; } /** diff --git a/src/common/type-utils.ts b/src/common/type-utils.ts index 93b2310..39a03fa 100644 --- a/src/common/type-utils.ts +++ b/src/common/type-utils.ts @@ -1,3 +1,10 @@ +/* + * Custom TypeScript utilities. + * + * Copyright (c) Joseph R Cowman + * Licensed under MIT License (see https://github.com/regal/regal) + */ + /** * Makes readonly properties in an object mutable and requried. * Only use this when you need it; it's like admin access. diff --git a/test/unit/agents.test.ts b/test/unit/agents.test.ts index 84b52e1..81776b7 100644 --- a/test/unit/agents.test.ts +++ b/test/unit/agents.test.ts @@ -2636,18 +2636,20 @@ describe("Agents", function() { }); describe("StaticAgentRegistry", function() { - // it("StaticAgentRegistry.getNextAvailableId increments by 1 for each static agent", function() { - // expect(StaticAgentRegistry.getNextAvailableId()).to.equal(1); + it("STATIC_AGENT_PK_PROVIDER increments by 1 for each static agent added to the static registry", function() { + const [_pk0, pk1, pk2, pk3, _pk4, pk5] = pks(5); - // const D = new Dummy("D1", 10); - // expect(StaticAgentRegistry.getNextAvailableId()).to.equal(2); + expect(STATIC_AGENT_PK_PROVIDER.peek().equals(pk1)).to.be.true; - // const P = new Parent(D); - // expect(StaticAgentRegistry.getNextAvailableId()).to.equal(3); + const D = new Dummy("D1", 10); + expect(STATIC_AGENT_PK_PROVIDER.peek().equals(pk2)).to.be.true; + + const P = new Parent(D); + expect(STATIC_AGENT_PK_PROVIDER.peek().equals(pk3)).to.be.true; - // const P2 = new Parent(new Dummy("D2", 5)); - // expect(StaticAgentRegistry.getNextAvailableId()).to.equal(5); - // }); + const P2 = new Parent(new Dummy("D2", 5)); + expect(STATIC_AGENT_PK_PROVIDER.peek().equals(pk5)).to.be.true; + }); it("StaticAgentRegistry.hasAgentProperty works properly", function() { const D = new Dummy("D1", 10); diff --git a/test/unit/api-hooks.test.ts b/test/unit/api-hooks.test.ts index 4027321..8d5ea4d 100644 --- a/test/unit/api-hooks.test.ts +++ b/test/unit/api-hooks.test.ts @@ -3,7 +3,7 @@ import "mocha"; import { on, noop } from "../../src/events"; import { metadataWithOptions, getDemoMetadata } from "../test-utils"; -import { PropertyOperation } from "../../src/agents"; +import { PropertyOperation, getGameInstancePK } from "../../src/agents"; import { RegalError } from "../../src/error"; import { Game, @@ -59,7 +59,7 @@ describe("API Hooks", function() { name: "INPUT", changes: [ { - agentId: 0, + agentId: getGameInstancePK(), op: PropertyOperation.ADDED, property: "input", init: undefined, @@ -101,7 +101,7 @@ describe("API Hooks", function() { name: "SET INPUT", changes: [ { - agentId: 0, + agentId: getGameInstancePK(), op: PropertyOperation.ADDED, property: "input", init: undefined, @@ -157,7 +157,7 @@ describe("API Hooks", function() { name: "DOUBLE INPUT", changes: [ { - agentId: 0, + agentId: getGameInstancePK(), op: PropertyOperation.MODIFIED, property: "input", init: "Test Command", @@ -171,7 +171,7 @@ describe("API Hooks", function() { name: "SET INPUT", changes: [ { - agentId: 0, + agentId: getGameInstancePK(), op: PropertyOperation.ADDED, property: "input", init: undefined, @@ -230,7 +230,7 @@ describe("API Hooks", function() { name: "START", changes: [ { - agentId: 0, + agentId: getGameInstancePK(), op: PropertyOperation.ADDED, property: "init", init: undefined, @@ -270,7 +270,7 @@ describe("API Hooks", function() { name: "SET INIT", changes: [ { - agentId: 0, + agentId: getGameInstancePK(), op: PropertyOperation.ADDED, property: "init", init: undefined, @@ -339,7 +339,7 @@ describe("API Hooks", function() { name: "APPEND FOO", changes: [ { - agentId: 0, + agentId: getGameInstancePK(), op: PropertyOperation.MODIFIED, property: "foo", init: "One", @@ -353,7 +353,7 @@ describe("API Hooks", function() { name: "SET FOO", changes: [ { - agentId: 0, + agentId: getGameInstancePK(), op: PropertyOperation.ADDED, property: "foo", init: undefined, @@ -367,7 +367,7 @@ describe("API Hooks", function() { name: "SET INIT", changes: [ { - agentId: 0, + agentId: getGameInstancePK(), op: PropertyOperation.ADDED, property: "init", init: undefined, diff --git a/test/unit/config.test.ts b/test/unit/config.test.ts index ecb209b..e0436e6 100644 --- a/test/unit/config.test.ts +++ b/test/unit/config.test.ts @@ -16,7 +16,10 @@ import { log, libraryVersion, metadataWithVersion, - Dummy + Dummy, + smartObjectEquals, + TestProperty, + pks } from "../test-utils"; import { on } from "../../src/events"; import { Agent, PropertyOperation, getGameInstancePK } from "../../src/agents"; @@ -266,6 +269,7 @@ describe("Config", function() { it("Full agent property history is shown when GameOptions.trackAgentChanges is set to true", function() { prepAgentTest(); + const [pk0, pk1, pk2] = pks(2); let response = Game.postStartCommand({ trackAgentChanges: true @@ -273,11 +277,11 @@ describe("Config", function() { let responseInstance = response.instance as GameInstanceInternal; - expect(responseInstance.agents).to.deep.equal({ - _nextId: 1, + smartObjectEquals(responseInstance.agents, { + _pkProvider: TestProperty.REQUIRE_BUT_SKIP, game: response.instance, - "0": { - id: 0, + [pk0.value()]: { + id: pk0, game: response.instance, dummyCount: [ { @@ -297,7 +301,7 @@ describe("Config", function() { causedBy: 1, changes: [ { - agentId: 0, + agentId: pk0, final: 0, init: undefined, op: PropertyOperation.ADDED, @@ -315,11 +319,11 @@ describe("Config", function() { response = Game.postPlayerCommand(response.instance, "Lars"); responseInstance = response.instance as GameInstanceInternal; - expect(responseInstance.agents).to.deep.equal({ - _nextId: 2, + smartObjectEquals(responseInstance.agents, { + _pkProvider: TestProperty.REQUIRE_BUT_SKIP, game: response.instance, - "0": { - id: 0, + [pk0.value()]: { + id: pk0, game: response.instance, dummyCount: [ { @@ -341,14 +345,14 @@ describe("Config", function() { { eventId: 4, eventName: "ADD", - final: { refId: 1 }, + final: { refId: pk1 }, init: undefined, op: PropertyOperation.ADDED } ] }, - "1": { - id: 1, + [pk1.value()]: { + id: pk1, game: response.instance, name: [ { @@ -392,7 +396,7 @@ describe("Config", function() { output: [1], changes: [ { - agentId: 1, + agentId: pk1, final: 15, init: 10, op: PropertyOperation.MODIFIED, @@ -407,35 +411,35 @@ describe("Config", function() { caused: [5], changes: [ { - agentId: 1, + agentId: pk1, final: "Lars", init: undefined, op: PropertyOperation.ADDED, property: "name" }, { - agentId: 1, + agentId: pk1, final: 10, init: undefined, op: PropertyOperation.ADDED, property: "health" }, { - agentId: 1, + agentId: pk1, final: "Lars the Great", init: "Lars", op: PropertyOperation.MODIFIED, property: "name" }, { - agentId: 0, - final: { refId: 1 }, + agentId: pk0, + final: { refId: pk1 }, init: undefined, op: PropertyOperation.ADDED, property: "currentDummy" }, { - agentId: 0, + agentId: pk0, final: 1, init: 0, op: PropertyOperation.MODIFIED, @@ -453,11 +457,11 @@ describe("Config", function() { response = Game.postPlayerCommand(response.instance, "Jeffrey"); responseInstance = response.instance as GameInstanceInternal; - expect(responseInstance.agents).to.deep.equal({ - _nextId: 3, + smartObjectEquals(responseInstance.agents, { + _pkProvider: TestProperty.REQUIRE_BUT_SKIP, game: response.instance, - "0": { - id: 0, + [pk0.value()]: { + id: pk0, game: response.instance, dummyCount: [ { @@ -479,21 +483,21 @@ describe("Config", function() { { eventId: 7, eventName: "ADD", - final: { refId: 2 }, - init: { refId: 1 }, + final: { refId: pk2 }, + init: { refId: pk1 }, op: PropertyOperation.MODIFIED }, { eventId: 0, eventName: "DEFAULT", - final: { refId: 1 }, + final: { refId: pk1 }, init: undefined, op: PropertyOperation.ADDED } ] }, - "1": { - id: 1, + [pk1.value()]: { + id: pk1, game: response.instance, name: [ { @@ -514,8 +518,8 @@ describe("Config", function() { } ] }, - "2": { - id: 2, + [pk2.value()]: { + id: pk2, game: response.instance, name: [ { @@ -559,7 +563,7 @@ describe("Config", function() { output: [2], changes: [ { - agentId: 2, + agentId: pk2, final: 15, init: 10, op: PropertyOperation.MODIFIED, @@ -574,35 +578,35 @@ describe("Config", function() { caused: [8], changes: [ { - agentId: 2, + agentId: pk2, final: "Jeffrey", init: undefined, op: PropertyOperation.ADDED, property: "name" }, { - agentId: 2, + agentId: pk2, final: 10, init: undefined, op: PropertyOperation.ADDED, property: "health" }, { - agentId: 2, + agentId: pk2, final: "Jeffrey the Great", init: "Jeffrey", op: PropertyOperation.MODIFIED, property: "name" }, { - agentId: 0, - final: { refId: 2 }, - init: { refId: 1 }, + agentId: pk0, + final: { refId: pk2 }, + init: { refId: pk1 }, op: PropertyOperation.MODIFIED, property: "currentDummy" }, { - agentId: 0, + agentId: pk0, final: 2, init: 1, op: PropertyOperation.MODIFIED, @@ -620,17 +624,18 @@ describe("Config", function() { it("Reduced agent property history is shown when GameOptions.trackAgentChanges is set to false", function() { prepAgentTest(); + const [pk0, pk1, pk2] = pks(2); let response = Game.postStartCommand({ trackAgentChanges: false }); let responseInstance = response.instance as GameInstanceInternal; - expect(responseInstance.agents).to.deep.equal({ - _nextId: 1, + smartObjectEquals(responseInstance.agents, { + _pkProvider: TestProperty.REQUIRE_BUT_SKIP, game: response.instance, - "0": { - id: 0, + [pk0.value()]: { + id: pk0, game: response.instance, dummyCount: [ { @@ -659,11 +664,11 @@ describe("Config", function() { response = Game.postPlayerCommand(response.instance, "Lars"); responseInstance = response.instance as GameInstanceInternal; - expect(responseInstance.agents).to.deep.equal({ - _nextId: 2, + smartObjectEquals(responseInstance.agents, { + _pkProvider: TestProperty.REQUIRE_BUT_SKIP, game: response.instance, - "0": { - id: 0, + [pk0.value()]: { + id: pk0, game: response.instance, dummyCount: [ { @@ -685,14 +690,14 @@ describe("Config", function() { { eventId: 4, eventName: "ADD", - final: { refId: 1 }, + final: { refId: pk1 }, init: undefined, op: PropertyOperation.ADDED } ] }, - "1": { - id: 1, + [pk1.value()]: { + id: pk1, game: response.instance, name: [ { @@ -737,11 +742,11 @@ describe("Config", function() { response = Game.postPlayerCommand(response.instance, "Jeffrey"); responseInstance = response.instance as GameInstanceInternal; - expect(responseInstance.agents).to.deep.equal({ - _nextId: 3, + smartObjectEquals(responseInstance.agents, { + _pkProvider: TestProperty.REQUIRE_BUT_SKIP, game: response.instance, - "0": { - id: 0, + [pk0.value()]: { + id: pk0, game: response.instance, dummyCount: [ { @@ -763,21 +768,21 @@ describe("Config", function() { { eventId: 7, eventName: "ADD", - final: { refId: 2 }, - init: { refId: 1 }, + final: { refId: pk2 }, + init: { refId: pk1 }, op: PropertyOperation.MODIFIED }, { eventId: 0, eventName: "DEFAULT", - final: { refId: 1 }, + final: { refId: pk1 }, init: undefined, op: PropertyOperation.ADDED } ] }, - "1": { - id: 1, + [pk1.value()]: { + id: pk1, game: response.instance, name: [ { @@ -798,8 +803,8 @@ describe("Config", function() { } ] }, - "2": { - id: 2, + [pk2.value()]: { + id: pk2, game: response.instance, name: [ { diff --git a/test/unit/events.test.ts b/test/unit/events.test.ts index 6428a64..cbe348a 100644 --- a/test/unit/events.test.ts +++ b/test/unit/events.test.ts @@ -13,11 +13,12 @@ import { buildInstanceEvents, GameEventBuilder } from "../../src/events"; -import { log, getDemoMetadata, Dummy } from "../test-utils"; +import { log, getDemoMetadata, Dummy, pks } from "../test-utils"; import { Agent, PropertyOperation, - StaticAgentRegistry + StaticAgentRegistry, + getGameInstancePK } from "../../src/agents"; import { OutputLineType } from "../../src/output"; import { Game } from "../../src/api"; @@ -751,7 +752,7 @@ describe("Events", function() { output: [1], changes: [ { - agentId: 1, + agentId: getGameInstancePK().plus(1), op: PropertyOperation.MODIFIED, init: 10, final: 25, @@ -811,6 +812,8 @@ describe("Events", function() { const myGame = buildGameInstance({ trackAgentChanges: true }); start(myGame); + const [pk0, pk1, pk2] = pks(2); + expect(myGame.events.history).to.deep.equal([ { id: 3, @@ -824,7 +827,7 @@ describe("Events", function() { causedBy: 2, changes: [ { - agentId: 2, + agentId: pk2, op: PropertyOperation.MODIFIED, property: "health", init: 25, @@ -840,26 +843,26 @@ describe("Events", function() { caused: [4], changes: [ { - agentId: 2, + agentId: pk2, op: PropertyOperation.ADDED, property: "name", init: undefined, final: "Bill" }, { - agentId: 2, + agentId: pk2, op: PropertyOperation.ADDED, property: "health", init: undefined, final: 25 }, { - agentId: 1, + agentId: pk1, op: PropertyOperation.ADDED, property: "friend", init: undefined, final: { - refId: 2 + refId: pk2 } } ] @@ -870,26 +873,26 @@ describe("Events", function() { caused: [2, 3], changes: [ { - agentId: 1, + agentId: pk1, op: PropertyOperation.ADDED, property: "name", init: undefined, final: "Lars" }, { - agentId: 1, + agentId: pk1, op: PropertyOperation.ADDED, property: "health", init: undefined, final: 10 }, { - agentId: 0, + agentId: pk0, op: PropertyOperation.ADDED, property: "mainAgent", init: undefined, final: { - refId: 1 + refId: pk1 } } ] diff --git a/test/unit/game-api.test.ts b/test/unit/game-api.test.ts index cb30ab5..0634c99 100644 --- a/test/unit/game-api.test.ts +++ b/test/unit/game-api.test.ts @@ -16,7 +16,8 @@ import { getDemoMetadata, metadataWithOptions, metadataWithVersion, - Dummy + Dummy, + pks } from "../test-utils"; import { Agent, StaticAgentRegistry } from "../../src/agents"; import { @@ -271,10 +272,11 @@ describe("Game API", function() { const r1 = Game.postStartCommand(); const r1_instance = r1.instance as GameInstanceInternal; + const pks0_3 = pks(3); expect( r1_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2, 3]); + ).to.deep.equal(pks0_3); expect(r1.output).to.deep.equal({ wasSuccessful: true, @@ -292,10 +294,10 @@ describe("Game API", function() { expect( r1_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2, 3]); + ).to.deep.equal(pks0_3); expect( r2_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2, 3]); + ).to.deep.equal(pks0_3); expect(r2.output).to.deep.equal({ wasSuccessful: true, @@ -313,10 +315,10 @@ describe("Game API", function() { expect( r2_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2, 3]); + ).to.deep.equal(pks0_3); expect( r3_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2]); + ).to.deep.equal(pks(2)); expect(r3.output).to.deep.equal({ wasSuccessful: true, @@ -334,10 +336,10 @@ describe("Game API", function() { expect( r3_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2]); + ).to.deep.equal(pks(2)); expect( r4_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1]); + ).to.deep.equal(pks(1)); }); it("Calling before initialization throws an error", function() { @@ -628,10 +630,11 @@ describe("Game API", function() { const r1 = Game.postStartCommand(); const r1_instance = r1.instance as GameInstanceInternal; + const pks0_3 = pks(3); expect( r1_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2, 3]); + ).to.deep.equal(pks0_3); expect( r1_instance.agents.getAgentProperty(getGameInstancePK(), "arr") .length @@ -643,10 +646,10 @@ describe("Game API", function() { expect( r1_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2, 3]); + ).to.deep.equal(pks0_3); expect( r2_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2, 3]); + ).to.deep.equal(pks0_3); expect( r2_instance.agents.getAgentProperty(getGameInstancePK(), "arr") .length @@ -658,7 +661,7 @@ describe("Game API", function() { expect( r3_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2, 3]); + ).to.deep.equal(pks0_3); expect( r3_instance.agents.getAgentProperty(getGameInstancePK(), "arr") .length @@ -670,7 +673,7 @@ describe("Game API", function() { expect( r4_instance.agents.agentManagers().map(am => am.id) - ).to.deep.equal([0, 1, 2, 3]); + ).to.deep.equal(pks0_3); expect( r4_instance.agents.getAgentProperty(getGameInstancePK(), "arr") .length @@ -716,9 +719,11 @@ describe("Game API", function() { delete game.state.parent; }); + const pk2 = getGameInstancePK().plus(2); + const r1 = Game.postStartCommand(); expect(r1.instance.state.parent.child).to.deep.equal({ - id: 2, + id: pk2, name: "D2", health: 15 }); @@ -728,7 +733,7 @@ describe("Game API", function() { const r3 = Game.postUndoCommand(r2.instance); expect(r3.instance.state.parent.child).to.deep.equal({ - id: 2, + id: pk2, name: "D2", health: 15 }); diff --git a/test/unit/keys.test.ts b/test/unit/keys.test.ts index 4b993b0..641e8f4 100644 --- a/test/unit/keys.test.ts +++ b/test/unit/keys.test.ts @@ -165,6 +165,56 @@ describe("Keys", function() { fork.next(); fork.fork().next(); // What do you eat soup with? - expect(original.next().equals(firstPK.plus(1))); + expect(original.next().equals(firstPK.plus(1))).to.be.true; + }); + + it("Resetting a PK Provider with reserved and generated keys", function() { + const prov = buildPKProvider(RESERVED_KEYS); + const originalNext = prov.peek(); + + prov.next(); + prov.next(); + prov.reset(); + + expect(prov.peek().equals(originalNext)).to.be.true; + }); + + it("Resetting a PK Provider with no reserved keys", function() { + const prov = buildPKProvider(); + const originalNext = prov.peek(); + + prov.next(); + prov.next(); + prov.reset(); + + expect(prov.peek().equals(originalNext)).to.be.true; + }); + + it("Resetting a PK Provider with no keys whatsoever", function() { + const prov = buildPKProvider(); + const originalNext = prov.peek(); + prov.reset(); + expect(prov.peek().equals(originalNext)).to.be.true; + }); + + it("PKProvider.peek doesn't generate a key", function() { + const prov = buildPKProvider(); + const peek = prov.peek(); + const next = prov.next(); + expect(peek.equals(next)).to.be.true; + }); + + it("A key generated after another will have a higher index", function() { + const prov = buildPKProvider(RESERVED_KEYS); + + expect(prov.next().index()).to.be.lessThan(prov.next().index()); + expect(prov.next().index()).to.be.greaterThan( + prov.reserved(RESERVED_KEYS.LARS).index() + ); + }); + + it("If a set of reserved keys is used to generate a PKProvider, the one with the lowest value will have an index of zero.", function() { + const prov = buildPKProvider(RESERVED_KEYS); + expect(prov.reserved(RESERVED_KEYS.FOO).index()).equals(0); }); });