Skip to content
Merged
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
36 changes: 34 additions & 2 deletions packages/client/src/clients/guide/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ const predicate = (
// If in debug mode with a forced guide key, bypass other filtering and always
// return true for that guide only. This should always run AFTER checking the
// filters but BEFORE checking archived status and location rules.
if (
debug.focusedGuideKeys &&
Object.keys(debug.focusedGuideKeys).length > 0
) {
return !!debug.focusedGuideKeys[guide.key];
}
if (debug.forcedGuideKey) {
return debug.forcedGuideKey === guide.key;
}
Expand Down Expand Up @@ -563,7 +569,11 @@ export class KnockGuideClient {
// Clear debug state from store
this.store.setState((state) => ({
...state,
debug: { forcedGuideKey: null, previewSessionId: null },
debug: {
forcedGuideKey: null,
previewSessionId: null,
focusedGuideKeys: {},
},
previewGuides: {}, // Clear preview guides when exiting debug mode
}));

Expand Down Expand Up @@ -591,6 +601,7 @@ export class KnockGuideClient {
debug: {
skipEngagementTracking: true,
ignoreDisplayInterval: true,
focusedGuideKeys: {},
...debugOpts,
debugging: true,
},
Expand Down Expand Up @@ -754,6 +765,10 @@ export class KnockGuideClient {
return guide;
}

// If focused while in debug mode, then we want to ignore the guide order
// and throttle settings and force render this guide.
const focusedInDebug = state.debug?.focusedGuideKeys?.[guide.key];

const throttled = !opts.includeThrottled && checkStateIfThrottled(state);

switch (this.stage.status) {
Expand All @@ -770,6 +785,13 @@ export class KnockGuideClient {
// we can re-resolve when the group stage closes.
this.stage.ordered[index] = guide.key;

if (focusedInDebug) {
this.knock.log(
`[Guide] Focused to return \`${guide.key}\` (stage: ${formatGroupStage(this.stage)})`,
);
return guide;
}

if (throttled) {
this.knock.log(`[Guide] Throttling the selected guide: ${guide.key}`);
return undefined;
Expand All @@ -783,6 +805,13 @@ export class KnockGuideClient {
}

case "closed": {
if (focusedInDebug) {
this.knock.log(
`[Guide] Focused to return \`${guide.key}\` (stage: ${formatGroupStage(this.stage)})`,
);
return guide;
}

if (throttled) {
this.knock.log(`[Guide] Throttling the selected guide: ${guide.key}`);
return undefined;
Expand Down Expand Up @@ -1082,7 +1111,10 @@ export class KnockGuideClient {
// Get the next unarchived step.
getStep() {
// If debugging this guide, return the first step regardless of archive status
if (self.store.state.debug?.forcedGuideKey === this.key) {
if (
self.store.state.debug?.forcedGuideKey === this.key ||
self.store.state.debug?.focusedGuideKeys?.[this.key]
) {
return this.steps[0];
}

Expand Down
1 change: 1 addition & 0 deletions packages/client/src/clients/guide/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export type QueryStatus = {
export type DebugState = {
debugging?: boolean;
forcedGuideKey?: string | null;
focusedGuideKeys?: Record<KnockGuide["key"], true>;
previewSessionId?: string | null;
skipEngagementTracking?: boolean;
ignoreDisplayInterval?: boolean;
Expand Down
256 changes: 256 additions & 0 deletions packages/client/test/clients/guide/guide.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2260,6 +2260,116 @@ describe("KnockGuideClient", () => {
expect(result).toBeUndefined();
});

test("returns an archived guide when focusedGuideKeys includes it", () => {
const archivedGuide = {
...mockGuideThree,
steps: [
{
...mockStep,
message: {
...mockStep.message,
archived_at: new Date().toISOString(),
},
},
],
};

const stateWithArchivedGuide = {
guideGroups: [mockDefaultGroup],
guideGroupDisplayLogs: {},
guides: {
...mockGuides,
[mockGuideThree.key]: archivedGuide,
},
ineligibleGuides: {},
previewGuides: {},
queries: {},
location: undefined,
counter: 0,
debug: {
focusedGuideKeys: { [mockGuideThree.key]: true as const },
},
};

const client = new KnockGuideClient(mockKnock, channelId);
const result = client["_selectGuide"](stateWithArchivedGuide, {
key: mockGuideThree.key,
});

// Should return the focused guide even though it's archived
expect(result!.key).toBe("system_status");
expect(result!.steps[0]!.message.archived_at).toBeTruthy();
});

test("returns only focused guides when focusedGuideKeys is set", () => {
const stateWithGuides = {
guideGroups: [mockDefaultGroup],
guideGroupDisplayLogs: {},
guides: mockGuides,
ineligibleGuides: {},
previewGuides: {},
queries: {},
location: undefined,
counter: 0,
debug: {
focusedGuideKeys: { [mockGuideTwo.key]: true as const },
},
};

const client = new KnockGuideClient(mockKnock, channelId);
const result = client["_selectGuide"](stateWithGuides);

// Should return the focused guide
expect(result!.key).toBe("feature_tour");
});

test("doesn't return a guide not in focusedGuideKeys", () => {
const stateWithGuides = {
guideGroups: [mockDefaultGroup],
guideGroupDisplayLogs: {},
guides: mockGuides,
ineligibleGuides: {},
previewGuides: {},
queries: {},
location: undefined,
counter: 0,
debug: {
focusedGuideKeys: { [mockGuideTwo.key]: true as const },
},
};

const client = new KnockGuideClient(mockKnock, channelId);
const result = client["_selectGuide"](stateWithGuides, {
key: mockGuideOne.key,
});

// Guide one is not in focusedGuideKeys, so should not be returned
expect(result).toBeUndefined();
});

test("falls through to normal filtering when focusedGuideKeys is empty object", () => {
const stateWithGuides = {
guideGroups: [mockDefaultGroup],
guideGroupDisplayLogs: {},
guides: mockGuides,
ineligibleGuides: {},
previewGuides: {},
queries: {},
location: undefined,
counter: 0,
debug: {
focusedGuideKeys: {},
},
};

const client = new KnockGuideClient(mockKnock, channelId);
const result = client["_selectGuide"](stateWithGuides);

// Empty focusedGuideKeys should not filter — normal selection applies
expect(result).toBeDefined();
expect(result!.key).toBe("feature_tour");
});

test("does not return a guide inside a throttle window ", () => {
const stateWithGuides = {
guideGroups: [
Expand Down Expand Up @@ -4217,6 +4327,152 @@ describe("KnockGuideClient", () => {
expect(result).toBeDefined();
expect(result!.key).toBe("onboarding");
});

test("returns focused guide in closed stage even when throttled", () => {
const stateWithGuides = {
guideGroups: [throttleDefaultGroup],
guideGroupDisplayLogs: {
default: new Date().toISOString(),
},
guides: { onboarding: mockGuide },
ineligibleGuides: {},
previewGuides: {},
queries: {},
location: undefined,
counter: 0,
debug: {
focusedGuideKeys: { onboarding: true as const },
},
};

const client = new KnockGuideClient(mockKnock, channelId);

// Set up a closed stage with the guide resolved
client["stage"] = {
status: "closed",
ordered: ["onboarding"],
resolved: "onboarding",
results: {},
timeoutId: null,
};

// Focused guides bypass throttle in closed stage
const result = client.selectGuide(stateWithGuides, {
key: "onboarding",
});
expect(result).toBeDefined();
expect(result!.key).toBe("onboarding");
});

test("returns focused guide in patch stage even when throttled", () => {
const stateWithGuides = {
guideGroups: [throttleDefaultGroup],
guideGroupDisplayLogs: {
default: new Date().toISOString(),
},
guides: { onboarding: mockGuide },
ineligibleGuides: {},
previewGuides: {},
queries: {},
location: undefined,
counter: 0,
debug: {
focusedGuideKeys: { onboarding: true as const },
},
};

const client = new KnockGuideClient(mockKnock, channelId);

// Set up a closed stage then patch it
client["stage"] = {
status: "closed",
ordered: ["onboarding"],
resolved: "onboarding",
results: {},
timeoutId: null,
};
client["patchClosedGroupStage"]();

expect(client.getStage()!.status).toBe("patch");

// Focused guides bypass throttle in patch stage
const result = client.selectGuide(stateWithGuides, {
key: "onboarding",
});
expect(result).toBeDefined();
expect(result!.key).toBe("onboarding");
});

test("returns focused guide in patch stage even when not the resolved guide", () => {
const stateWithGuides = {
guideGroups: [throttleDefaultGroup],
guideGroupDisplayLogs: {},
guides: { onboarding: mockGuide },
ineligibleGuides: {},
previewGuides: {},
queries: {},
location: undefined,
counter: 0,
debug: {
focusedGuideKeys: { onboarding: true as const },
},
};

const client = new KnockGuideClient(mockKnock, channelId);

// Set up a closed stage where a DIFFERENT guide was resolved
client["stage"] = {
status: "closed",
ordered: ["onboarding"],
resolved: "some_other_guide",
results: {},
timeoutId: null,
};
client["patchClosedGroupStage"]();

expect(client.getStage()!.status).toBe("patch");

// Focused guides bypass the resolved check in patch stage
const result = client.selectGuide(stateWithGuides, {
key: "onboarding",
});
expect(result).toBeDefined();
expect(result!.key).toBe("onboarding");
});

test("returns focused guide in closed stage even when not the resolved guide", () => {
const stateWithGuides = {
guideGroups: [throttleDefaultGroup],
guideGroupDisplayLogs: {},
guides: { onboarding: mockGuide },
ineligibleGuides: {},
previewGuides: {},
queries: {},
location: undefined,
counter: 0,
debug: {
focusedGuideKeys: { onboarding: true as const },
},
};

const client = new KnockGuideClient(mockKnock, channelId);

// Set up a closed stage where a DIFFERENT guide was resolved
client["stage"] = {
status: "closed",
ordered: ["onboarding"],
resolved: "some_other_guide",
results: {},
timeoutId: null,
};

// Focused guides bypass the resolved check in closed stage
const result = client.selectGuide(stateWithGuides, {
key: "onboarding",
});
expect(result).toBeDefined();
expect(result!.key).toBe("onboarding");
});
});

describe("setDebug", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ export const GuideContextDetails = () => {
const { defaultGroup, debugSettings } = useStore(client.store, (state) => {
return {
defaultGroup: state.guideGroups[0],
debugSettings: {
skipEngagementTracking: !!state.debug?.skipEngagementTracking,
ignoreDisplayInterval: !!state.debug?.ignoreDisplayInterval,
},
debugSettings: state.debug || {},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use the whole debug object to make life simpler.

};
});
const displayInterval = defaultGroup?.display_interval ?? null;
Expand Down
Loading
Loading