11// Imessage tests cover the downtime-recovery cursor.
22import { createHash } from "node:crypto" ;
3+ import os from "node:os" ;
4+ import path from "node:path" ;
35import { beforeEach , describe , expect , it } from "vitest" ;
46import { getIMessageRuntime } from "../runtime.js" ;
57import { installIMessageStateRuntimeForTest } from "../test-support/runtime.js" ;
6- import { advanceIMessageRecoveryCursor , loadIMessageRecoveryCursor } from "./recovery-cursor.js" ;
8+ import {
9+ advanceIMessageRecoveryCursor ,
10+ IMESSAGE_RECOVERY_CURSOR_MAX_ENTRIES ,
11+ IMESSAGE_RECOVERY_CURSOR_NAMESPACE ,
12+ loadIMessageRecoveryCursor ,
13+ resolveIMessageRecoveryCursorDbIdentity ,
14+ } from "./recovery-cursor.js" ;
15+
16+ // Default database identity used by tests that are not exercising the db-scoping
17+ // behavior directly.
18+ const DB = "local:/db-a" ;
19+ const DB_B = "local:/db-b" ;
720
821function writeLegacyCatchupCursor ( accountId : string , lastSeenRowid : number ) : void {
922 const store = getIMessageRuntime ( ) . state . openSyncKeyedStore < {
@@ -14,58 +27,140 @@ function writeLegacyCatchupCursor(accountId: string, lastSeenRowid: number): voi
1427 store . register ( key , { lastSeenMs : Date . now ( ) , lastSeenRowid } ) ;
1528}
1629
30+ // Writes a pre-database-scoping recovery cursor: keyed by accountId alone, with
31+ // no database identity, as older builds persisted it.
32+ function writeLegacyRecoveryCursor ( accountId : string , lastRowid : number ) : void {
33+ getIMessageRuntime ( )
34+ . state . openSyncKeyedStore < { lastRowid : number } > ( {
35+ namespace : IMESSAGE_RECOVERY_CURSOR_NAMESPACE ,
36+ maxEntries : IMESSAGE_RECOVERY_CURSOR_MAX_ENTRIES ,
37+ } )
38+ . register ( accountId , { lastRowid } ) ;
39+ }
40+
1741describe ( "iMessage recovery cursor" , ( ) => {
1842 beforeEach ( ( ) => {
1943 installIMessageStateRuntimeForTest ( ) ;
2044 } ) ;
2145
2246 it ( "returns null before anything is recorded" , ( ) => {
23- expect ( loadIMessageRecoveryCursor ( "default" ) ) . toBeNull ( ) ;
47+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBeNull ( ) ;
2448 } ) ;
2549
2650 it ( "persists the last dispatched rowid" , ( ) => {
27- advanceIMessageRecoveryCursor ( "default" , 100 ) ;
28- expect ( loadIMessageRecoveryCursor ( "default" ) ) . toBe ( 100 ) ;
51+ advanceIMessageRecoveryCursor ( "default" , DB , 100 ) ;
52+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 100 ) ;
2953 } ) ;
3054
3155 it ( "advances forward only and never rewinds" , ( ) => {
32- advanceIMessageRecoveryCursor ( "default" , 100 ) ;
33- advanceIMessageRecoveryCursor ( "default" , 50 ) ;
34- expect ( loadIMessageRecoveryCursor ( "default" ) ) . toBe ( 100 ) ;
35- advanceIMessageRecoveryCursor ( "default" , 150 ) ;
36- expect ( loadIMessageRecoveryCursor ( "default" ) ) . toBe ( 150 ) ;
56+ advanceIMessageRecoveryCursor ( "default" , DB , 100 ) ;
57+ advanceIMessageRecoveryCursor ( "default" , DB , 50 ) ;
58+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 100 ) ;
59+ advanceIMessageRecoveryCursor ( "default" , DB , 150 ) ;
60+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 150 ) ;
3761 } ) ;
3862
3963 it ( "scopes the cursor per account" , ( ) => {
40- advanceIMessageRecoveryCursor ( "work" , 10 ) ;
41- advanceIMessageRecoveryCursor ( "home" , 20 ) ;
42- expect ( loadIMessageRecoveryCursor ( "work" ) ) . toBe ( 10 ) ;
43- expect ( loadIMessageRecoveryCursor ( "home" ) ) . toBe ( 20 ) ;
64+ advanceIMessageRecoveryCursor ( "work" , DB , 10 ) ;
65+ advanceIMessageRecoveryCursor ( "home" , DB , 20 ) ;
66+ expect ( loadIMessageRecoveryCursor ( "work" , DB ) ) . toBe ( 10 ) ;
67+ expect ( loadIMessageRecoveryCursor ( "home" , DB ) ) . toBe ( 20 ) ;
68+ } ) ;
69+
70+ it ( "ignores a cursor recorded against a different database (#99638)" , ( ) => {
71+ // A high-water from db-a must not seed since_rowid after repointing to db-b,
72+ // or every lower rowid in db-b is silently suppressed forever.
73+ advanceIMessageRecoveryCursor ( "default" , DB , 12396 ) ;
74+ expect ( loadIMessageRecoveryCursor ( "default" , DB_B , { migrateLegacyCatchup : false } ) ) . toBeNull ( ) ;
75+ // The original database still reports its cursor.
76+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 12396 ) ;
77+ } ) ;
78+
79+ it ( "re-scopes the cursor to the new database on advance" , ( ) => {
80+ advanceIMessageRecoveryCursor ( "default" , DB , 12396 ) ;
81+ // Advancing on db-b starts fresh (not blocked by db-a's higher monotonic value).
82+ advanceIMessageRecoveryCursor ( "default" , DB_B , 15 ) ;
83+ expect ( loadIMessageRecoveryCursor ( "default" , DB_B ) ) . toBe ( 15 ) ;
84+ // db-a keeps its own high-water; switching back does not lose it.
85+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 12396 ) ;
86+ } ) ;
87+
88+ it ( "adopts a pre-database-scoping cursor once for the active database (#99638)" , ( ) => {
89+ writeLegacyRecoveryCursor ( "default" , 12396 ) ;
90+ // Upgrade restart: the identity-less cursor is adopted for the active
91+ // database so downtime replay still works (not dropped to the watermark).
92+ expect ( loadIMessageRecoveryCursor ( "default" , DB , { migrateLegacyCatchup : false } ) ) . toBe ( 12396 ) ;
93+ // It is consumed and re-scoped, so a different database does not inherit it.
94+ expect ( loadIMessageRecoveryCursor ( "default" , DB_B , { migrateLegacyCatchup : false } ) ) . toBeNull ( ) ;
95+ // The adopted database keeps it across reloads.
96+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 12396 ) ;
4497 } ) ;
4598
4699 it ( "ignores non-finite rowids" , ( ) => {
47- advanceIMessageRecoveryCursor ( "default" , Number . NaN ) ;
48- expect ( loadIMessageRecoveryCursor ( "default" ) ) . toBeNull ( ) ;
100+ advanceIMessageRecoveryCursor ( "default" , DB , Number . NaN ) ;
101+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBeNull ( ) ;
49102 } ) ;
50103
51104 it ( "seeds from the retired catchup cursor once on upgrade, then consumes it" , ( ) => {
52105 writeLegacyCatchupCursor ( "default" , 4321 ) ;
53106 // First load with no recovery cursor seeds from the legacy catchup cursor.
54- expect ( loadIMessageRecoveryCursor ( "default" ) ) . toBe ( 4321 ) ;
107+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 4321 ) ;
55108 // The legacy entry is consumed and the value is now the recovery cursor, so
56109 // a later load still returns it without re-reading the legacy store.
57- expect ( loadIMessageRecoveryCursor ( "default" ) ) . toBe ( 4321 ) ;
110+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 4321 ) ;
58111 } ) ;
59112
60113 it ( "can skip legacy catchup cursor migration when compatibility catchup still owns it" , ( ) => {
61114 writeLegacyCatchupCursor ( "default" , 4321 ) ;
62- expect ( loadIMessageRecoveryCursor ( "default" , { migrateLegacyCatchup : false } ) ) . toBeNull ( ) ;
63- expect ( loadIMessageRecoveryCursor ( "default" ) ) . toBe ( 4321 ) ;
115+ expect ( loadIMessageRecoveryCursor ( "default" , DB , { migrateLegacyCatchup : false } ) ) . toBeNull ( ) ;
116+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 4321 ) ;
64117 } ) ;
65118
66119 it ( "prefers an existing recovery cursor over the legacy catchup cursor" , ( ) => {
67- advanceIMessageRecoveryCursor ( "default" , 9000 ) ;
120+ advanceIMessageRecoveryCursor ( "default" , DB , 9000 ) ;
68121 writeLegacyCatchupCursor ( "default" , 10 ) ;
69- expect ( loadIMessageRecoveryCursor ( "default" ) ) . toBe ( 9000 ) ;
122+ expect ( loadIMessageRecoveryCursor ( "default" , DB ) ) . toBe ( 9000 ) ;
123+ } ) ;
124+
125+ it ( "unifies the implicit default with explicit spellings of the same chat.db (#99638)" , ( ) => {
126+ const home = ( process . env . HOME || os . homedir ( ) ) . trim ( ) ;
127+ const defaultIdentity = `local:${ path . resolve ( path . join ( home , "Library" , "Messages" , "chat.db" ) ) } ` ;
128+
129+ // Implicit default (imsg binary, no dbPath) and any explicit spelling of the
130+ // same file resolve to one identity, so switching between them does not drop
131+ // the cursor and skip downtime messages.
132+ expect ( resolveIMessageRecoveryCursorDbIdentity ( { cliPath : "imsg" } ) ) . toBe ( defaultIdentity ) ;
133+ expect ( resolveIMessageRecoveryCursorDbIdentity ( { cliPath : "/opt/homebrew/bin/imsg" } ) ) . toBe (
134+ defaultIdentity ,
135+ ) ;
136+ expect (
137+ resolveIMessageRecoveryCursorDbIdentity ( {
138+ dbPath : path . join ( home , "Library/Messages/chat.db" ) ,
139+ } ) ,
140+ ) . toBe ( defaultIdentity ) ;
141+ expect ( resolveIMessageRecoveryCursorDbIdentity ( { dbPath : "~/Library/Messages/chat.db" } ) ) . toBe (
142+ defaultIdentity ,
143+ ) ;
144+ } ) ;
145+
146+ it ( "keeps distinct databases, wrappers, and remotes on separate identities" , ( ) => {
147+ const defaultId = resolveIMessageRecoveryCursorDbIdentity ( { cliPath : "imsg" } ) ;
148+ const otherDb = resolveIMessageRecoveryCursorDbIdentity ( { dbPath : "/Users/other/chat.db" } ) ;
149+ // Distinct wrappers with no dbPath/remoteHost must not collapse together.
150+ const wrapperA = resolveIMessageRecoveryCursorDbIdentity ( {
151+ cliPath : "/usr/local/bin/imsg-a.sh" ,
152+ } ) ;
153+ const wrapperB = resolveIMessageRecoveryCursorDbIdentity ( {
154+ cliPath : "/usr/local/bin/imsg-b.sh" ,
155+ } ) ;
156+ const remote = resolveIMessageRecoveryCursorDbIdentity ( {
157+ remoteHost : "bot@host" ,
158+ dbPath : "/Users/b/chat.db" ,
159+ } ) ;
160+ expect ( new Set ( [ defaultId , otherDb , wrapperA , wrapperB , remote ] ) . size ) . toBe ( 5 ) ;
161+ // Stable for the same inputs.
162+ expect ( otherDb ) . toBe (
163+ resolveIMessageRecoveryCursorDbIdentity ( { dbPath : "/Users/other/chat.db" } ) ,
164+ ) ;
70165 } ) ;
71166} ) ;
0 commit comments