import * as BackHandler from "@effect/ui-native/kit/BackHandler" import { Link } from "@effect/ui-native/kit/Link" import { Pressable } from "@effect/ui-native/kit/Pressable" import { Stack } from "@effect/ui-native/kit/Stack" import * as Native from "@effect/ui-native/Native" import * as NativeRouter from "@effect/ui-native/NativeRouter" import * as Context from "effect/Context" import * as Effect from "effect/Effect" import * as Layer from "effect/Layer" import * as Option from "effect/Option" import * as Schema from "effect/Schema" import * as Stream from "effect/Stream" import * as Head from "effect/ui/Head" import * as Renderer from "effect/ui/Renderer" import * as Route from "effect/ui/Route" import * as Router from "effect/ui/Router" import * as RouterStack from "effect/ui/RouterStack" import * as UiSchema from "effect/ui/UiSchema" import { assert, describe, it } from "vitest" import * as FakeDevice from "./utils/FakeDevice.ts" import * as FakeFabric from "./utils/FakeFabric.ts" const AppRoute = Route.union({ Home: Route.make("/"), Person: Route.make("/people/:id", { params: { id: Schema.Number } }) }) const HomeMessage = UiSchema.TaggedEnum({ Counted: {}, Backed: {} }) // Takes Back while its count is odd. const Home = Native.component({ name: "Home", model: Schema.Struct({ count: Schema.Number, backs: Schema.Number }), message: HomeMessage }).make({ init: () => ({ count: 0, backs: 0 }), update: { Counted: ($) => $.evolve({ count: (count) => count + 1 }), Backed: ($) => $.evolve({ backs: (backs) => backs + 1 }) }, subscriptions: ({ model }) => [ model.count % 2 === 1 && BackHandler.onPress(HomeMessage.Backed()) ], head: () => [Head.title("Home")], view: (h, model) => h.view.children( Pressable(h, { onPress: HomeMessage.Counted() }).children( h.text(`count ${model.count}`) ), h.text(`home backs ${model.backs}`), Link(h, { href: AppRoute.print(AppRoute.Person({ params: { id: 1 } })) }) .children(h.text("Ada")), Link(h, { href: "https://effect.website" }).children(h.text("Docs")) ) }) const PersonMessage = UiSchema.TaggedEnum({ Toggled: {}, Dirtied: {}, Backed: {}, Asked: {} }) const Person = Native.component({ name: "Person", props: Schema.Struct({ id: Schema.Number }), model: Schema.Struct({ taking: Schema.Boolean, dirty: Schema.Boolean, backs: Schema.Number, asks: Schema.Number }), message: PersonMessage }).make({ init: () => ({ taking: false, dirty: false, backs: 0, asks: 0 }), update: { Toggled: ($) => $.evolve({ taking: (taking) => !taking }), Dirtied: ($) => $.evolve({ dirty: (dirty) => !dirty }), Backed: ($) => $.evolve({ backs: (backs) => backs + 1 }), Asked: ($) => $.evolve({ asks: (asks) => asks + 1 }) }, subscriptions: ({ model }) => [ model.taking && BackHandler.onPress(PersonMessage.Backed()), model.dirty && Router.blocked("form", () => PersonMessage.Asked()) ], head: ($) => [Head.title(`Person ${$.props.id}`)], view: (h, model) => h.view.children( h.text(`person ${h.props.id} backs ${model.backs}`), h.text(`asks ${model.asks}`), Pressable(h, { onPress: PersonMessage.Toggled() }).children( h.text("take back") ), Pressable(h, { onPress: PersonMessage.Dirtied() }).children( h.text("dirty") ) ) }) const AppMessage = UiSchema.TaggedEnum({ Navigated: { navigation: RouterStack.Navigation(AppRoute) } }) // Home on a stack of its own, inside the outer stack's first screen. const Nest = Native.component({ name: "Nest", model: Schema.Null, message: Schema.Never }).make({ init: () => null, update: ($) => $.model, view: (h) => Stack(h, { entries: [{ id: 0, route: null }], screen: (s) => Home(s, { key: "home" }) }) }) const makeApp = (nested: boolean) => Native.component({ name: "App", props: Schema.Struct({ stack: RouterStack.Stack(AppRoute) }), model: RouterStack.Stack(AppRoute), message: AppMessage }).make({ init: ($) => $.props.stack, update: { Navigated: ($, { navigation }) => $.set(RouterStack.follow(AppRoute, $.model, navigation)) }, subscriptions: ({ model }) => [ RouterStack.subscription( AppRoute, model, (navigation) => AppMessage.Navigated({ navigation }) ) ], view: (h, stack) => Stack(h, { entries: stack, screen: (s, { route }) => AppRoute.match(route, { Home: () => nested ? Nest(s, { key: "nest" }) : Home(s, { key: "home" }), Person: ({ params }) => Person(s, { key: "person", props: { id: params.id } }) }) }) }) const App = makeApp(false) const NestedApp = makeApp(true) const start = async (launched: string | null = null, nested = false) => { let router: Router.Router | undefined const back = FakeDevice.module() const linking = FakeDevice.module({ getInitialURL: () => Promise.resolve(launched), openURL: () => Promise.resolve() }) const a11y = FakeDevice.module() const fake = FakeDevice.make("android", { DeviceEventManager: back.module, IntentAndroid: linking.module, AccessibilityInfo: a11y.module }) const mounted = await FakeFabric.mount((container) => Effect.gen(function*() { const context = yield* Layer.build( NativeRouter.layer({ routes: AppRoute, prefixes: ["myapp://"] }) ) router = Context.get(context, Router.Router) return yield* Effect.flatMap( RouterStack.props(AppRoute), (props) => Renderer.mount(nested ? NestedApp : App, props, container) ).pipe(Effect.provide(context)) }).pipe(Effect.provideService(Native.Device, fake.device)) ) const { fabric, settle } = mounted const shown = (text: string) => FakeFabric.find(fabric.committed(), FakeFabric.byText(text)) !== undefined const press = async (text: string) => { FakeFabric.touch(fabric, mounted.find(FakeFabric.byText(text))) await settle() } const back_ = async () => { fake.emit("hardwareBackPress") await settle() } const screens = () => { const out: Array = [] const walk = (nodes: ReadonlyArray) => { for (const n of nodes) { if (n.props.position === "absolute") out.push(n) walk(n.children) } } walk(fabric.committed()) return out } const calls = (module: ReturnType, name: string) => module.calls.filter(([method]) => method === name).map(([, ...args]) => args) return { ...mounted, fake, run: async (effect: Effect.Effect) => { Effect.runSync(effect) await settle() }, router: router!, shown, press, back: back_, screens, exits: () => calls(back, "invokeDefaultBackPressHandler").length, opened: () => calls(linking, "openURL").map(([url]) => url), announced: () => calls(a11y, "announceForAccessibility").map(([text]) => text), focused: () => fabric.accessibilityEvents.filter(([, type]) => type === "focus").map(( [tag] ) => tag) } } // A runtime recognizes one touch at a time, so the tests take turns. describe("NativeRouter", { concurrent: false }, () => { it("pushes on a Link, keeps the covered screen mounted, and goes back with Back until the first screen", async () => { const app = await start() await app.press("count 0") await app.press("count 1") await app.press("Ada") assert.isTrue(app.shown("person 1 backs 0")) const [home, person] = app.screens() assert.strictEqual(app.screens().length, 2) assert.include(home!.props, { opacity: 0, pointerEvents: "none", importantForAccessibility: "no-hide-descendants", accessibilityElementsHidden: true }) assert.include(person!.props, { opacity: 1, pointerEvents: "auto" }) assert.deepStrictEqual(app.focused(), [person!.tag]) await app.back() assert.isFalse(app.shown("person 1 backs 0")) assert.isTrue(app.shown("count 2")) assert.strictEqual(app.screens().length, 1) assert.include(app.screens()[0]!.props, { opacity: 1 }) assert.deepStrictEqual(app.focused(), [person!.tag, home!.tag]) assert.strictEqual(app.exits(), 0) await app.back() assert.strictEqual(app.exits(), 1) await app.close() assert.strictEqual(app.fake.listening("hardwareBackPress"), 0) }) it.each([false, true])( "gives Back to the top screen's handler before a covered screen's, even one started later (nested: %s)", async (nested) => { const app = await start(null, nested) await app.press("Ada") await app.press("take back") // The covered Home starts its handler after Person's. await app.press("count 0") await app.back() assert.isTrue(app.shown("person 1 backs 1")) assert.isTrue(app.shown("home backs 0")) await app.press("take back") await app.back() assert.isTrue(app.shown("home backs 0")) assert.isFalse(app.shown("person 1 backs 1")) await app.back() assert.isTrue(app.shown("home backs 1")) assert.strictEqual(app.exits(), 0) await app.close() } ) it("goes back to the screen a replace covered, though its route is the same", async () => { const app = await start() await app.press("count 0") await app.press("count 1") await app.press("Ada") await app.run(app.router.replace(AppRoute.Home())) assert.isTrue(app.shown("count 0")) assert.strictEqual(app.screens().length, 2) await app.back() assert.strictEqual(app.screens().length, 1) assert.isTrue(app.shown("count 2")) await app.back() assert.strictEqual(app.exits(), 1) await app.close() }) it("adds a screen for a #hash link, which Back removes", async () => { const app = await start() await app.press("Ada") await app.run(app.router.navigate("/people/1#bio")) assert.strictEqual(app.screens().length, 3) await app.back() assert.strictEqual(app.screens().length, 2) await app.back() assert.strictEqual(app.screens().length, 1) assert.strictEqual(app.exits(), 0) await app.back() assert.strictEqual(app.exits(), 1) await app.close() }) it("presses and follows links activated without a pointer", async () => { const app = await start() const link = app.find((n) => n.props.accessibilityRole === "link") // Android sends a click only to a focusable view. assert.strictEqual(link.props.focusable, true) // A touch's click has a pointer, and the touch has already pressed. app.fabric.emit(link, "topClick", { pointerId: 1, pointerType: "touch" }) await app.settle() assert.strictEqual(app.router.location().index, 0) // TalkBack activates the Pressable, around the text. const counter = app.find((n) => n.children.some((c) => c.children.some(FakeFabric.byText("count 0")))) app.fabric.emit(counter, "topClick", {}) await app.settle() assert.isTrue(app.shown("count 1")) app.fabric.emit(link, "topClick", {}) await app.settle() assert.strictEqual(app.router.location().index, 1) assert.isTrue(app.shown("person 1 backs 0")) await app.close() }) it("moves the screen reader's focus to a screen that replaces the first", async () => { const app = await start() await app.run(app.router.replace(AppRoute.Person({ params: { id: 1 } }))) const [person] = app.screens() assert.isTrue(app.shown("person 1 backs 0")) assert.deepStrictEqual(app.focused(), [person!.tag]) await app.close() }) it("reports Back to the block while one is held, and stays", async () => { const app = await start() await app.press("Ada") await app.press("dirty") await app.back() assert.isTrue(app.shown("person 1 backs 0")) assert.isTrue(app.shown("asks 1")) assert.strictEqual(app.exits(), 0) await app.press("dirty") await app.back() assert.isFalse(app.shown("person 1 backs 0")) await app.close() }) it("closes the app when Back on the first screen is retried after a block", async () => { const app = await start("myapp://people/7") // A fractional index from a direct call moves nowhere. await app.run(app.router.retry({ _tag: "Go", index: -0.5 })) assert.strictEqual(app.exits(), 0) await app.press("dirty") await app.back() assert.strictEqual(app.exits(), 0) await app.press("dirty") await app.run(app.router.retry({ _tag: "Go", index: -1 })) assert.strictEqual(app.exits(), 1) await app.close() }) it("starts at the deep link that launched the app, pushes later ones, and ignores the rest", async () => { const app = await start("myapp://people/7") assert.isTrue(app.shown("person 7 backs 0")) assert.strictEqual(app.screens().length, 1) app.fake.emit("url", { url: "myapp://people/8" }) await app.settle() assert.isTrue(app.shown("person 8 backs 0")) assert.strictEqual(app.screens().length, 2) // An unparsed one isn't opened with Linking, which would open the app again. app.fake.emit("url", { url: "myapp://missing" }) await app.settle() assert.strictEqual(app.screens().length, 2) assert.deepStrictEqual(app.opened(), []) await app.back() assert.isTrue(app.shown("person 7 backs 0")) await app.back() assert.strictEqual(app.exits(), 1) await app.close() }) const launchedAt = ( routes: Router.Routes, url: string | null, prefixes: NativeRouter.Options["prefixes"] ) => { const linking = FakeDevice.module({ getInitialURL: () => Promise.resolve(url) }) const { device } = FakeDevice.make("android", { IntentAndroid: linking.module }) return Effect.runPromise( Effect.map(Router.Router, (router) => router.location().url).pipe( Effect.provide(NativeRouter.layer({ routes, prefixes })), Effect.provideService(Native.Device, device) ) ) } it("starts at the launching deep link, else the catch-all or the routes' root", async () => { const catchAll = Route.union({ Home: Route.make("/"), NotFound: Route.make("/*") }) const hashed = Route.union({ Home: Route.make("/"), About: Route.make("/about") }) hashed.setBase("#") const toHash = (url: string) => url.startsWith("myapp://") ? "#/" + url.slice(8) : undefined const started: Array = [] for ( const [routes, url, prefixes] of [ [AppRoute, "myapp://missing", ["myapp://"]], [catchAll, "myapp://missing", ["myapp://"]], [hashed, "myapp://about", toHash], [hashed, "myapp://missing", toHash], [hashed, null, toHash] ] as const ) started.push(await launchedAt(routes, url, prefixes)) assert.deepStrictEqual(started, ["/", "/missing", "#/about", "#/", "#/"]) }) it("opens an href the routes don't parse with Linking", async () => { const app = await start() await app.press("Docs") await new Promise((r) => setTimeout(r, 0)) assert.deepStrictEqual(app.opened(), ["https://effect.website"]) assert.strictEqual(app.screens().length, 1) await app.close() }) it("settles with the top screen's title, and announces nothing", async () => { const app = await start() // Replay includes the first screen, which has already settled. const titles = Effect.runPromise( app.router.settled.pipe( Stream.map(({ title }) => title), Stream.take(3), Stream.runCollect ) ) await app.press("Ada") await app.settle() await app.back() await app.settle() assert.deepStrictEqual(await titles, ["Home", "Person 1", "Home"]) assert.deepStrictEqual(app.announced(), []) await app.close() }) }) describe("NativeRouter.pathOf", () => { it("routes a URL that starts with a prefix, with the rest as its path", () => { const prefixes = ["myapp://", "https://example.com"] assert.deepStrictEqual( NativeRouter.pathOf(prefixes, "myapp://people/1?tab=a"), Option.some("/people/1?tab=a") ) assert.deepStrictEqual( NativeRouter.pathOf(prefixes, "https://example.com/people/1"), Option.some("/people/1") ) assert.deepStrictEqual( NativeRouter.pathOf(prefixes, "https://example.com"), Option.some("/") ) assert.deepStrictEqual( NativeRouter.pathOf(prefixes, "https://example.community/x"), Option.none() ) assert.deepStrictEqual( NativeRouter.pathOf((url) => url.slice(3), "abc/x"), Option.some("/x") ) }) })