@kensio/yulin 0.0.1

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.
Files changed (34) hide show
  1. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  2. package/.idea/jsLinters/eslint.xml +6 -0
  3. package/.idea/modules.xml +8 -0
  4. package/.idea/prettier.xml +7 -0
  5. package/.idea/vcs.xml +6 -0
  6. package/.idea/yulin.iml +8 -0
  7. package/.nvmrc +1 -0
  8. package/.prettierrc +4 -0
  9. package/LICENSE +661 -0
  10. package/README.md +11 -0
  11. package/eslint.config.ts +208 -0
  12. package/package.json +69 -0
  13. package/pnpm-workspace.yaml +1 -0
  14. package/src/command/command-handler.ts +3 -0
  15. package/src/service/dynamodb/command/create-table/create-table.handler.ts +59 -0
  16. package/src/service/dynamodb/command/create-table/create-table.iso.test.ts +67 -0
  17. package/src/service/dynamodb/command/describe-table/describe-table.handler.ts +50 -0
  18. package/src/service/dynamodb/command/describe-table/describe-table.iso.test.ts +67 -0
  19. package/src/service/dynamodb/command/list-tables/list-tables.handler.ts +51 -0
  20. package/src/service/dynamodb/command/list-tables/list-tables.iso.test.ts +74 -0
  21. package/src/service/dynamodb/dynamodb-table.iso.test.ts +41 -0
  22. package/src/service/dynamodb/dynamodb-table.ts +46 -0
  23. package/src/service/dynamodb/dynamodb.ts +46 -0
  24. package/src/service/organizations/sim-aws-account.ts +36 -0
  25. package/src/util/background/background.iso.test.ts +74 -0
  26. package/src/util/background/background.ts +62 -0
  27. package/src/util/brand.type.ts +11 -0
  28. package/src/util/memo/memo.iso.test.ts +43 -0
  29. package/src/util/memo/memo.ts +26 -0
  30. package/src/util/sleep.ts +16 -0
  31. package/tsconfig.build.json +13 -0
  32. package/tsconfig.json +36 -0
  33. package/typings/eslint-plugin-promise.d.ts +11 -0
  34. package/vitest.config.ts +50 -0
@@ -0,0 +1,208 @@
1
+ import { dirname } from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+
4
+ import eslint from "@eslint/js";
5
+ import vitest from "@vitest/eslint-plugin";
6
+ import { defineConfig } from "eslint/config";
7
+ import prettier from "eslint-config-prettier";
8
+ import { jsdoc } from "eslint-plugin-jsdoc";
9
+ import noSecrets from "eslint-plugin-no-secrets";
10
+ import security from "eslint-plugin-security";
11
+ import unicorn from "eslint-plugin-unicorn";
12
+ import globals from "globals";
13
+ import tseslint from "typescript-eslint";
14
+
15
+ const __dirname = dirname(fileURLToPath(import.meta.url));
16
+
17
+ export default defineConfig(
18
+ // ── Global ignores ──────────────────────────────────────
19
+ {
20
+ ignores: ["dist/", "**/.coverage/*", "node_modules/", "*.config.ts"],
21
+ },
22
+
23
+ // ── Base ESLint recommended ─────────────────────────────
24
+ eslint.configs.recommended,
25
+
26
+ // ── TypeScript (strictest level + type-aware) ───────────
27
+ ...tseslint.configs.strictTypeChecked,
28
+ ...tseslint.configs.stylisticTypeChecked,
29
+
30
+ // ── General settings for all TS files ───────────────────
31
+ {
32
+ languageOptions: {
33
+ ecmaVersion: "latest",
34
+ sourceType: "module",
35
+ globals: {
36
+ ...globals.node,
37
+ },
38
+ parserOptions: {
39
+ projectService: true,
40
+ tsconfigRootDir: __dirname,
41
+ },
42
+ },
43
+ rules: {
44
+ // ── TypeScript overrides & additions ──────────────
45
+ "@typescript-eslint/explicit-function-return-type": "error",
46
+ "@typescript-eslint/explicit-module-boundary-types": "error",
47
+ "@typescript-eslint/no-explicit-any": "error",
48
+ "@typescript-eslint/consistent-type-imports": [
49
+ "error",
50
+ { prefer: "type-imports", fixStyle: "inline-type-imports" },
51
+ ],
52
+ "@typescript-eslint/consistent-type-exports": [
53
+ "error",
54
+ { fixMixedExportsWithInlineTypeSpecifier: true },
55
+ ],
56
+ "@typescript-eslint/no-import-type-side-effects": "error",
57
+ "@typescript-eslint/switch-exhaustiveness-check": "error",
58
+ "@typescript-eslint/no-unnecessary-condition": "error",
59
+ "@typescript-eslint/prefer-nullish-coalescing": "error",
60
+ "@typescript-eslint/strict-boolean-expressions": "error",
61
+ "no-await-in-loop": "error", // catches sequential awaits that should be Promise.all()
62
+ "no-template-curly-in-string": "error", // catches '${name}' in regular strings (missing backticks)
63
+ "no-promise-executor-return": "error", // catches accidental return in new Promise((resolve) => return ...)
64
+ "no-unreachable-loop": "error", // catches loops that only ever run once
65
+ "no-param-reassign": "error", // prevents mutating function parameters (major bug source)
66
+ "prefer-const": "error", // const over let when never reassigned
67
+ "object-shorthand": ["error", "always"], // { foo: foo } → { foo }
68
+ "prefer-template": "error", // template literals over string concatenation
69
+ "@typescript-eslint/prefer-readonly": "error", // flags private fields that are never reassigned
70
+ "@typescript-eslint/require-array-sort-compare": "error", // prevents [1, 10, 2].sort() (lexicographic surprise)
71
+ "@typescript-eslint/naming-convention": [
72
+ "error",
73
+ {
74
+ selector: ["property", "objectLiteralProperty", "typeProperty"],
75
+ format: ["camelCase", "PascalCase"],
76
+ leadingUnderscore: "allow",
77
+ },
78
+ {
79
+ selector: "default",
80
+ format: ["camelCase"],
81
+ leadingUnderscore: "allow",
82
+ },
83
+ {
84
+ selector: "typeLike",
85
+ format: ["PascalCase"],
86
+ },
87
+ {
88
+ selector: "enumMember",
89
+ format: ["PascalCase"],
90
+ },
91
+ {
92
+ selector: "variable",
93
+ modifiers: ["const", "exported"],
94
+ format: ["camelCase", "UPPER_CASE"],
95
+ },
96
+ ],
97
+
98
+ // ── General quality ──────────────────────────────
99
+ "no-console": "warn",
100
+ eqeqeq: ["error", "always"],
101
+ curly: ["error", "all"],
102
+ "@typescript-eslint/only-throw-error": "error",
103
+ },
104
+ },
105
+
106
+ // ── Security (low cost security checks) ────────────────
107
+ // TODO: Remove cast when eslint-plugin-security fixes defineConfig() types
108
+ // https://github.com/eslint-community/eslint-plugin-security/issues/175
109
+ security.configs.recommended as any,
110
+
111
+ // ── Unicorn (modern JS best practices) ──────────────────
112
+ // https://github.com/sindresorhus/eslint-plugin-unicorn?tab=readme-ov-file#recommended-config
113
+ unicorn.configs.recommended,
114
+ {
115
+ rules: {
116
+ "unicorn/better-regex": "warn",
117
+ "unicorn/prevent-abbreviations": "off",
118
+ },
119
+ },
120
+
121
+ // ── Vitest (test files only) ────────────────────────────
122
+ {
123
+ files: ["**/*.test.ts"],
124
+ plugins: {
125
+ vitest,
126
+ },
127
+ rules: {
128
+ ...vitest.configs.recommended.rules,
129
+ "vitest/consistent-test-it": ["error", { fn: "it" }],
130
+ "vitest/no-focused-tests": "error",
131
+ "vitest/no-disabled-tests": "warn",
132
+ "vitest/no-duplicate-hooks": "error",
133
+ "vitest/prefer-to-be": "error",
134
+ "vitest/prefer-to-have-length": "error",
135
+ "vitest/prefer-strict-equal": "error",
136
+ "vitest/require-top-level-describe": "error",
137
+ "vitest/expect-expect": "off",
138
+
139
+ // Relax some rules that are too strict for test files
140
+ "@typescript-eslint/no-explicit-any": "off",
141
+ "@typescript-eslint/no-unsafe-assignment": "off",
142
+ "@typescript-eslint/no-unsafe-member-access": "off",
143
+ "@typescript-eslint/explicit-function-return-type": "off",
144
+ "import-x/no-default-export": "off",
145
+ },
146
+ },
147
+
148
+ // ── Config files (allow default exports) ────────────────
149
+ {
150
+ files: ["*.config.ts", "*.config.js"],
151
+ rules: {
152
+ "import-x/no-default-export": "off",
153
+ },
154
+ },
155
+
156
+ // ── No Secrets (detect accidental secret inclusion) ────────
157
+ {
158
+ plugins: { "no-secrets": noSecrets },
159
+ rules: {
160
+ "no-secrets/no-secrets": "error",
161
+ },
162
+ },
163
+
164
+ // ── JSDoc (enforce minimal doc commenting) ────────────────
165
+ jsdoc({
166
+ config: "flat/recommended-error",
167
+ }),
168
+ {
169
+ rules: {
170
+ "jsdoc/require-param": "off",
171
+ "jsdoc/require-returns": "off",
172
+ "jsdoc/require-param-description": "off",
173
+ "jsdoc/require-returns-description": "off",
174
+ "jsdoc/require-yields": "off",
175
+ "jsdoc/require-description": [
176
+ "error",
177
+ {
178
+ descriptionStyle: "body",
179
+ checkConstructors: false,
180
+ checkGetters: false,
181
+ checkSetters: false,
182
+ },
183
+ ],
184
+ "jsdoc/require-jsdoc": [
185
+ "error",
186
+ {
187
+ contexts: [
188
+ "ClassDeclaration",
189
+ "ExportNamedDeclaration > VariableDeclaration[kind='const'] > VariableDeclarator[init.type='ObjectExpression']",
190
+ "ExportNamedDeclaration > VariableDeclaration[kind='const'] > VariableDeclarator[init.type='ArrayExpression']",
191
+ "ExportNamedDeclaration > VariableDeclaration[kind='const'] > VariableDeclarator[init.type='NewExpression']",
192
+ "ExportNamedDeclaration > VariableDeclaration[kind='const'] > VariableDeclarator[init.type='CallExpression']",
193
+ ],
194
+ publicOnly: true,
195
+ checkConstructors: false,
196
+ require: {
197
+ FunctionDeclaration: true,
198
+ MethodDefinition: true,
199
+ ClassDeclaration: true,
200
+ },
201
+ },
202
+ ],
203
+ },
204
+ },
205
+
206
+ // ── Prettier (must be last — disables conflicting rules)
207
+ prettier,
208
+ );
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@kensio/yulin",
3
+ "version": "0.0.1",
4
+ "description": "AWS system behaviour simulation for isolated unit testing",
5
+ "repository": "https://github.com/KensioSoftware/yulin",
6
+ "type": "module",
7
+ "main": "index.js",
8
+ "keywords": [
9
+ "aws",
10
+ "test",
11
+ "mock",
12
+ "emulator",
13
+ "simulator",
14
+ "isolated"
15
+ ],
16
+ "author": "Kensio Software",
17
+ "license": "AGPL-3.0",
18
+ "devDependencies": {
19
+ "@aws-sdk/client-dynamodb": "^3.1001.0",
20
+ "@aws-sdk/client-s3": "^3.1001.0",
21
+ "@eslint/js": "^10.0.1",
22
+ "@kensio/smartass": "^0.1.4",
23
+ "@smithy/smithy-client": "^4.12.1",
24
+ "@smithy/types": "^4.13.0",
25
+ "@types/eslint-plugin-security": "^3.0.1",
26
+ "@types/lodash-es": "^4.17.12",
27
+ "@types/node": "^25.3.0",
28
+ "@vitest/coverage-v8": "4.0.18",
29
+ "@vitest/eslint-plugin": "^1.6.9",
30
+ "aws-sdk-client-mock": "^4.1.0",
31
+ "eslint": "^10.0.1",
32
+ "eslint-config-prettier": "^10.1.8",
33
+ "eslint-plugin-jsdoc": "^62.7.1",
34
+ "eslint-plugin-no-secrets": "^2.2.2",
35
+ "eslint-plugin-security": "^4.0.0",
36
+ "eslint-plugin-unicorn": "^63.0.0",
37
+ "globals": "^17.3.0",
38
+ "jiti": "^2.6.1",
39
+ "prettier": "^3.8.1",
40
+ "typescript-eslint": "^8.56.0",
41
+ "vitest": "^4.0.18"
42
+ },
43
+ "peerDependencies": {
44
+ "@aws-sdk/client-dynamodb": "^3.1001.0",
45
+ "@aws-sdk/client-s3": "^3.1001.0",
46
+ "@smithy/smithy-client": "^4.12.1"
47
+ },
48
+ "peerDependenciesMeta": {
49
+ "@smithy/smithy-client": {
50
+ "optional": true
51
+ },
52
+ "@aws-sdk/client-s3": {
53
+ "optional": true
54
+ },
55
+ "@aws-sdk/client-dynamodb": {
56
+ "optional": true
57
+ }
58
+ },
59
+ "dependencies": {
60
+ "lodash-es": "^4.17.23"
61
+ },
62
+ "scripts": {
63
+ "build": "tsc -p tsconfig.build.json",
64
+ "fmt": "eslint --fix '{src,test}/**/*.{ts,js}' && prettier --write '{src,test}/**/*.{ts,js,json,md,css,html,yml,yaml}'",
65
+ "lint": "eslint '{src,test}/**/*.{ts,js}' && prettier --check '{src,test}/**/*.{ts,js,json,md,css,html,yml,yaml}'",
66
+ "test": "vitest run",
67
+ "test:coverage": "vitest run --coverage"
68
+ }
69
+ }
@@ -0,0 +1 @@
1
+ useNodeVersion: 24.13.1
@@ -0,0 +1,3 @@
1
+ export interface CommandHandler<TCommand, TOutput> {
2
+ handle(cmd: TCommand): Promise<TOutput>;
3
+ }
@@ -0,0 +1,59 @@
1
+ import type { DynamoDbTableName } from "../../dynamodb-table.js";
2
+ import { SimDynamoDbTable } from "../../dynamodb-table.js";
3
+ import {
4
+ type CreateTableCommand,
5
+ type CreateTableCommandOutput,
6
+ ResourceInUseException,
7
+ } from "@aws-sdk/client-dynamodb";
8
+ import type { BackgroundScheduler } from "../../../../util/background/background.js";
9
+ import type { CommandHandler } from "../../../../command/command-handler.js";
10
+ import { jitter } from "../../../../util/sleep.js";
11
+
12
+ /**
13
+ * DynamoDB CreateTableCommand handler.
14
+ */
15
+ export class CreateTableCommandHandler implements CommandHandler<
16
+ CreateTableCommand,
17
+ CreateTableCommandOutput
18
+ > {
19
+ constructor(
20
+ private readonly tables: Map<DynamoDbTableName, SimDynamoDbTable>,
21
+ private readonly background: BackgroundScheduler,
22
+ ) {}
23
+
24
+ /**
25
+ * Handle creation of a new DynamoDB Table.
26
+ */
27
+ async handle(cmd: CreateTableCommand): Promise<CreateTableCommandOutput> {
28
+ if (cmd.input.TableName === undefined) {
29
+ throw new Error("CreateTableCommand.input.TableName is required");
30
+ }
31
+
32
+ const tableName = cmd.input.TableName as DynamoDbTableName;
33
+ if (this.tables.has(tableName)) {
34
+ throw new ResourceInUseException({
35
+ message: `DynamoDB Table ${tableName} already exists`,
36
+ $metadata: {},
37
+ });
38
+ }
39
+
40
+ await jitter();
41
+
42
+ const table = new SimDynamoDbTable(cmd);
43
+ this.tables.set(tableName, table);
44
+
45
+ this.background.schedule(() => table.activate());
46
+
47
+ return {
48
+ TableDescription: {
49
+ AttributeDefinitions: [],
50
+ TableName: tableName,
51
+ KeySchema: [],
52
+ TableStatus: table.status,
53
+ CreationDateTime: table.creationDateTime,
54
+ GlobalSecondaryIndexes: [],
55
+ },
56
+ $metadata: {},
57
+ };
58
+ }
59
+ }
@@ -0,0 +1,67 @@
1
+ import { describe, it } from "vitest";
2
+ import {
3
+ CreateTableCommand,
4
+ ListTablesCommand,
5
+ ResourceInUseException,
6
+ } from "@aws-sdk/client-dynamodb";
7
+ import { SimAwsAccount } from "../../../organizations/sim-aws-account.js";
8
+ import {
9
+ assertArrayLength,
10
+ assertIdentical,
11
+ assertInstanceOf,
12
+ assertNonNullable,
13
+ assertThrowsErrorAsync,
14
+ } from "@kensio/smartass";
15
+
16
+ describe("DynamoDB CreateTableCommand", () => {
17
+ it("creates new DynamoDB Table", async () => {
18
+ const simAccount = new SimAwsAccount();
19
+ const simDynamoDb = simAccount.getDynamoDb();
20
+
21
+ const createTableOutput = await simDynamoDb.createTable(
22
+ new CreateTableCommand({ TableName: "FoobarTable" }),
23
+ );
24
+
25
+ assertNonNullable(createTableOutput.TableDescription);
26
+ assertIdentical(
27
+ createTableOutput.TableDescription.TableName,
28
+ "FoobarTable",
29
+ );
30
+ assertIdentical(createTableOutput.TableDescription.TableStatus, "CREATING");
31
+
32
+ const listTablesOutput = await simDynamoDb.listTables(
33
+ new ListTablesCommand(),
34
+ );
35
+
36
+ assertArrayLength(listTablesOutput.TableNames, 1);
37
+ assertIdentical(listTablesOutput.TableNames[0], "FoobarTable");
38
+
39
+ await simAccount.backgroundTasksComplete();
40
+ });
41
+
42
+ it("throws on undefined Table name", async () => {
43
+ const simAccount = new SimAwsAccount();
44
+ const simDynamoDb = simAccount.getDynamoDb();
45
+
46
+ await assertThrowsErrorAsync(async () =>
47
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: undefined })),
48
+ );
49
+ });
50
+
51
+ it("throws on duplicate Table name", async () => {
52
+ const simAccount = new SimAwsAccount();
53
+ const simDynamoDb = simAccount.getDynamoDb();
54
+
55
+ await simDynamoDb.createTable(
56
+ new CreateTableCommand({ TableName: "FoobarTable" }),
57
+ );
58
+
59
+ const error = await assertThrowsErrorAsync(async () =>
60
+ simDynamoDb.createTable(
61
+ new CreateTableCommand({ TableName: "FoobarTable" }),
62
+ ),
63
+ );
64
+ assertInstanceOf(error, ResourceInUseException);
65
+ assertIdentical(error.$fault, "client");
66
+ });
67
+ });
@@ -0,0 +1,50 @@
1
+ import {
2
+ ResourceNotFoundException,
3
+ type DescribeTableCommand,
4
+ type DescribeTableOutput,
5
+ } from "@aws-sdk/client-dynamodb";
6
+ import type { CommandHandler } from "../../../../command/command-handler.js";
7
+ import type {
8
+ DynamoDbTableName,
9
+ SimDynamoDbTable,
10
+ } from "../../dynamodb-table.js";
11
+ import { jitter } from "../../../../util/sleep.js";
12
+
13
+ /**
14
+ * DynamoDB DescribeTableCommand handler.
15
+ */
16
+ export class DescribeTableCommandHandler implements CommandHandler<
17
+ DescribeTableCommand,
18
+ DescribeTableOutput
19
+ > {
20
+ constructor(
21
+ private readonly tables: Map<DynamoDbTableName, SimDynamoDbTable>,
22
+ ) {}
23
+
24
+ /**
25
+ * Simulate describing DynamoDB Table.
26
+ */
27
+ async handle(cmd: DescribeTableCommand): Promise<DescribeTableOutput> {
28
+ if (cmd.input.TableName === undefined) {
29
+ throw new Error("DescribeTableCommand.input.TableName is required");
30
+ }
31
+ const tableName = cmd.input.TableName as DynamoDbTableName;
32
+
33
+ await jitter();
34
+
35
+ const table = this.tables.get(tableName);
36
+ if (table === undefined) {
37
+ throw new ResourceNotFoundException({
38
+ message: `No DynamoDB Table named ${tableName}`,
39
+ $metadata: {},
40
+ });
41
+ }
42
+
43
+ return {
44
+ Table: {
45
+ TableName: table.tableName,
46
+ TableStatus: table.status,
47
+ },
48
+ };
49
+ }
50
+ }
@@ -0,0 +1,67 @@
1
+ import { describe, it } from "vitest";
2
+ import { SimAwsAccount } from "../../../organizations/sim-aws-account.js";
3
+ import {
4
+ CreateTableCommand,
5
+ DescribeTableCommand,
6
+ ResourceNotFoundException,
7
+ } from "@aws-sdk/client-dynamodb";
8
+ import {
9
+ assertIdentical,
10
+ assertInstanceOf,
11
+ assertNonNullable,
12
+ assertOneOf,
13
+ assertThrowsErrorAsync,
14
+ } from "@kensio/smartass";
15
+
16
+ describe("DynamoDB DescribeTableCommand", () => {
17
+ it("describes DynamoDB Table", async () => {
18
+ const simAccount = new SimAwsAccount();
19
+ const simDynamoDb = simAccount.getDynamoDb();
20
+
21
+ await Promise.all([
22
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableA" })),
23
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableB" })),
24
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableC" })),
25
+ ]);
26
+
27
+ const describeTableOutput = await simDynamoDb.describeTable(
28
+ new DescribeTableCommand({ TableName: "TableB" }),
29
+ );
30
+
31
+ assertNonNullable(describeTableOutput.Table?.TableName);
32
+ assertIdentical(describeTableOutput.Table.TableName, "TableB");
33
+ assertNonNullable(describeTableOutput.Table.TableStatus);
34
+ assertOneOf(describeTableOutput.Table.TableStatus, ["CREATING", "ACTIVE"]);
35
+
36
+ await simAccount.backgroundTasksComplete();
37
+
38
+ const describeAgain = await simDynamoDb.describeTable(
39
+ new DescribeTableCommand({ TableName: "TableB" }),
40
+ );
41
+ assertNonNullable(describeAgain.Table?.TableStatus);
42
+ assertIdentical(describeAgain.Table.TableStatus, "ACTIVE");
43
+ });
44
+
45
+ it("throws on undefined Table name", async () => {
46
+ const simAccount = new SimAwsAccount();
47
+ const simDynamoDb = simAccount.getDynamoDb();
48
+
49
+ await assertThrowsErrorAsync(async () =>
50
+ simDynamoDb.describeTable(
51
+ new DescribeTableCommand({ TableName: undefined }),
52
+ ),
53
+ );
54
+ });
55
+
56
+ it("throws on describing non-existent DynamoDB Table", async () => {
57
+ const simAccount = new SimAwsAccount();
58
+ const simDynamoDb = simAccount.getDynamoDb();
59
+
60
+ const error = await assertThrowsErrorAsync(async () =>
61
+ simDynamoDb.describeTable(
62
+ new DescribeTableCommand({ TableName: "NonExistentTable" }),
63
+ ),
64
+ );
65
+ assertInstanceOf(error, ResourceNotFoundException);
66
+ });
67
+ });
@@ -0,0 +1,51 @@
1
+ import type { CommandHandler } from "../../../../command/command-handler.js";
2
+ import type {
3
+ ListTablesCommand,
4
+ ListTablesOutput,
5
+ } from "@aws-sdk/client-dynamodb";
6
+ import type {
7
+ DynamoDbTableName,
8
+ SimDynamoDbTable,
9
+ } from "../../dynamodb-table.js";
10
+ import { jitter } from "../../../../util/sleep.js";
11
+
12
+ /**
13
+ * Simulated DynamoDB ListTablesCommand handler.
14
+ */
15
+ export class ListTablesCommandHandler implements CommandHandler<
16
+ ListTablesCommand,
17
+ ListTablesOutput
18
+ > {
19
+ constructor(
20
+ private readonly tables: Map<DynamoDbTableName, SimDynamoDbTable>,
21
+ ) {}
22
+
23
+ /**
24
+ * Simulate listing DynamoDB Tables.
25
+ */
26
+ async handle(cmd: ListTablesCommand): Promise<ListTablesOutput> {
27
+ await jitter();
28
+
29
+ const tables = [...this.tables.values()];
30
+ tables.sort((a, b) => a.tableName.localeCompare(b.tableName));
31
+
32
+ const exclusiveStartTableName = cmd.input.ExclusiveStartTableName;
33
+ const limit = cmd.input.Limit ?? 100;
34
+
35
+ const startIndex =
36
+ exclusiveStartTableName === undefined
37
+ ? 0
38
+ : Math.max(
39
+ 0,
40
+ tables.findIndex((t) => t.tableName === exclusiveStartTableName) +
41
+ 1,
42
+ );
43
+
44
+ const page = tables.slice(startIndex, startIndex + limit);
45
+
46
+ return {
47
+ TableNames: page.map((table) => table.tableName),
48
+ LastEvaluatedTableName: page.at(-1)?.tableName,
49
+ };
50
+ }
51
+ }
@@ -0,0 +1,74 @@
1
+ import { describe, it } from "vitest";
2
+ import { SimAwsAccount } from "../../../organizations/sim-aws-account.js";
3
+ import {
4
+ CreateTableCommand,
5
+ ListTablesCommand,
6
+ } from "@aws-sdk/client-dynamodb";
7
+ import { assertArrayLength, assertIdentical } from "@kensio/smartass";
8
+
9
+ describe("DynamoDB ListTablesCommand", () => {
10
+ it("List all DynamoDB Tables", async () => {
11
+ const simAccount = new SimAwsAccount();
12
+ const simDynamoDb = simAccount.getDynamoDb();
13
+
14
+ await Promise.all([
15
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableA" })),
16
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableB" })),
17
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableC" })),
18
+ ]);
19
+
20
+ const listTablesOutput = await simDynamoDb.listTables(
21
+ new ListTablesCommand(),
22
+ );
23
+
24
+ assertArrayLength(listTablesOutput.TableNames, 3);
25
+
26
+ assertIdentical(listTablesOutput.TableNames[0], "TableA");
27
+ assertIdentical(listTablesOutput.TableNames[1], "TableB");
28
+ assertIdentical(listTablesOutput.TableNames[2], "TableC");
29
+
30
+ assertIdentical(listTablesOutput.LastEvaluatedTableName, "TableC");
31
+ });
32
+
33
+ it("List DynamoDB Tables with limit", async () => {
34
+ const simAccount = new SimAwsAccount();
35
+ const simDynamoDb = simAccount.getDynamoDb();
36
+
37
+ await Promise.all([
38
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableA" })),
39
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableB" })),
40
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableC" })),
41
+ ]);
42
+
43
+ const listTablesOutput = await simDynamoDb.listTables(
44
+ new ListTablesCommand({ Limit: 2 }),
45
+ );
46
+
47
+ assertArrayLength(listTablesOutput.TableNames, 2);
48
+
49
+ assertIdentical(listTablesOutput.TableNames[0], "TableA");
50
+ assertIdentical(listTablesOutput.TableNames[1], "TableB");
51
+
52
+ assertIdentical(listTablesOutput.LastEvaluatedTableName, "TableB");
53
+ });
54
+
55
+ it("List DynamoDB Tables with exclusive start and limit", async () => {
56
+ const simAccount = new SimAwsAccount();
57
+ const simDynamoDb = simAccount.getDynamoDb();
58
+
59
+ await Promise.all([
60
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableA" })),
61
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableB" })),
62
+ simDynamoDb.createTable(new CreateTableCommand({ TableName: "TableC" })),
63
+ ]);
64
+
65
+ const listTablesOutput = await simDynamoDb.listTables(
66
+ new ListTablesCommand({ ExclusiveStartTableName: "TableB", Limit: 2 }),
67
+ );
68
+
69
+ assertArrayLength(listTablesOutput.TableNames, 1);
70
+
71
+ assertIdentical(listTablesOutput.TableNames[0], "TableC");
72
+ assertIdentical(listTablesOutput.LastEvaluatedTableName, "TableC");
73
+ });
74
+ });