Skip to content

Commit bfd9122

Browse files
committed
force select only the matching guide when forced guide key is set
1 parent 9313398 commit bfd9122

2 files changed

Lines changed: 47 additions & 46 deletions

File tree

packages/client/src/clients/guide/client.ts

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -157,29 +157,11 @@ const select = (state: StoreState, filters: SelectFilterParams = {}) => {
157157
const defaultGroup = findDefaultGroup(state.guideGroups);
158158
if (!defaultGroup) return result;
159159

160-
const displaySequence = [...defaultGroup.display_sequence];
160+
const displaySequence = defaultGroup.display_sequence;
161161
const location = state.location;
162162

163-
// If in debug mode, put the forced guide at the beginning of the display sequence.
164-
if (state.debug.forcedGuideKey) {
165-
const forcedKeyIndex = displaySequence.indexOf(state.debug.forcedGuideKey);
166-
if (forcedKeyIndex > -1) {
167-
displaySequence.splice(forcedKeyIndex, 1);
168-
}
169-
displaySequence.unshift(state.debug.forcedGuideKey);
170-
}
171-
172163
for (const [index, guideKey] of displaySequence.entries()) {
173-
let guide = state.guides[guideKey];
174-
175-
// Use preview guide if it exists and matches the forced guide key
176-
if (
177-
state.debug.forcedGuideKey === guideKey &&
178-
state.previewGuides[guideKey]
179-
) {
180-
guide = state.previewGuides[guideKey];
181-
}
182-
164+
const guide = state.previewGuides[guideKey] || state.guides[guideKey];
183165
if (!guide) continue;
184166

185167
const affirmed = predicate(guide, {
@@ -215,11 +197,11 @@ const predicate = (
215197
return false;
216198
}
217199

218-
// Bypass filtering if the debugged guide matches the given filters.
219-
// This should always run AFTER checking the filters but BEFORE
220-
// checking archived status and location rules.
221-
if (debug.forcedGuideKey === guide.key) {
222-
return true;
200+
// If in debug mode with a forced guide key, bypass other filtering and always
201+
// return true for that guide only. This should always run AFTER checking the
202+
// filters but BEFORE checking archived status and location rules.
203+
if (debug.forcedGuideKey) {
204+
return debug.forcedGuideKey === guide.key;
223205
}
224206

225207
if (!guide.active) {
@@ -736,17 +718,8 @@ export class KnockGuideClient {
736718
// callback to a setTimeout, but just to be safe.
737719
this.ensureClearTimeout();
738720

739-
// If in debug mode, try to resolve the forced guide, otherwise return the first non-undefined guide.
740-
let resolved = undefined;
741-
if (this.store.state.debug.forcedGuideKey) {
742-
resolved = this.stage.ordered.find(
743-
(x) => x === this.store.state.debug.forcedGuideKey,
744-
);
745-
}
746-
747-
if (!resolved) {
748-
resolved = this.stage.ordered.find((x) => x !== undefined);
749-
}
721+
// Resolve to the first non-undefined guide in the stage.
722+
const resolved = this.stage.ordered.find((x) => x !== undefined);
750723

751724
this.knock.log(
752725
`[Guide] Closing the current group stage: resolved=${resolved}`,

packages/client/test/clients/guide/guide.test.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,7 @@ describe("KnockGuideClient", () => {
17581758
expect(result!.type).not.toBe("regular-type");
17591759
});
17601760

1761-
test("doesn't return the preview guide when filtered by a different key", () => {
1761+
test("doesn't return any guide when filtered by a different key than forced guide", () => {
17621762
const previewGuide = {
17631763
...mockGuideTwo,
17641764
type: "preview-type",
@@ -1791,10 +1791,12 @@ describe("KnockGuideClient", () => {
17911791
key: "onboarding",
17921792
});
17931793

1794-
expect(result!.key).toBe("onboarding");
1794+
// When forcedGuideKey is set, only that guide is considered, so filtering
1795+
// by a different key returns undefined
1796+
expect(result).toBeUndefined();
17951797
});
17961798

1797-
test("doesn't return the preview guide when filtered by a different type", () => {
1799+
test("doesn't return any guide when filtered by a different type than forced guide", () => {
17981800
const previewGuide = {
17991801
...mockGuideTwo,
18001802
type: "preview-type",
@@ -1827,7 +1829,9 @@ describe("KnockGuideClient", () => {
18271829
type: "banner",
18281830
});
18291831

1830-
expect(result!.key).toBe("system_status");
1832+
// When forcedGuideKey is set, only that guide is considered, so filtering
1833+
// by a different type returns undefined
1834+
expect(result).toBeUndefined();
18311835
});
18321836

18331837
test("does not return a guide inside a throttle window ", () => {
@@ -2131,7 +2135,7 @@ describe("KnockGuideClient", () => {
21312135
expect(result[0]!.key).toBe(mockGuideTwo.key);
21322136
});
21332137

2134-
test("does not return an inactive guide when forced guide key is set", () => {
2138+
test("returns the target guide even if inactive when forced guide key is set", () => {
21352139
const stateWithGuides = {
21362140
guideGroups: [mockDefaultGroup],
21372141
guideGroupDisplayLogs: {},
@@ -2147,17 +2151,41 @@ describe("KnockGuideClient", () => {
21472151
location: undefined,
21482152
counter: 0,
21492153
debug: {
2150-
forcedGuideKey: mockGuideThree.key,
2154+
forcedGuideKey: mockGuideTwo.key,
21512155
previewSessionId: "test-session-id",
21522156
},
21532157
};
21542158

21552159
const client = new KnockGuideClient(mockKnock, channelId);
2156-
const result = client["_selectGuides"](stateWithGuides);
2160+
const result = client["_selectGuides"](stateWithGuides, {
2161+
key: mockGuideTwo.key
2162+
});
21572163

2158-
expect(result).toHaveLength(2);
2159-
expect(result[0]!.key).toBe(mockGuideThree.key);
2160-
expect(result[1]!.key).toBe(mockGuideOne.key);
2164+
expect(result[0]!.key).toBe(mockGuideTwo.key);
2165+
});
2166+
2167+
test("returns only the forced guide when forced guide key is set", () => {
2168+
const stateWithGuides = {
2169+
guideGroups: [mockDefaultGroup],
2170+
guideGroupDisplayLogs: {},
2171+
guides: mockGuides,
2172+
previewGuides: {},
2173+
queries: {},
2174+
location: undefined,
2175+
counter: 0,
2176+
debug: {
2177+
forcedGuideKey: mockGuideOne.key,
2178+
previewSessionId: "test-session-id",
2179+
},
2180+
};
2181+
2182+
const client = new KnockGuideClient(mockKnock, channelId);
2183+
const result = client["_selectGuides"](stateWithGuides, {
2184+
type: "card"
2185+
});
2186+
2187+
expect(result).toHaveLength(1);
2188+
expect(result[0]!.key).toBe(mockGuideOne.key);
21612189
});
21622190

21632191
test("returns empty array when inside throttle window by default", () => {

0 commit comments

Comments
 (0)