Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/commands/apps/rules/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ export default class RulesCreateCommand extends ControlBaseCommand {

static args = {
ruleName: Args.string({
description: "Name of the rule",
description:
'Match expression selecting the channels the rule applies to, e.g. "chat:*" or "*:presence"',
required: true,
}),
};

static examples = [
'$ ably apps rules create "chat" --persisted',
'$ ably apps rules create "chat" --mutable-messages',
'$ ably apps rules create "events" --push-enabled',
'$ ably apps rules create "notifications" --persisted --push-enabled --app "My App"',
'$ ably apps rules create "chat" --persisted --json',
'$ ably apps rules create "chat:*" --persisted',
'$ ably apps rules create "chat:*" --mutable-messages',
'$ ably apps rules create "*:presence" --push-enabled',
'$ ably apps rules create "notifications:*" --persisted --push-enabled --app "My App"',
'$ ably apps rules create "chat:*" --persisted --json',
];

static flags = {
Expand Down Expand Up @@ -119,6 +120,7 @@ export default class RulesCreateCommand extends ControlBaseCommand {
conflationEnabled: flags["conflation-enabled"],
conflationInterval: flags["conflation-interval"],
conflationKey: flags["conflation-key"],
mode: "matcher" as const,
mutableMessages,
persistLast: flags["persist-last"],
persisted,
Expand All @@ -145,6 +147,7 @@ export default class RulesCreateCommand extends ControlBaseCommand {
conflationKey: createdNamespace.conflationKey ?? null,
created: new Date(createdNamespace.created).toISOString(),
id: createdNamespace.id,
mode: createdNamespace.mode ?? null,
modified: new Date(createdNamespace.modified).toISOString(),
mutableMessages: createdNamespace.mutableMessages || false,
persistLast: createdNamespace.persistLast || false,
Expand Down
7 changes: 6 additions & 1 deletion src/commands/apps/rules/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Flags } from "@oclif/core";
import type { Namespace } from "../../../services/control-api.js";
import type {
ChannelRuleMode,
Namespace,
} from "../../../services/control-api.js";

import { ControlBaseCommand } from "../../../control-base-command.js";
import { formatChannelRuleDetails } from "../../../utils/channel-rule-display.js";
Expand All @@ -18,6 +21,7 @@ interface ChannelRuleOutput {
conflationKey: null | string;
created: string;
id: string;
mode: ChannelRuleMode | null;
modified: string;
mutableMessages: boolean;
persistLast: boolean;
Expand Down Expand Up @@ -76,6 +80,7 @@ export default class RulesListCommand extends ControlBaseCommand {
conflationKey: rule.conflationKey ?? null,
created: new Date(rule.created).toISOString(),
id: rule.id,
mode: rule.mode ?? null,
modified: new Date(rule.modified).toISOString(),
mutableMessages: rule.mutableMessages || false,
persistLast: rule.persistLast || false,
Expand Down
1 change: 1 addition & 0 deletions src/commands/apps/rules/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export default class RulesUpdateCommand extends ControlBaseCommand {
conflationKey: updatedNamespace.conflationKey ?? null,
created: new Date(updatedNamespace.created).toISOString(),
id: updatedNamespace.id,
mode: updatedNamespace.mode ?? null,
modified: new Date(updatedNamespace.modified).toISOString(),
mutableMessages: updatedNamespace.mutableMessages || false,
persistLast: updatedNamespace.persistLast || false,
Expand Down
7 changes: 7 additions & 0 deletions src/services/control-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export interface Key {
status: string;
}

// `matcher` rules select channels by match expression (e.g. "chat:*");
// `namespace` rules predate them and match a first segment plus everything
// beneath it. A rule's mode is fixed at creation.
export type ChannelRuleMode = "matcher" | "namespace";

export interface Namespace {
appId: string;
authenticated?: boolean;
Expand All @@ -76,6 +81,7 @@ export interface Namespace {
created: number;
exposeTimeSerial?: boolean;
id: string;
mode?: ChannelRuleMode;
modified: number;
mutableMessages?: boolean;
persistLast?: boolean;
Expand Down Expand Up @@ -338,6 +344,7 @@ export class ControlApi {
conflationInterval?: number;
conflationKey?: string;
exposeTimeSerial?: boolean;
mode?: ChannelRuleMode;
mutableMessages?: boolean;
persistLast?: boolean;
persisted?: boolean;
Expand Down
10 changes: 8 additions & 2 deletions src/utils/channel-rule-display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ export function formatChannelRuleDetails(
showTimestamps = true,
} = options;
const bool = bold ? boolFieldBold : boolField;
const lines: string[] = [
const lines: string[] = [];

if (rule.mode) {
lines.push(`${indent}${formatLabel("Mode")} ${rule.mode}`);
}

lines.push(
`${indent}${formatLabel("Persisted")} ${bool(rule.persisted)}`,
`${indent}${formatLabel("Push Enabled")} ${bool(rule.pushEnabled)}`,
];
);

if (rule.mutableMessages !== undefined) {
lines.push(
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/control-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export interface MockNamespace {
id: string;
persisted: boolean;
pushEnabled: boolean;
mode?: "matcher" | "namespace";
mutableMessages?: boolean;
created: number;
modified: number;
Expand Down
17 changes: 17 additions & 0 deletions test/unit/commands/apps/rules/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ describe("apps:rules:create command", () => {
expect(result!.rule).toHaveProperty("persisted", true);
expect(result!.rule).toHaveProperty("mutableMessages", true);
});

it("should always create a matcher-mode rule", async () => {
const appId = getMockConfigManager().getCurrentAppId()!;
nockControl()
.post(`/v1/apps/${appId}/namespaces`, (body) => {
return body.id === "chat:*" && body.mode === "matcher";
})
.reply(201, mockNamespace({ id: "chat:*", mode: "matcher" }));

const { stdout } = await runCommand(
["apps:rules:create", "chat:*", "--json"],
import.meta.url,
);

const result = parseNdjsonLines(stdout).find((r) => r.type === "result")!;
expect(result.rule).toHaveProperty("mode", "matcher");
});
});

describe("error handling", () => {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/commands/apps/rules/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ describe("apps:rules:list command", () => {
expect(result).toHaveProperty("success", true);
expect(result.rules).toHaveLength(2);
});

it("should show each rule's mode", async () => {
const appId = getMockConfigManager().getCurrentAppId()!;
nockControl()
.get(`/v1/apps/${appId}/namespaces`)
.times(2)
.reply(200, [
mockNamespace({ id: "chat:*", mode: "matcher" }),
mockNamespace({ id: "legacy", mode: "namespace" }),
]);

const { stdout } = await runCommand(["apps:rules:list"], import.meta.url);
expect(stdout).toContain("Mode: matcher");
expect(stdout).toContain("Mode: namespace");

const { stdout: jsonStdout } = await runCommand(
["apps:rules:list", "--json"],
import.meta.url,
);
const result = parseJsonOutput(jsonStdout);
expect(result.rules.map((r: { mode: string }) => r.mode)).toEqual([
"matcher",
"namespace",
]);
});
});

describe("error handling", () => {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/commands/apps/rules/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,29 @@ describe("apps:rules:update command", () => {
expect(result).toHaveProperty("success", true);
expect(result!.rule).toHaveProperty("persisted", true);
});

it("should not send a mode, so an older namespace-mode rule stays one", async () => {
const appId = getMockConfigManager().getCurrentAppId()!;
nockControl()
.get(`/v1/apps/${appId}/namespaces`)
.reply(200, [mockNamespace({ id: mockRuleId, mode: "namespace" })]);

nockControl()
.patch(`/v1/apps/${appId}/namespaces/${mockRuleId}`, (body) => {
return body.persisted === true && !("mode" in body);
})
.reply(
200,
mockNamespace({ id: mockRuleId, mode: "namespace", persisted: true }),
);

const { stdout } = await runCommand(
["apps:rules:update", mockRuleId, "--persisted"],
import.meta.url,
);

expect(stdout).toContain("Mode: namespace");
});
});

describe("error handling", () => {
Expand Down
Loading