Skip to content

Commit

Permalink
test: test case with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
floydspace committed Sep 7, 2024
1 parent 823fcde commit 80245a5
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions packages/secrets-manager/test/ConfigProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
SecretsManagerClientInstance,
} from "@effect-aws/client-secrets-manager";
import { Arg, Substitute } from "@fluffy-spoon/substitute";
import { Config, ConfigError, Effect, Exit, Layer } from "effect";
import { Config, ConfigError, Effect, Exit, Layer, Secret } from "effect";
import { describe, expect, it } from "vitest";
import { fromSecretsManager } from "../src/ConfigProvider";

describe("fromSecretsManager", () => {
Expand All @@ -29,16 +30,43 @@ describe("fromSecretsManager", () => {
);

const result = await Config.string("test").pipe(
Effect.provide(
Layer.setConfigProvider(fromSecretsManager({ serviceLayer })),
),
Effect.withConfigProvider(fromSecretsManager({ serviceLayer })),
Effect.runPromiseExit,
);

expect(result).toEqual(Exit.succeed("mocked-secret"));
clientSubstitute.received(1).send(Arg.any(), {});
});

it("should load default value if the secret does not exist", async () => {
const clientSubstitute = Substitute.for<SecretsManagerClient>();
clientSubstitute.send(Arg.all()).rejects(
new ResourceNotFoundException({
$metadata: {},
message: "mocked-error",
}),
);

const clientInstanceLayer = Layer.succeed(
SecretsManagerClientInstance,
clientSubstitute,
);
const serviceLayer = Layer.provide(
BaseSecretsManagerServiceLayer,
clientInstanceLayer,
);

const result = await Config.secret("my-secret-that-doesnt-exist").pipe(
Config.withDefault(Secret.fromString("mocked-default-value")),
Effect.withConfigProvider(fromSecretsManager({ serviceLayer })),
Effect.map(Secret.value),
Effect.runPromiseExit,
);

expect(result).toEqual(Exit.succeed("mocked-default-value"));
clientSubstitute.received(1).send(Arg.any(), {});
});

it("should fail if the secret does not exist", async () => {
const clientSubstitute = Substitute.for<SecretsManagerClient>();
clientSubstitute.send(Arg.all()).rejects(
Expand All @@ -58,9 +86,7 @@ describe("fromSecretsManager", () => {
);

const result = await Config.string("test").pipe(
Effect.provide(
Layer.setConfigProvider(fromSecretsManager({ serviceLayer })),
),
Effect.withConfigProvider(fromSecretsManager({ serviceLayer })),
Effect.runPromiseExit,
);

Expand Down Expand Up @@ -92,9 +118,7 @@ describe("fromSecretsManager", () => {
);

const result = await Config.string("test").pipe(
Effect.provide(
Layer.setConfigProvider(fromSecretsManager({ serviceLayer })),
),
Effect.withConfigProvider(fromSecretsManager({ serviceLayer })),
Effect.runPromiseExit,
);

Expand Down

0 comments on commit 80245a5

Please sign in to comment.