Repository navigation
feat(chat): auto-favorite nodes on direct message #1491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FawwazLawal
wants to merge
4
commits into
meshtastic:main
Choose a base branch
from
FawwazLawal:feat/1240-auto-favorite-on-dm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
abebe8c
feat(chat): auto-favorite nodes on direct message
FawwazLawal 4dab84d
fix(chat): deduplicate autoFavorite calls and cover outbound DMs
FawwazLawal 03c19d3
fix(chat): clear favoriteRequested set after request settles
FawwazLawal 27e5b4b
fix(chat): suppress unhandled rejection on autoFavorite promise
FawwazLawal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
packages/sdk/src/features/chat/ChatClient.autoFavorite.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { create } from "@bufbuild/protobuf"; | ||
| import * as Protobuf from "@meshtastic/protobufs"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { MeshClient } from "../../core/client/MeshClient.ts"; | ||
| import { createFakeTransport } from "../../core/testing/createFakeTransport.ts"; | ||
| import { ChannelNumber } from "../../core/types.ts"; | ||
| import { Result } from "better-result"; | ||
|
|
||
| const MY_NODE = 0xdeadbeef; | ||
|
|
||
| function setMyNode(client: MeshClient): void { | ||
| client.events.onMyNodeInfo.dispatch( | ||
| create(Protobuf.Mesh.MyNodeInfoSchema, { myNodeNum: MY_NODE }), | ||
| ); | ||
| } | ||
|
|
||
| function setDeviceRole( | ||
| client: MeshClient, | ||
| role: Protobuf.Config.Config_DeviceConfig_Role, | ||
| ): void { | ||
| client.events.onConfigPacket.dispatch( | ||
| create(Protobuf.Config.ConfigSchema, { | ||
| payloadVariant: { | ||
| case: "device", | ||
| value: create(Protobuf.Config.Config_DeviceConfigSchema, { role }), | ||
| }, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| function dispatchDirectMessage( | ||
| client: MeshClient, | ||
| opts: { from: number; to: number; ms: number }, | ||
| ): void { | ||
| client.events.onMessagePacket.dispatch({ | ||
| id: opts.ms, | ||
| from: opts.from, | ||
| to: opts.to, | ||
| channel: ChannelNumber.Primary, | ||
| type: "direct", | ||
| rxTime: new Date(opts.ms), | ||
| data: "hello", | ||
| } as never); | ||
| } | ||
|
|
||
| describe("ChatClient auto-favorite on direct message", () => { | ||
| it("auto-favorites the peer on inbound direct message", () => { | ||
| const { transport } = createFakeTransport(); | ||
| const client = new MeshClient({ transport }); | ||
| setMyNode(client); | ||
| setDeviceRole(client, Protobuf.Config.Config_DeviceConfig_Role.CLIENT); | ||
|
|
||
| const favoriteSpy = vi | ||
| .spyOn(client.nodes, "favorite") | ||
| .mockResolvedValue(Result.ok(1)); | ||
|
|
||
| dispatchDirectMessage(client, { from: 100, to: MY_NODE, ms: 1000 }); | ||
|
|
||
| expect(favoriteSpy).toHaveBeenCalledWith(100); | ||
| }); | ||
|
|
||
| it("skips auto-favorite when peer is already favorited", () => { | ||
| const { transport } = createFakeTransport(); | ||
| const client = new MeshClient({ transport }); | ||
| setMyNode(client); | ||
| setDeviceRole(client, Protobuf.Config.Config_DeviceConfig_Role.CLIENT); | ||
|
|
||
| client.events.onNodeInfoPacket.dispatch( | ||
| create(Protobuf.Mesh.NodeInfoSchema, { | ||
| num: 100, | ||
| isFavorite: true, | ||
| }), | ||
| ); | ||
|
|
||
| const favoriteSpy = vi | ||
| .spyOn(client.nodes, "favorite") | ||
| .mockResolvedValue(Result.ok(1)); | ||
|
|
||
| dispatchDirectMessage(client, { from: 100, to: MY_NODE, ms: 1000 }); | ||
|
|
||
| expect(favoriteSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not auto-favorite when local role is CLIENT_BASE", () => { | ||
| const { transport } = createFakeTransport(); | ||
| const client = new MeshClient({ transport }); | ||
| setMyNode(client); | ||
| setDeviceRole(client, Protobuf.Config.Config_DeviceConfig_Role.CLIENT_BASE); | ||
|
|
||
| const favoriteSpy = vi | ||
| .spyOn(client.nodes, "favorite") | ||
| .mockResolvedValue(Result.ok(1)); | ||
|
|
||
| dispatchDirectMessage(client, { from: 200, to: MY_NODE, ms: 1000 }); | ||
|
|
||
| expect(favoriteSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("calls favorite only once for repeated messages from the same peer", () => { | ||
| const { transport } = createFakeTransport(); | ||
| const client = new MeshClient({ transport }); | ||
| setMyNode(client); | ||
| setDeviceRole(client, Protobuf.Config.Config_DeviceConfig_Role.CLIENT); | ||
|
|
||
| const favoriteSpy = vi | ||
| .spyOn(client.nodes, "favorite") | ||
| .mockResolvedValue(Result.ok(1)); | ||
|
|
||
| dispatchDirectMessage(client, { from: 100, to: MY_NODE, ms: 1000 }); | ||
| dispatchDirectMessage(client, { from: 100, to: MY_NODE, ms: 2000 }); | ||
| dispatchDirectMessage(client, { from: 100, to: MY_NODE, ms: 3000 }); | ||
|
|
||
| expect(favoriteSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("re-attempts favorite after the in-flight request settles", async () => { | ||
| const { transport } = createFakeTransport(); | ||
| const client = new MeshClient({ transport }); | ||
| setMyNode(client); | ||
| setDeviceRole(client, Protobuf.Config.Config_DeviceConfig_Role.CLIENT); | ||
|
|
||
| const favoriteSpy = vi | ||
| .spyOn(client.nodes, "favorite") | ||
| .mockResolvedValue(Result.ok(1)); | ||
|
|
||
| dispatchDirectMessage(client, { from: 100, to: MY_NODE, ms: 1000 }); | ||
| expect(favoriteSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(favoriteSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| await new Promise((r) => setTimeout(r, 0)); | ||
|
|
||
| dispatchDirectMessage(client, { from: 100, to: MY_NODE, ms: 2000 }); | ||
| expect(favoriteSpy).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("auto-favorites the peer on outbound direct message via send()", async () => { | ||
| const { transport } = createFakeTransport(); | ||
| const client = new MeshClient({ transport }); | ||
| setMyNode(client); | ||
| setDeviceRole(client, Protobuf.Config.Config_DeviceConfig_Role.CLIENT); | ||
|
|
||
| const favoriteSpy = vi | ||
| .spyOn(client.nodes, "favorite") | ||
| .mockResolvedValue(Result.ok(1)); | ||
|
|
||
| const flush = setInterval(() => { | ||
| for (const item of client.queue.getState()) | ||
| client.queue.processAck(item.id); | ||
| }, 5); | ||
| try { | ||
| await client.chat.send({ text: "hey", destination: 300 }); | ||
| } finally { | ||
| clearInterval(flush); | ||
| } | ||
|
|
||
| expect(favoriteSpy).toHaveBeenCalledWith(300); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.