Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cockpit location ts #21276

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

Conversation

jelly
Copy link
Member

@jelly jelly commented Nov 15, 2024

  • remove arguments use
  • Property 'replaceAll' does not exist on type 'string'.
  • Rethink API, singleton?
  • Solve A property name to write to depends on a user provided value
  • solve
# pkg/lib/cockpit/location.ts:168:57 - error TS2322: Type 'string | string[]' is not assignable to type 'string'.
#   Type 'string[]' is not assignable to type 'string'.
#
# 168                                 last = options[name] = [last];
#                                                             ~~~~

@jelly jelly added the no-test For doc/workflow changes, or experiments which don't need a full CI run, label Nov 15, 2024
if (options[name]) {
let last = options[name];
if (!Array.isArray(value))
last = options[name] = [last];

Check failure

Code scanning / CodeQL

Remote property injection High

A property name to write to depends on a
user-provided value
.
pkg/lib/cockpit/location.ts Fixed Show fixed Hide fixed
@jelly
Copy link
Member Author

jelly commented Nov 18, 2024

Locally I have this test failure to cleanup:

Expected: 	"#/other"
Result: 	"#/not-gonna-happen"
    window.setTimeout(function() {
        location.go(["not-gonna-happen"]);
        assert.strictEqual(window.location.hash, "#/other", "hash is correct");

        cockpit.location.go(["gonna-happen"]);
        assert.strictEqual(window.location.hash, "#/gonna-happen", "hash has changed");

        done();
    }, 100);

    /* User or something else navigates */
    window.location.hash = "#/other";

@jelly
Copy link
Member Author

jelly commented Nov 20, 2024

So in to sum this all up, the test code is:

window.location.hash = "#/hello";

const location = cockpit.location;
assert.deepEqual(cockpit.location.path, ["hello"], "path is right");

window.setTimeout(function() {
  location.go(["not-gonna-happen"]);
  assert.strictEqual(window.location.hash, "#/other", "hash is correct");

  cockpit.location.go(["gonna-happen"]);
  assert.strictEqual(window.location.hash, "#/gonna-happen", "hash has changed");
done();
}, 100);

/* User or something else navigates */
window.location.hash = "#/other";

More verbose test output is by adding a console.log to replace and go as both work with last_loc

test - path is right
self -> Location, last_loc => null
test hash is correct
self -> Location, last_loc => Location
test hash has changed

So last_loc is our last cached location. This is initialized when cockpit.location is queried and set to null when the hashchange window event happens OR re-initialised when !last_loc || last_loc.href !== get_window_location_hash().

So this is why our tests fail. That leaves the question to why does this all happen.

pkg/lib/cockpit/location.ts Fixed Show fixed Hide fixed
pkg/lib/cockpit/location.ts Fixed Show fixed Hide fixed
Prepare for moving cockpit.location to a separate typescript file named
location.
Apparently String.prototype.replaceAll is an 2021 feature.
This function also allows `string[]` as can be seen by looking at how
the code is used in our code.
@jelly jelly removed the no-test For doc/workflow changes, or experiments which don't need a full CI run, label Nov 26, 2024
last = options[name] = [last];
last.push(value);
} else {
options[name] = value;

Check failure

Code scanning / CodeQL

Remote property injection High

A property name to write to depends on a
user-provided value
.
Comment on lines +45 to +46
if (window.mock?.url_root)
this.url_root = window.mock.url_root;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 added lines are not executed by any test.

Comment on lines +48 to +51
if (application.indexOf("cockpit+=") === 0) {
if (this.url_root)
this.url_root += '/';
this.url_root = this.url_root + application.replace("cockpit+", '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 4 added lines are not executed by any test.

Comment on lines +70 to +73
} else if (part == "..") {
if (out.length === 0)
return [];
out.pop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 4 added lines are not executed by any test.

Comment on lines +85 to +86
} else if (path instanceof Location) {
return path.href;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 added lines are not executed by any test.


decode(href: string, options: Options) {
if (href[0] == '#')
href = href.substring(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This added line is not executed by any test.

Comment on lines +164 to +168
if (options[name]) {
let last = options[name];
if (!Array.isArray(last))
last = options[name] = [last];
last.push(value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 5 added lines are not executed by any test.

Comment on lines +196 to +197
toString() {
return this.href;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 added lines are not executed by any test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants