~james/core

core/e2e/specs/strings.spec.ts -rw-r--r-- 5.0 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { expect } from "@playwright/test";

import { getOk } from "../harness/api";
import { config } from "../harness/config";
import {
	bobbin,
	type Ctx,
	flow,
	hydrant,
	link,
	not,
	pds,
	record,
	replacing,
	rkeyOf,
	starId,
	target,
	text,
	testid,
	through,
	where,
	within
} from "../harness/flow";
import { get, until, xrpc } from "../harness/net";
import { acceptConfirm, pdsRecordUrl, recall, remember } from "../harness/shared";

const string = record("org.tangled.snippet.string");
const stringFilename = "notes.txt";
const stringBody = "the needle is in the haystack";
const newString = where("the new string form", () => "/strings/new");
const stringPage = where(
	"the string page",
	(ctx) => `/strings/${recall(ctx, "writer").handle}/${rkeyOf(ctx, string)}`
);
const stringsTab = where(
	"the writer's strings tab",
	(ctx) => `/${recall(ctx, "writer").handle}?tab=strings`
);
// the editor is a web component, its contenteditable is in a shadow root
const stringEditor = target("the string editor", (tab) =>
	tab.locator("diffs-container").getByRole("textbox")
);
const typeInEditor =
	(words: string, replace = false) =>
	async (ctx: Ctx) => {
		const editor = stringEditor.locate(ctx.page, ctx);
		const typed = ctx.page.locator(
			`[data-testid="contentBytes"][data-bytes="${Buffer.byteLength(words)}"]`
		);
		// the editor drops keys when it redraws after the filename changes
		await until("the editor should hold what was typed", 30_000, async () => {
			await editor.click();
			await ctx.page.waitForTimeout(500);
			await ctx.page.keyboard.press("ControlOrMeta+a");
			if (!replace) await ctx.page.keyboard.press("Backspace");
			await editor.pressSequentially(words, { delay: 20 });
			const held = await typed.isVisible({ timeout: 1500 }).catch(() => false);
			return [held, held ? "it does" : "it does not"];
		});
	};

const starredStringCard = target("the starred string's card", (tab, ctx) =>
	tab.locator(
		`[data-component="StringCardContent"]:has(a[href="/strings/${recall(ctx, "writer").handle}/${rkeyOf(ctx, string)}"])`
	)
);

const stringOnPds = async (ctx: Ctx) => {
	const { value } = await getOk(
		pdsRecordUrl(recall(ctx, "writer").did, string.nsid, rkeyOf(ctx, string))
	);
	const blob = await get(
		xrpc(config.pdsUrl, "com.atproto.sync.getBlob", {
			did: recall(ctx, "writer").did,
			cid: value.snippet.content.ref.$link
		})
	);
	return { value, contents: blob.body as string };
};

const writtenString = (name: string) =>
	flow(name)
		.actor("writer")
		.step("remember the writer", remember("writer"))
		.open(newString)
		.fill(testid("filename"), stringFilename)
		.fill(testid("description"), "my notes")
		.step("types the content", typeInEditor(stringBody))
		.click(testid("submit"))
		.sends(
			string,
			pds,
			{
				record: {
					$type: string.nsid,
					snippet: { name: stringFilename },
					description: "my notes"
				}
			},
			"put"
		)
		.trace(string, through(pds, hydrant, bobbin))
		.at(stringPage)
		.sees(text(stringBody));

writtenString("creating a string")
	.step("the pds keeps the text in a blob", async (ctx) => {
		const { value, contents } = await stringOnPds(ctx);
		expect(value.snippet.name).toBe(stringFilename);
		expect(contents).toBe(stringBody);
	})
	.reload()
	.sees(text(stringBody))
	.sees(within(testid("stringDescription"), text("my notes")))
	.open(stringsTab)
	.sees(link(stringFilename));

const edited = "the needle moved to another haystack";

writtenString("editing a string")
	.click(testid("edit"))
	.fill(testid("description"), "my edited notes")
	.step("types the new content", typeInEditor(edited, true))
	.click(testid("submit"))
	.sends(
		string,
		pds,
		replacing(string, {
			snippet: { name: stringFilename },
			description: "my edited notes"
		}),
		"put"
	)
	.trace(string, through(pds, hydrant, bobbin))
	.sees(text(edited))
	.sees(within(testid("stringDescription"), text("my edited notes")))
	.step("the pds keeps the new text", async (ctx) => {
		expect((await stringOnPds(ctx)).contents).toBe(edited);
	})
	.reload()
	.sees(text(edited))
	.sees(not(text(stringBody)));

writtenString("deleting a string")
	.step("agree to the confirm", acceptConfirm)
	.click(testid("moreMenu"))
	.click(testid("delete"))
	.sends(string, pds, replacing(string), "delete")
	.trace(string, through(pds, hydrant, bobbin))
	.at(stringsTab)
	.sees(not(link(stringFilename)))
	.open(stringPage)
	.sees(testid("errorTitle", { status: "404" }));

writtenString("starring a string")
	.actor("fan")
	.open(stringPage)
	.click(testid("star", { starred: "false" }))
	.sends(
		starId,
		pds,
		(ctx) => ({
			record: {
				subject: {
					$type: "org.tangled.feed.star#record",
					record: { uri: ctx.get(string).uri, cid: ctx.get(string).cid }
				}
			}
		}),
		"create"
	)
	.sees(testid("star", { starred: "true" }))
	.trace(starId, through(pds, hydrant, bobbin))
	.reload()
	.sees(testid("star", { starred: "true" }))
	.open(where("the fan's starred tab", (ctx) => `/${ctx.actor.handle}?tab=starred`))
	.sees(starredStringCard)
	.sees(within(starredStringCard, link(stringFilename)))
	.sees(within(starredStringCard, text("my notes")));