tor-browser

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

test_quicksuggest_flight_status.js (6075B)


      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 // Tests flight status suggestions.
      6 
      7 "use strict";
      8 
      9 const TEST_MERINO_SINGLE = [
     10  {
     11    provider: "flightaware",
     12    is_sponsored: false,
     13    score: 0,
     14    title: "Flight Suggestion",
     15    custom_details: {
     16      flightaware: {
     17        values: [
     18          {
     19            flight_number: "flight",
     20            origin: {
     21              city: "Origin",
     22              code: "O",
     23            },
     24            destination: {
     25              city: "Destination",
     26              code: "D",
     27            },
     28            departure_scheduled_time: "2025-09-17T14:05:00Z",
     29            arrival_scheduled_time: "2025-09-17T18:30:00Z",
     30            status: "Scheduled",
     31            url: "https://example.com/A1",
     32          },
     33        ],
     34      },
     35    },
     36  },
     37 ];
     38 
     39 add_setup(async function init() {
     40  // Disable search suggestions so we don't hit the network.
     41  await Services.search.init();
     42  Services.prefs.setBoolPref("browser.search.suggest.enabled", false);
     43 
     44  await QuickSuggestTestUtils.ensureQuickSuggestInit({
     45    merinoSuggestions: TEST_MERINO_SINGLE,
     46    prefs: [
     47      ["flightStatus.featureGate", true],
     48      ["suggest.flightStatus", true],
     49      ["suggest.quicksuggest.all", true],
     50    ],
     51  });
     52 });
     53 
     54 add_task(async function telemetry_type() {
     55  Assert.equal(
     56    QuickSuggest.getFeature(
     57      "FlightStatusSuggestions"
     58    ).getSuggestionTelemetryType({}),
     59    "flights",
     60    "Telemetry type should be 'flights'"
     61  );
     62 });
     63 
     64 add_task(async function disabledPrefs() {
     65  let prefs = [
     66    "quicksuggest.enabled",
     67    "suggest.flightStatus",
     68    "suggest.quicksuggest.all",
     69  ];
     70 
     71  for (let pref of prefs) {
     72    info("Testing pref: " + pref);
     73 
     74    // First make sure the suggestion is added.
     75    await check_results({
     76      context: createContext("test", {
     77        providers: [UrlbarProviderQuickSuggest.name],
     78        isPrivate: false,
     79      }),
     80      matches: [merinoResult()],
     81    });
     82 
     83    // Now disable them.
     84    UrlbarPrefs.set(pref, false);
     85    await check_results({
     86      context: createContext("test", {
     87        providers: [UrlbarProviderQuickSuggest.name],
     88        isPrivate: false,
     89      }),
     90      matches: [],
     91    });
     92 
     93    // Revert.
     94    UrlbarPrefs.set(pref, true);
     95    await QuickSuggestTestUtils.forceSync();
     96  }
     97 });
     98 
     99 add_task(async function not_interested_on_realtime() {
    100  await doDismissAllTest({
    101    result: merinoResult(),
    102    command: "not_interested",
    103    feature: QuickSuggest.getFeature("FlightStatusSuggestions"),
    104    pref: "suggest.flightStatus",
    105    queries: [{ query: "a1" }],
    106  });
    107 });
    108 
    109 add_task(async function show_less_frequently() {
    110  UrlbarPrefs.clear("flightStatus.showLessFrequentlyCount");
    111  UrlbarPrefs.clear("flightStatus.minKeywordLength");
    112 
    113  let cleanUpNimbus = await UrlbarTestUtils.initNimbusFeature({
    114    realtimeMinKeywordLength: 0,
    115    realtimeShowLessFrequentlyCap: 3,
    116  });
    117 
    118  let result = merinoResult();
    119 
    120  const testData = [
    121    {
    122      input: "fli",
    123      before: {
    124        canShowLessFrequently: true,
    125        showLessFrequentlyCount: 0,
    126        minKeywordLength: 0,
    127      },
    128      after: {
    129        canShowLessFrequently: true,
    130        showLessFrequentlyCount: 1,
    131        minKeywordLength: 4,
    132      },
    133    },
    134    {
    135      input: "fligh",
    136      before: {
    137        canShowLessFrequently: true,
    138        showLessFrequentlyCount: 1,
    139        minKeywordLength: 4,
    140      },
    141      after: {
    142        canShowLessFrequently: true,
    143        showLessFrequentlyCount: 2,
    144        minKeywordLength: 6,
    145      },
    146    },
    147    {
    148      input: "flight",
    149      before: {
    150        canShowLessFrequently: true,
    151        showLessFrequentlyCount: 2,
    152        minKeywordLength: 6,
    153      },
    154      after: {
    155        canShowLessFrequently: false,
    156        showLessFrequentlyCount: 3,
    157        minKeywordLength: 7,
    158      },
    159    },
    160  ];
    161 
    162  for (let { input, before, after } of testData) {
    163    let feature = QuickSuggest.getFeature("FlightStatusSuggestions");
    164 
    165    await check_results({
    166      context: createContext(input, {
    167        providers: [UrlbarProviderQuickSuggest.name],
    168        isPrivate: false,
    169      }),
    170      matches: [result],
    171    });
    172 
    173    Assert.equal(
    174      UrlbarPrefs.get("flightStatus.minKeywordLength"),
    175      before.minKeywordLength
    176    );
    177    Assert.equal(feature.canShowLessFrequently, before.canShowLessFrequently);
    178    Assert.equal(
    179      feature.showLessFrequentlyCount,
    180      before.showLessFrequentlyCount
    181    );
    182 
    183    triggerCommand({
    184      result,
    185      feature,
    186      command: "show_less_frequently",
    187      searchString: input,
    188    });
    189 
    190    Assert.equal(
    191      UrlbarPrefs.get("flightStatus.minKeywordLength"),
    192      after.minKeywordLength
    193    );
    194    Assert.equal(feature.canShowLessFrequently, after.canShowLessFrequently);
    195    Assert.equal(
    196      feature.showLessFrequentlyCount,
    197      after.showLessFrequentlyCount
    198    );
    199 
    200    await check_results({
    201      context: createContext(input, {
    202        providers: [UrlbarProviderQuickSuggest.name],
    203        isPrivate: false,
    204      }),
    205      matches: [],
    206    });
    207  }
    208 
    209  await cleanUpNimbus();
    210  UrlbarPrefs.clear("flightStatus.showLessFrequentlyCount");
    211  UrlbarPrefs.clear("flightStatus.minKeywordLength");
    212 });
    213 
    214 function merinoResult() {
    215  return {
    216    type: UrlbarUtils.RESULT_TYPE.DYNAMIC,
    217    source: UrlbarUtils.RESULT_SOURCE.SEARCH,
    218    isBestMatch: true,
    219    hideRowLabel: true,
    220    heuristic: false,
    221    payload: {
    222      source: "merino",
    223      provider: "flightaware",
    224      dynamicType: "realtime-flightStatus",
    225      telemetryType: "flights",
    226      isSponsored: false,
    227      items: [
    228        {
    229          flight_number: "flight",
    230          origin: {
    231            city: "Origin",
    232            code: "O",
    233          },
    234          destination: { city: "Destination", code: "D" },
    235          departure_scheduled_time: "2025-09-17T14:05:00Z",
    236          arrival_scheduled_time: "2025-09-17T18:30:00Z",
    237          status: "Scheduled",
    238          url: "https://example.com/A1",
    239        },
    240      ],
    241    },
    242  };
    243 }