tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_MemoriesConversationScheduler.js (4789B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 do_get_profile();
      6 ("use strict");
      7 
      8 const { sinon } = ChromeUtils.importESModule(
      9  "resource://testing-common/Sinon.sys.mjs"
     10 );
     11 const { MemoriesConversationScheduler } = ChromeUtils.importESModule(
     12  "moz-src:///browser/components/aiwindow/models/memories/MemoriesConversationScheduler.sys.mjs"
     13 );
     14 const { MemoriesManager } = ChromeUtils.importESModule(
     15  "moz-src:///browser/components/aiwindow/models/memories/MemoriesManager.sys.mjs"
     16 );
     17 const { PREF_GENERATE_MEMORIES } = ChromeUtils.importESModule(
     18  "moz-src:///browser/components/aiwindow/models/memories/MemoriesConstants.sys.mjs"
     19 );
     20 const { ChatStore, ChatMessage, MESSAGE_ROLE } = ChromeUtils.importESModule(
     21  "moz-src:///browser/components/aiwindow/ui/modules/ChatStore.sys.mjs"
     22 );
     23 
     24 // Clear memories pref after testing
     25 add_setup(async function () {
     26  registerCleanupFunction(() => {
     27    Services.prefs.clearUserPref(PREF_GENERATE_MEMORIES);
     28  });
     29 });
     30 
     31 /**
     32 * Builds fake chat history data for testing
     33 *
     34 * @param {number} numMessagesToCreate  Number of user messages to create (default: 10)
     35 * @returns {Promise<ChatMessage[]>}    Array of ChatMessage instances
     36 */
     37 async function buildFakeChatHistory(numMessagesToCreate = 10) {
     38  const fixedNow = 1_700_000_000_000;
     39 
     40  let messages = [];
     41  for (let i = 0; i < numMessagesToCreate; i++) {
     42    messages.push(
     43      new ChatMessage({
     44        createdDate: fixedNow - i * 10_000,
     45        ordinal: i + 1,
     46        role: MESSAGE_ROLE.USER,
     47        content: { type: "text", body: `Test message ${i + 1}` },
     48        pageUrl: `https://example.com/${i + 1}`,
     49        turnIndex: 0,
     50      })
     51    );
     52  }
     53 
     54  return messages;
     55 }
     56 
     57 /**
     58 * Tests the scheduler does not initialize when the memories preference is false
     59 */
     60 add_task(async function test_schedule_not_init_when_pref_false() {
     61  Services.prefs.setBoolPref(PREF_GENERATE_MEMORIES, false);
     62 
     63  let scheduler = MemoriesConversationScheduler.maybeInit();
     64  Assert.equal(
     65    scheduler,
     66    null,
     67    "Scheduler should not be initialized when pref is false"
     68  );
     69 });
     70 
     71 /**
     72 * Tests the scheduler initializes but does not run when there aren't enough messages
     73 */
     74 add_task(async function test_scheduler_doesnt_run_with_insufficient_messages() {
     75  Services.prefs.setBoolPref(PREF_GENERATE_MEMORIES, true);
     76 
     77  // Need at least 10 messages for memories generation to trigger
     78  // 5 will cause the expected failure
     79  const messages = await buildFakeChatHistory(5);
     80  const sb = sinon.createSandbox();
     81 
     82  try {
     83    const findMessagesStub = sb
     84      .stub(ChatStore.prototype, "findMessagesByDate")
     85      .callsFake(async () => {
     86        return messages;
     87      });
     88 
     89    const lastTsStub = sb
     90      .stub(MemoriesManager, "getLastConversationMemoryTimestamp")
     91      .resolves(0);
     92 
     93    const generateStub = sb
     94      .stub(MemoriesManager, "generateMemoriesFromConversationHistory")
     95      .resolves();
     96 
     97    let scheduler = MemoriesConversationScheduler.maybeInit();
     98    Assert.ok(scheduler, "Scheduler should be initialized when pref is true");
     99 
    100    await scheduler.runNowForTesting();
    101    Assert.ok(
    102      findMessagesStub.calledOnce,
    103      "Should check for recent messages once"
    104    );
    105    Assert.ok(lastTsStub.calledOnce, "Should check last memory timestamp once");
    106    Assert.ok(
    107      !generateStub.calledOnce,
    108      "Memories generation should not be triggered with only 5 messages"
    109    );
    110  } finally {
    111    sb.restore();
    112  }
    113 });
    114 
    115 /**
    116 * Tests the scheduler initializes and runs when there are enough messages
    117 */
    118 add_task(async function test_scheduler_runs_with_small_history() {
    119  Services.prefs.setBoolPref(PREF_GENERATE_MEMORIES, true);
    120 
    121  const messages = await buildFakeChatHistory();
    122  const sb = sinon.createSandbox();
    123 
    124  try {
    125    const findMessagesStub = sb
    126      .stub(ChatStore.prototype, "findMessagesByDate")
    127      .callsFake(async () => {
    128        return messages;
    129      });
    130 
    131    const lastTsStub = sb
    132      .stub(MemoriesManager, "getLastConversationMemoryTimestamp")
    133      .resolves(0);
    134 
    135    const generateStub = sb
    136      .stub(MemoriesManager, "generateMemoriesFromConversationHistory")
    137      .resolves();
    138 
    139    let scheduler = MemoriesConversationScheduler.maybeInit();
    140    Assert.ok(scheduler, "Scheduler should be initialized when pref is true");
    141 
    142    await scheduler.runNowForTesting();
    143    Assert.ok(
    144      findMessagesStub.calledOnce,
    145      "Should check for recent messages once"
    146    );
    147    Assert.ok(lastTsStub.calledOnce, "Should check last memory timestamp once");
    148    Assert.ok(
    149      generateStub.calledOnce,
    150      "Memories generation should be triggered once"
    151    );
    152  } finally {
    153    sb.restore();
    154  }
    155 });