92 lines
3.6 KiB
TypeScript
92 lines
3.6 KiB
TypeScript
import { describe, expect, it } from "@effect/vitest";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import { PgDialect } from "drizzle-orm/pg-core";
|
|
|
|
import { RelayDb, type RelayDatabase } from "../db.ts";
|
|
import { relayEnvironmentLinks } from "../persistence/schema.ts";
|
|
import { EnvironmentLinks, layer } from "./EnvironmentLinks.ts";
|
|
|
|
describe("EnvironmentLinks", () => {
|
|
it.effect("selects users when either notifications or Live Activities are enabled", () => {
|
|
const whereConditions: Array<unknown> = [];
|
|
const fakeDb = {
|
|
select: (selection: unknown) => {
|
|
expect(selection).toBeDefined();
|
|
return {
|
|
from: (table: unknown) => {
|
|
expect(table).toBe(relayEnvironmentLinks);
|
|
return {
|
|
where: (condition: unknown) => {
|
|
whereConditions.push(condition);
|
|
return Effect.succeed([]);
|
|
},
|
|
};
|
|
},
|
|
};
|
|
},
|
|
} as unknown as RelayDatabase;
|
|
|
|
return Effect.gen(function* () {
|
|
const links = yield* EnvironmentLinks;
|
|
expect(yield* links.listUsersForEnvironment({ environmentId: "env-1" })).toEqual([]);
|
|
expect(whereConditions).toHaveLength(1);
|
|
|
|
const query = new PgDialect().sqlToQuery(whereConditions[0] as never);
|
|
expect(query.sql).toContain('"relay_environment_links"."environment_id" = $1');
|
|
expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
|
|
expect(query.sql).toContain('"relay_environment_links"."notifications_enabled" = $2');
|
|
expect(query.sql).toContain('"relay_environment_links"."live_activities_enabled" = $3');
|
|
expect(query.sql).toContain(" or ");
|
|
expect(query.params).toEqual(["env-1", true, true]);
|
|
}).pipe(Effect.provide(layer.pipe(Layer.provide(Layer.succeed(RelayDb, fakeDb)))));
|
|
});
|
|
|
|
it.effect("revokes only the active link owned by the requesting user", () => {
|
|
const updateValues: Array<Record<string, unknown>> = [];
|
|
const whereConditions: Array<unknown> = [];
|
|
const fakeDb = {
|
|
update: (table: unknown) => {
|
|
expect(table).toBe(relayEnvironmentLinks);
|
|
return {
|
|
set: (values: Record<string, unknown>) => {
|
|
updateValues.push(values);
|
|
return {
|
|
where: (condition: unknown) => {
|
|
whereConditions.push(condition);
|
|
return {
|
|
returning: (selection: unknown) => {
|
|
expect(selection).toBeDefined();
|
|
return Effect.succeed([{ environmentId: "env-1" }]);
|
|
},
|
|
};
|
|
},
|
|
};
|
|
},
|
|
};
|
|
},
|
|
} as unknown as RelayDatabase;
|
|
|
|
return Effect.gen(function* () {
|
|
const links = yield* EnvironmentLinks;
|
|
const revoked = yield* links.revokeForUser({
|
|
userId: "user-1",
|
|
environmentId: "env-1",
|
|
});
|
|
|
|
expect(revoked).toBe(true);
|
|
expect(updateValues).toHaveLength(1);
|
|
expect(updateValues[0]?.revokedAt).toEqual(updateValues[0]?.updatedAt);
|
|
expect(typeof updateValues[0]?.revokedAt).toBe("string");
|
|
expect(whereConditions).toHaveLength(1);
|
|
|
|
const dialect = new PgDialect();
|
|
const query = dialect.sqlToQuery(whereConditions[0] as never);
|
|
expect(query.sql).toContain('"relay_environment_links"."user_id" = $1');
|
|
expect(query.sql).toContain('"relay_environment_links"."environment_id" = $2');
|
|
expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
|
|
expect(query.params).toEqual(["user-1", "env-1"]);
|
|
}).pipe(Effect.provide(layer.pipe(Layer.provide(Layer.succeed(RelayDb, fakeDb)))));
|
|
});
|
|
});
|