Page MenuHomePhabricator

Investigate MediaWiki test failure (QUnit) - VisualEditor: prefixsearch ordering
Open, Needs TriagePublic

Description

As part of https://gerrit.wikimedia.org/r/c/mediawiki/core/+/947898, I noticed that VisualEditor's unit tests fail in Firefox (both locally on a plain setup with MW core + VisualEditor, as well as in Quibble/CI).

FAILED TESTS:
ve.ui.MWTemplateTitleInputWidget
  ✖ CirrusSearch with prefixsearch fallback
    Firefox 115.0 (Linux x86_64)
  Expected: [
    {
      "index": -10,
      "pageid": 202,
      "title": "C"
    },
    {
      "index": -9,
      "pageid": 201,
      "title": "D"
    },
    {
      "index": 0,
      "pageid": 102,
      "title": "A"
    },
    {
      "index": 1,
      "pageid": 101,
      "title": "B"
    }
  ]
  Actual: [
    {
      "index": 0,
      "pageid": 102,
      "title": "A"
    },
    {
      "index": 1,
      "pageid": 101,
      "title": "B"
    },
    {
      "index": -10,
      "pageid": 202,
      "title": "C"
    },
    {
      "index": -9,
      "pageid": 201,
      "title": "D"
    }
  ]

Event Timeline

newPages = [
    {
        "pageid": 101,
        "title": "B",
        "index": 1
    },
    {
        "pageid": 102,
        "title": "A",
        "index": 0
    },
    {
        "pageid": 103,
        "title": "A/(templatedata-doc-subpage)"
    },
    {
        "pageid": 202,
        "title": "C",
        "index": -10
    },
    {
        "pageid": 201,
        "title": "D",
        "index": -9
    }
];
newPages.sort( ( a, b ) => a.index - b.index );

// Firefox 126.0.1 (64-bit), macOS 13
[
  { "pageid": 102, "title": "A", "index": 0 },
  { "pageid": 101, "title": "B", "index": 1 },
  { "pageid": 103, "title": "A/(templatedata-doc-subpage)" },
  { "pageid": 202, "title": "C", "index": -10 },
  { "pageid": 201, "title": "D", "index": -9 }
]

// Chrome 125.0.6422.113, macOS 13
[
  { "pageid": 202, "title": "C", "index": -10 },
  { "pageid": 201, "title": "D", "index": -9  },
  { "pageid": 102, "title": "A", "index": 0 },
  { "pageid": 101, "title": "B",  "index": 1 },
  { "pageid": 103, "title": "A/(templatedata-doc-subpage)" }
]

Change #1037565 had a related patch set uploaded (by Krinkle; author: Krinkle):

[mediawiki/extensions/VisualEditor@master] MWTemplateTitleInputWidget: Fix unstable sort affecting Firefox

https://gerrit.wikimedia.org/r/1037565

These tests were written by WMDE as part of their focus on the Template dialog. Not sure what project to tag this with now. CC @thiemowmde @Lina_Farid_WMDE

I discussed this interoperatbility upstream with the TC39 members in their Matrix channel. TC39 is the committe that authors the JavaScript/ECMAScript spec. While there was some interest in specifying this behaviour (so as to require consistency across browsers), it is currently expected that "non-well formed" comparator functions behave differently as it depends a lot on the sort algo which items will be mis-positioned.

Allowing JS engines to evolve their sorting algo is seen as a positive thing, and that makes sense indeed.

It basically boils down to that if you have an undefined and if you simply do a - b (or otherwise produce NaN only in certain combinations but not others), then it does not adhere to the "5 qualities" required for a well-formed comparator. This quirk/caveat is documented on MDN at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#sorting_with_non-well-formed_comparator.

https://tc39.es/
https://matrix.to/#/#tc39-general:matrix.org
https://matrixlogs.bakkot.com/TC39_General/2024-05-30

@Esanders: The original WMDE-Templates-FocusArea tag is archived. We are not watching this any more. This is now part of VisualEditor, or specifically VisualEditor-MediaWiki-Templates.

You can tag technical issues we should have a look at with WMDE-TechWish-Maintenance, or just ping us. Thanks!

Change #1037565 merged by jenkins-bot:

[mediawiki/extensions/VisualEditor@master] MWTemplateTitleInputWidget: Fix unstable sort affecting Firefox

https://gerrit.wikimedia.org/r/1037565