Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
-
CI results over the last 22 days
- Manifest: toolkit/components/ai/tests/xpcshell/xpcshell.toml
/* Any copyright is dedicated to the Public Domain.
"use strict";
// These tests only give initial coverage of the AIStorage round trip and
// are mostly tautological. Do not use them as a template: new tests, including
// those written by coding agents, should exercise the actual integration of
// ai: content with its consumers instead.
const { AIStorage } = ChromeUtils.importESModule(
"moz-src:///toolkit/components/ai/AIStorage.sys.mjs"
);
const { NetUtil } = ChromeUtils.importESModule(
"resource://gre/modules/NetUtil.sys.mjs"
);
const AITAB_TYPE = "application/vnd.mozilla.aitab+json";
function load(spec) {
const channel = NetUtil.newChannel({
uri: spec,
loadUsingSystemPrincipal: true,
});
return new Promise(resolve => {
NetUtil.asyncFetch(channel, (stream, status) => {
resolve({
status,
text: Components.isSuccessCode(status)
? NetUtil.readInputStreamToString(stream, stream.available())
: null,
contentType: channel.contentType,
});
});
});
}
add_setup(function () {
do_get_profile();
});
add_task(async function test_put_is_served_by_the_protocol_handler() {
await AIStorage.put("cookie-recipes", "/index.aitab", "v1", AITAB_TYPE);
Assert.equal(result.status, Cr.NS_OK);
Assert.equal(result.text, "v1");
Assert.equal(result.contentType, AITAB_TYPE);
});
add_task(async function test_put_replaces_previous_content() {
await AIStorage.put("cookie-recipes", "/index.aitab", "v2", AITAB_TYPE);
Assert.equal(result.text, "v2");
});
add_task(async function test_origins_are_separate() {
await AIStorage.put("other-page", "/notes.txt", "other", "text/plain");
Assert.ok(
!Components.isSuccessCode(
)
);
});
add_task(async function test_clear_removes_only_its_origin() {
await AIStorage.put("to-clear", "/index.aitab", "gone", AITAB_TYPE);
await AIStorage.clear("to-clear");
Assert.ok(
"The cleared origin has no content left"
);
Assert.equal(
"other",
"Other origins keep their content"
);
});
add_task(async function test_host_must_be_the_origin_host() {
await AIStorage.put("cookie-recipes", "/notes.txt", "kept", "text/plain");
for (const host of [
"x@cookie-recipes",
"cookie-recipes/x",
"Cookie-Recipes",
"cookie-recipes:80",
]) {
await Assert.rejects(
AIStorage.put(host, "/notes.txt", "overwritten", "text/plain"),
/Invalid ai: host/,
`put() rejects ${host}`
);
await Assert.rejects(
AIStorage.clear(host),
/Invalid ai: host/,
`clear() rejects ${host}`
);
}
Assert.equal(
"kept",
"The origin is left untouched"
);
});
add_task(async function test_path_must_stay_in_the_origin() {
for (const path of [
"//other-page/notes.txt",
]) {
await Assert.rejects(
AIStorage.put("cookie-recipes", path, "outside", "text/plain"),
/Path outside of ai:\/\/cookie-recipes/,
`put() rejects ${path}`
);
}
Assert.equal(
"other",
"Other origins keep their content"
);
});