@otp-service/testkit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 otp-service contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,27 @@
1
+ import { ChallengeStore, ChallengeRecord, OtpDelivery, DeliveryRequest } from '@otp-service/core';
2
+
3
+ declare class InMemoryChallengeStore implements ChallengeStore {
4
+ private readonly records;
5
+ create(record: ChallengeRecord): Promise<void>;
6
+ delete(challengeId: string): Promise<void>;
7
+ get(challengeId: string): Promise<ChallengeRecord | null>;
8
+ list(): ChallengeRecord[];
9
+ mustGet(challengeId: string): ChallengeRecord;
10
+ reset(): void;
11
+ update(record: ChallengeRecord): Promise<void>;
12
+ }
13
+ declare class RecordingDelivery implements OtpDelivery {
14
+ readonly requests: DeliveryRequest[];
15
+ sendChallenge(request: DeliveryRequest): Promise<void>;
16
+ lastRequest(): DeliveryRequest;
17
+ reset(): void;
18
+ }
19
+ interface DeterministicOtpGenerator {
20
+ nextOtp(): string;
21
+ remaining(): string[];
22
+ reset(): void;
23
+ }
24
+ declare function createDeterministicOtpGenerator(values: readonly string[]): DeterministicOtpGenerator;
25
+ declare function createFixedClock(isoDate: string): () => Date;
26
+
27
+ export { type DeterministicOtpGenerator, InMemoryChallengeStore, RecordingDelivery, createDeterministicOtpGenerator, createFixedClock };
package/dist/index.js ADDED
@@ -0,0 +1,83 @@
1
+ // src/index.ts
2
+ var InMemoryChallengeStore = class {
3
+ records = /* @__PURE__ */ new Map();
4
+ create(record) {
5
+ this.records.set(record.challengeId, structuredClone(record));
6
+ return Promise.resolve();
7
+ }
8
+ delete(challengeId) {
9
+ this.records.delete(challengeId);
10
+ return Promise.resolve();
11
+ }
12
+ get(challengeId) {
13
+ const record = this.records.get(challengeId);
14
+ return Promise.resolve(record ? structuredClone(record) : null);
15
+ }
16
+ list() {
17
+ return Array.from(this.records.values(), (record) => structuredClone(record));
18
+ }
19
+ mustGet(challengeId) {
20
+ const record = this.records.get(challengeId);
21
+ if (record === void 0) {
22
+ throw new Error(`Missing challenge ${challengeId}`);
23
+ }
24
+ return structuredClone(record);
25
+ }
26
+ reset() {
27
+ this.records.clear();
28
+ }
29
+ update(record) {
30
+ this.records.set(record.challengeId, structuredClone(record));
31
+ return Promise.resolve();
32
+ }
33
+ };
34
+ var RecordingDelivery = class {
35
+ requests = [];
36
+ sendChallenge(request) {
37
+ this.requests.push(structuredClone(request));
38
+ return Promise.resolve();
39
+ }
40
+ lastRequest() {
41
+ const request = this.requests.at(-1);
42
+ if (request === void 0) {
43
+ throw new Error("No delivery requests were recorded.");
44
+ }
45
+ return structuredClone(request);
46
+ }
47
+ reset() {
48
+ this.requests.length = 0;
49
+ }
50
+ };
51
+ function createDeterministicOtpGenerator(values) {
52
+ if (values.length === 0) {
53
+ throw new Error("Deterministic OTP generator requires at least one OTP value.");
54
+ }
55
+ let index = 0;
56
+ return {
57
+ nextOtp() {
58
+ const value = values[index];
59
+ if (value === void 0) {
60
+ throw new Error("Deterministic OTP generator ran out of values.");
61
+ }
62
+ index += 1;
63
+ return value;
64
+ },
65
+ remaining() {
66
+ return [...values.slice(index)];
67
+ },
68
+ reset() {
69
+ index = 0;
70
+ }
71
+ };
72
+ }
73
+ function createFixedClock(isoDate) {
74
+ const fixed = new Date(isoDate);
75
+ if (Number.isNaN(fixed.getTime())) {
76
+ throw new Error("Fixed clock requires a valid ISO date.");
77
+ }
78
+ return () => new Date(fixed.getTime());
79
+ }
80
+
81
+ export { InMemoryChallengeStore, RecordingDelivery, createDeterministicOtpGenerator, createFixedClock };
82
+ //# sourceMappingURL=index.js.map
83
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAOO,IAAM,yBAAN,MAAuD;AAAA,EAC3C,OAAA,uBAAc,GAAA,EAA6B;AAAA,EAE5D,OAAO,MAAA,EAAwC;AAC7C,IAAA,IAAA,CAAK,QAAQ,GAAA,CAAI,MAAA,CAAO,WAAA,EAAa,eAAA,CAAgB,MAAM,CAAC,CAAA;AAC5D,IAAA,OAAO,QAAQ,OAAA,EAAQ;AAAA,EACzB;AAAA,EAEA,OAAO,WAAA,EAAoC;AACzC,IAAA,IAAA,CAAK,OAAA,CAAQ,OAAO,WAAW,CAAA;AAC/B,IAAA,OAAO,QAAQ,OAAA,EAAQ;AAAA,EACzB;AAAA,EAEA,IAAI,WAAA,EAAsD;AACxD,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAI,WAAW,CAAA;AAC3C,IAAA,OAAO,QAAQ,OAAA,CAAQ,MAAA,GAAS,eAAA,CAAgB,MAAM,IAAI,IAAI,CAAA;AAAA,EAChE;AAAA,EAEA,IAAA,GAA0B;AACxB,IAAA,OAAO,KAAA,CAAM,IAA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S,gCACd,MAAA,EAC2B;AAC3B,EAAA,IAAI,MAAA,CAAO,WAAW,CAAA,EAAG;AACvB,IAAA,MAAM,IAAI,MAAM,8DAA8D,CAAA;AAAA,EAChF;AAEA,EAAA,IAAI,KAAA,GAAQ,CAAA;AAEZ,EAAA,OAAO;AAAA,IACL,OAAA,GAAU;AACR,MAAA,MAAM,KAAA,GAAQ,OAAO,KAAK,CAAA;AAC1B,MAAA,IAAI,UAAU,MAAA,EAAW;AACvB,QAAA,MAAM,IAAI,MAAM,gDAAgD,CAAA;AAAA,MAClE;AAEA,MAAA,KAAA,IAAS,CAAA;AACT,MAAA,OAAO,KAAA;AAAA,IACT,CAAA;AAAA,IACA,SAAA,GAAY;AACV,MAAA,OAAO,CAAC,GAAG,MAAA,CAAO,KAAA,CAAM,KAAK,CAAC,CAAA;AAAA,IAChC,CAAA;AAAA,IACA,KAAA,GAAQ;AACN,MAAA,KAAA,GAAQ,CAAA;AAAA,IACV;AAAA,GACF;AACF;AAEO,SAAS,iBAAiB,OAAA,EAA6B;AAC5D,EAAA,MAAM,KAAA,GAAQ,IAAI,IAAA,CAAK,OAAO,CAAA;AAC9B,EAAA,IAAI,MAAA,CAAO,KAAA,CAAM,KAAA,CAAM,OAAA,EAAS,CAAA,EAAG;AACjC,IAAA,MAAM,IAAI,MAAM,wCAAwC,CAAA;AAAA,EAC1D;AAEA,EAAA,OAAO,MAAM,IAAI,IAAA,CAAK,KAAA,CAAM,SAAS,CAAA;AACvC","file":"index.js","sourcesContent":["import type {\n ChallengeRecord,\n ChallengeStore,\n DeliveryRequest,\n OtpDelivery\n} from \"@otp-service/core\";\n\nexport class InMemoryChallengeStore implements ChallengeStore {\n private readonly records = new Map<string, ChallengeRecord>();\n\n create(record: ChallengeRecord): Promise<void> {\n this.records.set(record.challengeId, structuredClone(record));\n return Promise.resolve();\n }\n\n delete(challengeId: string): Promise<void> {\n this.records.delete(challengeId);\n return Promise.resolve();\n }\n\n get(challengeId: string): Promise<ChallengeRecord | null> {\n const record = this.records.get(challengeId);\n return Promise.resolve(record ? structuredClone(record) : null);\n }\n\n list(): ChallengeRecord[] {\n return Array.from(this.records.values(), (record) => structuredClone(record));\n }\n\n mustGet(challengeId: string): ChallengeRecord {\n const record = this.records.get(challengeId);\n if (record === undefined) {\n throw new Error(`Missing challenge ${challengeId}`);\n }\n\n return structuredClone(record);\n }\n\n reset(): void {\n this.records.clear();\n }\n\n update(record: ChallengeRecord): Promise<void> {\n this.records.set(record.challengeId, structuredClone(record));\n return Promise.resolve();\n }\n}\n\nexport class RecordingDelivery implements OtpDelivery {\n readonly requests: DeliveryRequest[] = [];\n\n sendChallenge(request: DeliveryRequest): Promise<void> {\n this.requests.push(structuredClone(request));\n return Promise.resolve();\n }\n\n lastRequest(): DeliveryRequest {\n const request = this.requests.at(-1);\n if (request === undefined) {\n throw new Error(\"No delivery requests were recorded.\");\n }\n\n return structuredClone(request);\n }\n\n reset(): void {\n this.requests.length = 0;\n }\n}\n\nexport interface DeterministicOtpGenerator {\n nextOtp(): string;\n remaining(): string[];\n reset(): void;\n}\n\nexport function createDeterministicOtpGenerator(\n values: readonly string[]\n): DeterministicOtpGenerator {\n if (values.length === 0) {\n throw new Error(\"Deterministic OTP generator requires at least one OTP value.\");\n }\n\n let index = 0;\n\n return {\n nextOtp() {\n const value = values[index];\n if (value === undefined) {\n throw new Error(\"Deterministic OTP generator ran out of values.\");\n }\n\n index += 1;\n return value;\n },\n remaining() {\n return [...values.slice(index)];\n },\n reset() {\n index = 0;\n }\n };\n}\n\nexport function createFixedClock(isoDate: string): () => Date {\n const fixed = new Date(isoDate);\n if (Number.isNaN(fixed.getTime())) {\n throw new Error(\"Fixed clock requires a valid ISO date.\");\n }\n\n return () => new Date(fixed.getTime());\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@otp-service/testkit",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Test doubles and deterministic fixtures for OTP package testing.",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/Suraj-H/otp-service-package-v2.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/Suraj-H/otp-service-package-v2/issues"
13
+ },
14
+ "homepage": "https://github.com/Suraj-H/otp-service-package-v2/tree/main/packages/testkit#readme",
15
+ "sideEffects": false,
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "engines": {
28
+ "node": ">=22.0.0"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "dependencies": {
34
+ "@otp-service/core": "0.1.0"
35
+ },
36
+ "scripts": {
37
+ "build": "tsup --config tsup.config.ts",
38
+ "clean": "rm -rf dist",
39
+ "lint": "eslint src test",
40
+ "test": "vitest run --config vitest.config.ts",
41
+ "typecheck": "tsc -p tsconfig.json --noEmit"
42
+ }
43
+ }