2023-01-07 22:33:50 +01:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2023-01-07 22:33:50 +01:00
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
const {zrequire} = require("./lib/namespace.js");
|
|
|
|
const {run_test} = require("./lib/test.js");
|
2023-01-07 22:33:50 +01:00
|
|
|
|
2023-01-10 08:25:35 +01:00
|
|
|
const {$t} = zrequire("i18n");
|
2024-01-13 21:01:07 +01:00
|
|
|
const pygments_data = zrequire("pygments_data");
|
2023-01-07 22:33:50 +01:00
|
|
|
const realm_playground = zrequire("realm_playground");
|
2023-07-11 17:49:07 +02:00
|
|
|
const typeahead_helper = zrequire("typeahead_helper");
|
2023-01-07 22:33:50 +01:00
|
|
|
|
|
|
|
run_test("get_pygments_typeahead_list_for_composebox", () => {
|
|
|
|
// When no Code Playground is configured, the list of candidates should
|
|
|
|
// include everything in pygments_data, but nothing more. Order doesn't
|
|
|
|
// matter.
|
|
|
|
let candidates = realm_playground.get_pygments_typeahead_list_for_composebox();
|
|
|
|
assert.ok(Object.keys(pygments_data.langs).every((value) => candidates.includes(value)));
|
|
|
|
assert.equal(candidates.length, Object.keys(pygments_data.langs).length);
|
|
|
|
|
|
|
|
const custom_pygment_language = "Custom_lang";
|
|
|
|
const playground_data = [
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
name: "Custom Lang",
|
|
|
|
pygments_language: custom_pygment_language,
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template: "https://example.com/?q={code}",
|
2023-01-07 22:33:50 +01:00
|
|
|
},
|
|
|
|
];
|
2023-07-11 17:49:07 +02:00
|
|
|
realm_playground.initialize({
|
|
|
|
playground_data,
|
|
|
|
pygments_comparator_func: typeahead_helper.compare_language,
|
|
|
|
});
|
2023-01-07 22:33:50 +01:00
|
|
|
|
|
|
|
// Verify that Code Playground's pygments_language shows up in the result.
|
|
|
|
// As well as all of pygments_data. Order doesn't matter and the result
|
|
|
|
// shouldn't include anything else.
|
|
|
|
candidates = realm_playground.get_pygments_typeahead_list_for_composebox();
|
|
|
|
assert.ok(Object.keys(pygments_data.langs).every((value) => candidates.includes(value)));
|
|
|
|
assert.ok(candidates.includes(custom_pygment_language));
|
|
|
|
assert.equal(
|
|
|
|
candidates.length,
|
|
|
|
Object.keys(pygments_data.langs).length + playground_data.length,
|
|
|
|
);
|
|
|
|
});
|
2023-01-10 08:25:35 +01:00
|
|
|
|
|
|
|
run_test("get_pygments_typeahead_list_for_settings", () => {
|
|
|
|
const custom_pygment_language = "custom_lang";
|
|
|
|
const playground_data = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: "Custom Lang #1",
|
|
|
|
pygments_language: custom_pygment_language,
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template: "https://example.com/?q={code}",
|
2023-01-10 08:25:35 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
name: "Custom Lang #2",
|
|
|
|
pygments_language: custom_pygment_language,
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template: "https://example.com/?q={code}",
|
2023-01-10 08:25:35 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 3,
|
|
|
|
name: "Invent a Language",
|
|
|
|
pygments_language: "invent_a_lang",
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template: "https://example.com/?q={code}",
|
2023-01-10 08:25:35 +01:00
|
|
|
},
|
|
|
|
];
|
2023-07-11 17:49:07 +02:00
|
|
|
realm_playground.initialize({
|
|
|
|
playground_data,
|
|
|
|
pygments_comparator_func: typeahead_helper.compare_language,
|
|
|
|
});
|
2023-01-10 08:25:35 +01:00
|
|
|
|
|
|
|
let candidates = realm_playground.get_pygments_typeahead_list_for_settings("");
|
|
|
|
let iterator = candidates.entries();
|
|
|
|
assert.equal(iterator.next().value[1], $t({defaultMessage: "Custom language: custom_lang"}));
|
|
|
|
assert.equal(iterator.next().value[1], $t({defaultMessage: "Custom language: invent_a_lang"}));
|
|
|
|
assert.equal(iterator.next().value[1], "JavaScript (javascript, js, javascript, js)");
|
|
|
|
assert.equal(
|
|
|
|
iterator.next().value[1],
|
2023-12-02 01:54:31 +01:00
|
|
|
"Python (python, bazel, py, py3, python3, sage, starlark, python, bazel, py, py3, python3, sage, starlark)",
|
2023-01-10 08:25:35 +01:00
|
|
|
);
|
|
|
|
assert.equal(iterator.next().value[1], "Java (java, java)");
|
|
|
|
assert.equal(iterator.next().value[1], "Go (go, golang, go, golang)");
|
|
|
|
assert.equal(iterator.next().value[1], "Rust (rust, rs, rust, rs)");
|
|
|
|
|
|
|
|
// Test typing "cu". Previously added custom languages should show up too.
|
|
|
|
candidates = realm_playground.get_pygments_typeahead_list_for_settings("cu");
|
|
|
|
iterator = candidates.entries();
|
|
|
|
assert.equal(
|
|
|
|
iterator.next().value[1],
|
|
|
|
$t({defaultMessage: "Custom language: {query}"}, {query: "cu"}),
|
|
|
|
);
|
|
|
|
assert.equal(iterator.next().value[1], $t({defaultMessage: "Custom language: custom_lang"}));
|
|
|
|
assert.equal(iterator.next().value[1], $t({defaultMessage: "Custom language: invent_a_lang"}));
|
|
|
|
assert.equal(iterator.next().value[1], "JavaScript (javascript, js, javascript, js)");
|
|
|
|
assert.equal(
|
|
|
|
iterator.next().value[1],
|
2023-12-02 01:54:31 +01:00
|
|
|
"Python (python, bazel, py, py3, python3, sage, starlark, python, bazel, py, py3, python3, sage, starlark)",
|
2023-01-10 08:25:35 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Test typing "invent_a_lang". Make sure there is no duplicate entries.
|
|
|
|
candidates = realm_playground.get_pygments_typeahead_list_for_settings("invent_a_lang");
|
|
|
|
iterator = candidates.entries();
|
|
|
|
assert.equal(iterator.next().value[1], $t({defaultMessage: "Custom language: invent_a_lang"}));
|
|
|
|
assert.equal(iterator.next().value[1], $t({defaultMessage: "Custom language: custom_lang"}));
|
|
|
|
assert.equal(iterator.next().value[1], "JavaScript (javascript, js, javascript, js)");
|
|
|
|
assert.equal(
|
|
|
|
iterator.next().value[1],
|
2023-12-02 01:54:31 +01:00
|
|
|
"Python (python, bazel, py, py3, python3, sage, starlark, python, bazel, py, py3, python3, sage, starlark)",
|
2023-01-10 08:25:35 +01:00
|
|
|
);
|
|
|
|
});
|