refactor(channels): share account action gate resolution

This commit is contained in:
Peter Steinberger
2026-02-18 17:39:05 +00:00
parent b73a2de9f6
commit e702a9eb52
4 changed files with 65 additions and 26 deletions

View File

@@ -0,0 +1,34 @@
import { describe, expect, it } from "vitest";
import { createAccountActionGate } from "./account-action-gate.js";
type TestActions = {
send?: boolean;
reactions?: boolean;
};
describe("createAccountActionGate", () => {
it("prefers account action values over base values", () => {
const gate = createAccountActionGate<TestActions>({
baseActions: { send: false, reactions: true },
accountActions: { send: true },
});
expect(gate("send")).toBe(true);
});
it("falls back to base actions when account actions are unset", () => {
const gate = createAccountActionGate<TestActions>({
baseActions: { reactions: false },
accountActions: {},
});
expect(gate("reactions")).toBe(false);
});
it("uses default value when neither account nor base defines the key", () => {
const gate = createAccountActionGate<TestActions>({
baseActions: {},
accountActions: {},
});
expect(gate("send", false)).toBe(false);
expect(gate("send")).toBe(true);
});
});

View File

@@ -0,0 +1,21 @@
export type ActionGate<T extends Record<string, boolean | undefined>> = (
key: keyof T,
defaultValue?: boolean,
) => boolean;
export function createAccountActionGate<T extends Record<string, boolean | undefined>>(params: {
baseActions?: T;
accountActions?: T;
}): ActionGate<T> {
return (key, defaultValue = true) => {
const accountValue = params.accountActions?.[key];
if (accountValue !== undefined) {
return accountValue;
}
const baseValue = params.baseActions?.[key];
if (baseValue !== undefined) {
return baseValue;
}
return defaultValue;
};
}