From 39af9f7f8b1d958ff80b90e0896d40f651361766 Mon Sep 17 00:00:00 2001 From: TOMIKAWA Sotaro Date: Tue, 10 Dec 2024 17:46:34 +0900 Subject: [PATCH] test: more tests --- src/sim/verify.ts | 3 ++ tests/node.test.ts | 125 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 103 insertions(+), 25 deletions(-) diff --git a/src/sim/verify.ts b/src/sim/verify.ts index 6b7d55c..41efa00 100644 --- a/src/sim/verify.ts +++ b/src/sim/verify.ts @@ -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) { diff --git a/tests/node.test.ts b/tests/node.test.ts index 1308271..4f5dcd9 100644 --- a/tests/node.test.ts +++ b/tests/node.test.ts @@ -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"); });