tor-browser

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

test_chat-utils.js (7440B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 do_get_profile();
      5 
      6 const { ChatConversation, ChatMessage } = ChromeUtils.importESModule(
      7  "moz-src:///browser/components/aiwindow/ui/modules/ChatStore.sys.mjs"
      8 );
      9 const {
     10  makeGuid,
     11  parseConversationRow,
     12  parseMessageRows,
     13  parseChatHistoryViewRows,
     14  parseJSONOrNull,
     15  getRoleLabel,
     16 } = ChromeUtils.importESModule(
     17  "moz-src:///browser/components/aiwindow/ui/modules/ChatUtils.sys.mjs"
     18 );
     19 
     20 add_task(function test_makeGuid() {
     21  const guid = makeGuid();
     22 
     23  Assert.equal(guid.length, 12);
     24 });
     25 
     26 /**
     27 * Mock row
     28 */
     29 class RowStub {
     30  constructor(data) {
     31    this.data = data;
     32  }
     33 
     34  getResultByName(key) {
     35    if (Object.hasOwn(this.data, key)) {
     36      return this.data[key];
     37    }
     38 
     39    throw new Error("NS_ERROR_NOT_AVAILABLE");
     40  }
     41 }
     42 
     43 add_task(function test_parseConversationRow() {
     44  const now = Date.now();
     45  const testRow = new RowStub({
     46    conv_id: "123456789012",
     47    title: "the title",
     48    description: "the description",
     49    page_url: "https://www.firefox.com",
     50    page_meta: '{"hello": "world"}',
     51    created_date: now,
     52    updated_date: now,
     53    status: "a status",
     54  });
     55 
     56  const conversation = parseConversationRow(testRow);
     57 
     58  Assert.withSoftAssertions(function (soft) {
     59    // eslint-disable-next-line mozilla/use-isInstance
     60    soft.ok(conversation instanceof ChatConversation);
     61    soft.equal(conversation.id, "123456789012");
     62    soft.equal(conversation.title, "the title");
     63    soft.equal(conversation.description, "the description");
     64    soft.ok(URL.isInstance(conversation.pageUrl));
     65    soft.equal(conversation.pageUrl.href, "https://www.firefox.com/");
     66    soft.deepEqual(conversation.pageMeta, { hello: "world" });
     67    soft.equal(conversation.createdDate, now);
     68    soft.equal(conversation.updatedDate, now);
     69    soft.equal(conversation.status, "a status");
     70  });
     71 });
     72 
     73 add_task(function test_missingField_parseConversationRow() {
     74  const now = Date.now();
     75  const testRow = new RowStub({
     76    title: "the title",
     77    description: "the description",
     78    page_url: "https://www.firefox.com",
     79    page_meta: '{"hello": "world"}',
     80    created_date: now,
     81    updated_date: now,
     82    status: "a status",
     83  });
     84 
     85  Assert.throws(function () {
     86    parseConversationRow(testRow);
     87  }, /NS_ERROR_NOT_AVAILABLE/);
     88 });
     89 
     90 add_task(function test_parseConversationRow() {
     91  const now = Date.now();
     92  const testRow = new RowStub({
     93    message_id: "123456789012",
     94    created_date: now,
     95    parent_message_id: "123456",
     96    revision_root_message_id: "1234",
     97    ordinal: 0,
     98    is_active_branch: true,
     99    role: 0,
    100    model_id: "a model id",
    101    params: '{ "some": "data" }',
    102    usage: '{ "some": "usage data" }',
    103    content: '{ "some": "content data" }',
    104    conv_id: "123456789012",
    105    page_url: "https://www.firefox.com",
    106    turn_index: 0,
    107    insights_enabled: true,
    108    insights_flag_source: 1,
    109    insights_applied: '{ "some": "insights" }',
    110    web_search_queries: '{ "some": "web search queries" }',
    111  });
    112 
    113  const rows = parseMessageRows([testRow]);
    114  const message = rows[0];
    115 
    116  Assert.withSoftAssertions(function (soft) {
    117    // eslint-disable-next-line mozilla/use-isInstance
    118    soft.ok(message instanceof ChatMessage);
    119    soft.equal(message.id, "123456789012");
    120    soft.equal(message.createdDate, now);
    121    soft.equal(message.parentMessageId, "123456");
    122    soft.equal(message.revisionRootMessageId, "1234");
    123    soft.equal(message.ordinal, 0);
    124    soft.equal(message.isActiveBranch, true);
    125    soft.equal(message.role, 0);
    126    soft.equal(message.modelId, "a model id");
    127    soft.deepEqual(message.params, { some: "data" });
    128    soft.deepEqual(message.usage, { some: "usage data" });
    129    soft.deepEqual(message.content, { some: "content data" });
    130    soft.deepEqual(message.convId, "123456789012");
    131    soft.ok(URL.isInstance(message.pageUrl));
    132    soft.deepEqual(message.pageUrl.href, "https://www.firefox.com/");
    133    soft.equal(message.turnIndex, 0);
    134    soft.equal(message.insightsEnabled, true);
    135    soft.equal(message.insightsFlagSource, 1);
    136    soft.deepEqual(message.insightsApplied, { some: "insights" });
    137    soft.deepEqual(message.webSearchQueries, { some: "web search queries" });
    138  });
    139 });
    140 
    141 add_task(function test_missingField_parseConversationRow() {
    142  const now = Date.now();
    143  const testRow = new RowStub({
    144    //message_id: "123456789012",
    145    created_date: now,
    146    parent_message_id: "123456",
    147    revision_root_message_id: "1234",
    148    ordinal: 0,
    149    is_active_branch: true,
    150    role: 0,
    151    model_id: "a model id",
    152    params: '{ "some": "data" }',
    153    usage: '{ "some": "usage data" }',
    154    content: '{ "some": "content data" }',
    155    conv_id: "123456789012",
    156    page_url: "https://www.firefox.com",
    157    turn_index: 0,
    158    insights_enabled: true,
    159    insights_flag_source: 1,
    160    insights_applied: '{ "some": "insights" }',
    161    web_search_queries: '{ "some": "web search queries" }',
    162  });
    163 
    164  Assert.throws(function () {
    165    parseMessageRows([testRow]);
    166  }, /NS_ERROR_NOT_AVAILABLE/);
    167 });
    168 
    169 add_task(function test_parseJSONOrNull() {
    170  const json = '{"some": "data"}';
    171 
    172  const parsed = parseJSONOrNull(json);
    173 
    174  Assert.deepEqual(parsed, { some: "data" });
    175 });
    176 
    177 add_task(function test_invalidJson_parseJSONOrNull() {
    178  const json = '{some: "data"}';
    179 
    180  const parsed = parseJSONOrNull(json);
    181 
    182  Assert.equal(parsed, null);
    183 });
    184 
    185 add_task(function test_user_getRoleLabel() {
    186  const role = getRoleLabel(0);
    187 
    188  Assert.equal(role, "User");
    189 });
    190 
    191 add_task(function test_assistant_getRoleLabel() {
    192  const role = getRoleLabel(1);
    193 
    194  Assert.equal(role, "Assistant");
    195 });
    196 
    197 add_task(function test_system_getRoleLabel() {
    198  const role = getRoleLabel(2);
    199 
    200  Assert.equal(role, "System");
    201 });
    202 
    203 add_task(function test_user_getRoleLabel() {
    204  const role = getRoleLabel(3);
    205 
    206  Assert.equal(role, "Tool");
    207 });
    208 
    209 add_task(function test_parseChatHistoryViewRows() {
    210  const row1 = new RowStub({
    211    conv_id: "1",
    212    title: "conv 1",
    213    created_date: 116952982,
    214    updated_date: 116952982,
    215    urls: "https://www.firefox.com,https://www.mozilla.com",
    216  });
    217 
    218  const row2 = new RowStub({
    219    conv_id: "2",
    220    title: "conv 2",
    221    created_date: 117189198,
    222    updated_date: 117189198,
    223    urls: "https://www.mozilla.org",
    224  });
    225 
    226  const row3 = new RowStub({
    227    conv_id: "3",
    228    title: "conv 3",
    229    created_date: 168298919,
    230    updated_date: 168298919,
    231    urls: "https://www.firefox.com",
    232  });
    233 
    234  const rows = [row1, row2, row3];
    235 
    236  const viewRows = parseChatHistoryViewRows(rows);
    237 
    238  Assert.withSoftAssertions(function (soft) {
    239    soft.equal(viewRows[0].convId, "1");
    240    soft.equal(viewRows[0].title, "conv 1");
    241    soft.equal(viewRows[0].createdDate, 116952982);
    242    soft.equal(viewRows[0].updatedDate, 116952982);
    243    soft.deepEqual(viewRows[0].urls, [
    244      new URL("https://www.firefox.com"),
    245      new URL("https://www.mozilla.com"),
    246    ]);
    247 
    248    soft.equal(viewRows[1].convId, "2");
    249    soft.equal(viewRows[1].title, "conv 2");
    250    soft.equal(viewRows[1].createdDate, 117189198);
    251    soft.equal(viewRows[1].updatedDate, 117189198);
    252    soft.deepEqual(viewRows[1].urls, [new URL("https://www.mozilla.org")]);
    253 
    254    soft.equal(viewRows[2].convId, "3");
    255    soft.equal(viewRows[2].title, "conv 3");
    256    soft.equal(viewRows[2].createdDate, 168298919);
    257    soft.equal(viewRows[2].updatedDate, 168298919);
    258    soft.deepEqual(viewRows[2].urls, [new URL("https://www.firefox.com")]);
    259  });
    260 });