|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import test from 'node:test'; |
| 3 | +import { |
| 4 | + COMMUNITY_NOTE_THRESHOLD, |
| 5 | + REVIEW_AFTER_COMMUNITY_DELAY_MS, |
| 6 | + REVIEW_MIN_AGE_MS, |
| 7 | + REVIEW_NOTE_THRESHOLD, |
| 8 | + createLifecycleState, |
| 9 | + normalizeLifecycleState, |
| 10 | + recordUniqueNoteSave, |
| 11 | + shouldOfferCommunity, |
| 12 | + shouldRequestReview, |
| 13 | +} from '../src/services/lifecyclePolicy.ts'; |
| 14 | + |
| 15 | +const startedAt = '2026-01-01T00:00:00.000Z'; |
| 16 | + |
| 17 | +function stateWithSaves(count) { |
| 18 | + let state = createLifecycleState(startedAt); |
| 19 | + for (let index = 0; index < count; index += 1) { |
| 20 | + state = recordUniqueNoteSave( |
| 21 | + state, |
| 22 | + `note-${index}`, |
| 23 | + new Date(Date.parse(startedAt) + index * 1000).toISOString(), |
| 24 | + ); |
| 25 | + } |
| 26 | + return state; |
| 27 | +} |
| 28 | + |
| 29 | +test('community prompt becomes eligible on the third unique saved note', () => { |
| 30 | + assert.equal(shouldOfferCommunity(stateWithSaves(COMMUNITY_NOTE_THRESHOLD - 1)), false); |
| 31 | + assert.equal(shouldOfferCommunity(stateWithSaves(COMMUNITY_NOTE_THRESHOLD)), true); |
| 32 | +}); |
| 33 | + |
| 34 | +test('repeated saves of one note do not advance milestones', () => { |
| 35 | + let state = createLifecycleState(startedAt); |
| 36 | + for (let index = 0; index < 20; index += 1) { |
| 37 | + state = recordUniqueNoteSave(state, 'same-note', startedAt); |
| 38 | + } |
| 39 | + assert.deepEqual(state.savedNoteIds, ['same-note']); |
| 40 | + assert.equal(shouldOfferCommunity(state), false); |
| 41 | +}); |
| 42 | + |
| 43 | +test('community prompt never returns after it is resolved', () => { |
| 44 | + const eligible = stateWithSaves(COMMUNITY_NOTE_THRESHOLD); |
| 45 | + for (const communityPromptState of ['joined', 'dismissed']) { |
| 46 | + assert.equal( |
| 47 | + shouldOfferCommunity({ ...eligible, communityPromptState }), |
| 48 | + false, |
| 49 | + ); |
| 50 | + } |
| 51 | +}); |
| 52 | + |
| 53 | +test('review waits for five unique notes, seven days, and community handling', () => { |
| 54 | + const now = Date.parse(startedAt) + REVIEW_MIN_AGE_MS; |
| 55 | + const enoughNotes = stateWithSaves(REVIEW_NOTE_THRESHOLD); |
| 56 | + const handled = { |
| 57 | + ...enoughNotes, |
| 58 | + communityPromptState: 'dismissed', |
| 59 | + communityHandledAt: new Date( |
| 60 | + now - REVIEW_AFTER_COMMUNITY_DELAY_MS, |
| 61 | + ).toISOString(), |
| 62 | + }; |
| 63 | + |
| 64 | + assert.equal( |
| 65 | + shouldRequestReview(handled, '1.0', now - 1), |
| 66 | + false, |
| 67 | + ); |
| 68 | + assert.equal(shouldRequestReview(enoughNotes, '1.0', now), false); |
| 69 | + assert.equal(shouldRequestReview(handled, '1.0', now), true); |
| 70 | +}); |
| 71 | + |
| 72 | +test('review is limited to once per app version', () => { |
| 73 | + const state = { |
| 74 | + ...stateWithSaves(REVIEW_NOTE_THRESHOLD), |
| 75 | + communityPromptState: 'joined', |
| 76 | + communityHandledAt: new Date(Date.parse(startedAt)).toISOString(), |
| 77 | + reviewPromptedVersions: ['1.0'], |
| 78 | + }; |
| 79 | + const now = Date.parse(startedAt) + REVIEW_MIN_AGE_MS; |
| 80 | + assert.equal(shouldRequestReview(state, '1.0', now), false); |
| 81 | + assert.equal(shouldRequestReview(state, '1.1', now), true); |
| 82 | +}); |
| 83 | + |
| 84 | +test('normalization repairs corrupt fields and bounds saved note ids', () => { |
| 85 | + const normalized = normalizeLifecycleState( |
| 86 | + { |
| 87 | + firstSeenAt: 'not-a-date', |
| 88 | + savedNoteIds: ['a', 'a', 'b', 'c', 'd', 'e', 'f'], |
| 89 | + communityPromptState: 'unexpected', |
| 90 | + reviewPromptedVersions: ['1.0', '1.0', '1.1'], |
| 91 | + }, |
| 92 | + startedAt, |
| 93 | + ); |
| 94 | + |
| 95 | + assert.equal(normalized.firstSeenAt, startedAt); |
| 96 | + assert.equal(normalized.savedNoteIds.length, REVIEW_NOTE_THRESHOLD); |
| 97 | + assert.equal(normalized.communityPromptState, 'pending'); |
| 98 | + assert.deepEqual(normalized.reviewPromptedVersions, ['1.0', '1.1']); |
| 99 | +}); |
| 100 | + |
| 101 | +test('normalization recovers the legacy shown state after an interrupted prompt', () => { |
| 102 | + const normalized = normalizeLifecycleState( |
| 103 | + { |
| 104 | + ...stateWithSaves(COMMUNITY_NOTE_THRESHOLD), |
| 105 | + communityPromptState: 'shown', |
| 106 | + }, |
| 107 | + startedAt, |
| 108 | + ); |
| 109 | + |
| 110 | + assert.equal(normalized.communityPromptState, 'pending'); |
| 111 | + assert.equal(shouldOfferCommunity(normalized), true); |
| 112 | +}); |
0 commit comments