import { LitElement, css, html } from 'lit'
import { property } from 'lit/decorators/property.js';
import { customElement } from 'lit/decorators/custom-element.js';
import { ActorUrlTestRun, Assertion } from 'activitypub-testing/test-actor'
import { repeat } from 'lit/directives/repeat.js';
/**
* an element that tests an ActivityPub actor
* using activitypub-testing/test-actor.
*
* @element activitypub-actor-tester
*/
@customElement('activitypub-actor-tester')
export class ActivityPubActorTester extends LitElement {
/** URL of the Actor that should be tested. */
@property({
type: String,
reflect: true,
converter(value, type) {
return new URL(value ?? '', globalThis?.location?.toString())
},
})
protected actor: string;
/**
* whether the test should be run automatically.
* If false, showing test results requires clicking a button.
*/
@property({ type: Boolean, reflect: true })
protected test: boolean = false;
@property({ type: Array })
/** Test Assertions from the last test run */
assertions: Array> = []
@property({ attribute: false })
/** Error encountered during the last actor test run */
error: Error | undefined
constructor() {
super()
this.actor = this.#getSearchParamsFromHash().get('actor') || globalThis.location.toString()
}
connectedCallback() {
super.connectedCallback()
if (this.test) {
this.testActor()
}
}
#getSearchParamsFromHash(url=new URL(globalThis.document.location.toString())) {
const searchParams = new URLSearchParams(url.hash.replace(/^#/, ''))
return searchParams
}
#handleFormChange(event: Event) {
let form = event.target
// @ts-expect-error form EventTarget
if (event.target?.tagName === 'INPUT') {
// @ts-expect-error
form = event.target.form
}
// @ts-expect-error form EventTarget
const formData = new FormData(form)
const actor = formData.get('actor')?.valueOf()
if (actor) {
this.actor = String(actor)
}
}
#handleFormSubmit(event: Event) {
event.preventDefault()
const form = event.target
// @ts-expect-error form EventTarget
const formData = new FormData(form)
const actor = formData.get('actor')?.valueOf()
if (actor) {
this.actor = String(actor)
this.testActor()
}
}
render() {
const applicableAssertions = [];
const inapplicableAssertions = []
for (const assertion of this.assertions) {
if (assertion.result.outcome === "inapplicable") {
inapplicableAssertions.push(assertion)
} else {
applicableAssertions.push(assertion)
}
}
return html`