Skip to content

Commit

Permalink
Merge pull request #16 from st-tech/test
Browse files Browse the repository at this point in the history
test: more tests
  • Loading branch information
ssssota authored Dec 10, 2024
2 parents a145069 + 39af9f7 commit 8fd7d75
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/sim/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const createVerify: CreateVerify = (json) => {
if (!details || details.length === 0) return "unset";

for (const detail of details) {
if (!detail.appID && (!detail.appIDs || detail.appIDs.length === 0))
continue;

const components = detail.components;
if (!components) continue;
for (const component of components) {
Expand Down
125 changes: 100 additions & 25 deletions tests/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,110 @@
import * as assert from "node:assert";
import { test } from "node:test";
import { type AppleAppSiteAssociation, verify } from "universal-links-test";
import {
type AppleAppSiteAssociation,
type ApplinksDetailsComponents,
verify,
} from "universal-links-test";
import { verify as verifySim } from "universal-links-test/sim";

const json = {
applinks: {
details: [
{
appIDs: ["HOGE.com.example.app"],
components: [
{ "#": "nondeeplinking", exclude: true },
{ "/": "/search/" },
],
},
],
},
} as const satisfies AppleAppSiteAssociation;
const getJson = (
components: ApplinksDetailsComponents[],
): AppleAppSiteAssociation => ({
applinks: { details: [{ appID: "HOGE.com.example.app", components }] },
});

test("/search/ should match", async () => {
assert.strictEqual(await verify(json, "/search/"), "match");
assert.strictEqual(await verifySim(json, "/search/"), "match");
test("empty json", async () => {
const json = {};
const path = "/";
assert.strictEqual(await verify(json, path), "unset");
assert.strictEqual(await verifySim(json, path), "unset");
});

test("#nondeeplinking should block", async () => {
assert.strictEqual(await verify(json, "/search/#nondeeplinking"), "block");
assert.strictEqual(await verifySim(json, "/search/#nondeeplinking"), "block");
test("invalid json", async () => {
const json = "{[]}";
const path = "/";
await assert.rejects(() => verify(json, path));
await assert.rejects(() => verifySim(json, path));
});

test("empty json", async () => {
assert.strictEqual(await verify({}, "/"), "unset");
assert.strictEqual(await verifySim({}, "/"), "unset");
assert.strictEqual(await verify("{}", "/"), "unset");
assert.strictEqual(await verifySim("{}", "/"), "unset");
test("empty rule", async () => {
const json = getJson([]);
const path = "/";
assert.strictEqual(await verify(json, path), "unset");
assert.strictEqual(await verifySim(json, path), "unset");
});

test("should match path", async () => {
const json = getJson([{ "/": "/search/" }]);
const path = "/search/";
assert.strictEqual(await verify(json, path), "match");
assert.strictEqual(await verifySim(json, path), "match");
});

test("should exclude", async () => {
const json = getJson([
{ "#": "nondeeplinking", exclude: true },
{ "/": "/search/" },
]);
const path = "/search/#nondeeplinking";
assert.strictEqual(await verify(json, path), "block");
assert.strictEqual(await verifySim(json, path), "block");
});

test("default case sensitive", async () => {
const json = getJson([{ "/": "/search/" }]);
const path = "/SEARCH/";
assert.strictEqual(await verify(json, path), "unset");
assert.strictEqual(await verifySim(json, path), "unset");
});

test("case insensitive", async () => {
const json = getJson([{ "/": "/search/", caseSensitive: false }]);
const path = "/SEARCH/";
assert.strictEqual(await verify(json, path), "match");
assert.strictEqual(await verifySim(json, path), "match");
});

test("should match path with query string and hash", async () => {
const json = getJson([{ "/": "/search/" }]);
const path = "/search/?q=foo#bar";
assert.strictEqual(await verify(json, path), "match");
assert.strictEqual(await verifySim(json, path), "match");
});

test("should match query string", async () => {
const json = getJson([{ "?": "key=value" }]);
const path = "/?key=value";
assert.strictEqual(await verify(json, path), "match");
assert.strictEqual(await verifySim(json, path), "match");
});

test("should not match query string with extras", async () => {
const json = getJson([{ "?": "key=value" }]);
const path = "/?key=value&extra=value";
assert.strictEqual(await verify(json, path), "unset");
assert.strictEqual(await verifySim(json, path), "unset");
});

test("should match query string with key-value object", async () => {
const json = getJson([{ "?": { key: "value" } }]);
const path = "/?key=value";
assert.strictEqual(await verify(json, path), "match");
assert.strictEqual(await verifySim(json, path), "match");
});

test("should match query string with key-value object with extras", async () => {
const json = getJson([{ "?": { key: "value" } }]);
const path = "/?key=value&extra=value";
assert.strictEqual(await verify(json, path), "match");
assert.strictEqual(await verifySim(json, path), "match");
});

test("should unset appIDs and appID not provided", async () => {
const json = {
applinks: { details: [{ components: [{ "/": "/search/" }] }] },
};
const path = "/search/";
assert.strictEqual(await verify(json, path), "unset");
assert.strictEqual(await verifySim(json, path), "unset");
});

0 comments on commit 8fd7d75

Please sign in to comment.