import { test , expect } from '@playwright/test' ;
import { importUser } from './testUtils' ;
test ( 'Add contact, record gift, confirm gift' , async ( { page } ) = > {
// Generate a random string of 16 characters
let randomString = Math . random ( ) . toString ( 36 ) . substring ( 2 , 18 ) ;
// In case the string is shorter than 16 characters, generate more characters until it is 16 characters long
while ( randomString . length < 16 ) {
randomString += Math . random ( ) . toString ( 36 ) . substring ( 2 , 18 ) ;
}
const finalRandomString = randomString . substring ( 0 , 16 ) ;
// Generate a random non-zero single-digit number
const randomNonZeroNumber = Math . floor ( Math . random ( ) * 99 ) + 1 ;
// Standard title prefix
const standardTitle = 'Gift ' ;
// Combine title prefix with the random string
const finalTitle = standardTitle + finalRandomString ;
// Contact name
const contactName = 'Contact 00' ;
// Import user 01
await importUser ( page , '01' ) ;
// Add new contact 00
await page . goto ( './contacts' ) ;
await page . getByPlaceholder ( 'URL or DID, Name, Public Key' ) . fill ( 'did:ethr:0x0000694B58C2cC69658993A90D3840C560f2F51F' ) ;
await page . locator ( 'button > svg.fa-plus' ) . click ( ) ;
await expect ( page . locator ( 'div[role="alert"]' ) ) . toBeVisible ( ) ;
// Why doesn't the alert box come up every time?
// await page.locator('div[role="alert"] button:has-text("Yes")').click();
// Verify added contact
await expect ( page . locator ( 'li.border-b' ) ) . toContainText ( 'did:ethr:0x0000694B58C2cC69658993A90D3840C560f2F51F' ) ;
// Rename contact
await page . locator ( 'li.border-b h2 > button[title="Edit"]' ) . click ( ) ;
await expect ( page . locator ( 'div.dialog-overlay > div.dialog' ) . filter ( { hasText : 'Edit Name' } ) ) . toBeVisible ( ) ;
await page . getByPlaceholder ( 'Name' , { exact : true } ) . fill ( contactName ) ;
await page . locator ( '.dialog > .flex > button' ) . first ( ) . click ( ) ;
// Confirm that home shows contact in "Record Something…"
await page . goto ( './' ) ;
await expect ( page . locator ( '#sectionRecordSomethingGiven ul li' ) . filter ( { hasText : contactName } ) . nth ( 0 ) ) . toBeVisible ( ) ;
// Record something given by new contact
await page . getByRole ( 'heading' , { name : contactName } ) . click ( ) ;
await page . getByPlaceholder ( 'What was given' ) . fill ( finalTitle ) ;
await page . getByRole ( 'spinbutton' ) . fill ( randomNonZeroNumber . toString ( ) ) ;
await page . getByRole ( 'button' , { name : 'Sign & Send' } ) . click ( ) ;
await expect ( page . getByText ( 'That gift was recorded.' ) ) . toBeVisible ( ) ;
// Refresh home view and check gift
await page . goto ( './' ) ;
await page . locator ( 'li' ) . filter ( { hasText : finalTitle } ) . locator ( 'a' ) . click ( ) ;
await expect ( page . getByRole ( 'heading' , { name : 'Verifiable Claim Details' } ) ) . toBeVisible ( ) ;
await expect ( page . getByText ( finalTitle , { exact : true } ) ) . toBeVisible ( ) ;
// Switch to user 00
await page . goto ( './account' ) ;
await page . getByRole ( 'heading' , { name : 'Advanced' } ) . click ( ) ;
await page . getByRole ( 'link' , { name : 'Switch Identifier' } ) . click ( ) ;
await page . getByRole ( 'link' , { name : 'Add Another Identity…' } ) . click ( ) ;
await page . getByText ( 'You have a seed' ) . click ( ) ;
await page . getByPlaceholder ( 'Seed Phrase' ) . fill ( 'rigid shrug mobile smart veteran half all pond toilet brave review universe ship congress found yard skate elite apology jar uniform subway slender luggage' ) ;
await page . getByRole ( 'button' , { name : 'Import' } ) . click ( ) ;
// Go to home view and look for gift
await page . goto ( './' ) ;
await page . locator ( 'li' ) . filter ( { hasText : finalTitle } ) . locator ( 'a' ) . click ( ) ;
// Confirm gift as user 00
await page . getByRole ( 'button' , { name : 'Confirm' } ) . click ( ) ;
await page . getByRole ( 'button' , { name : 'Yes' } ) . click ( ) ;
await expect ( page . getByText ( 'Confirmation submitted.' ) ) . toBeVisible ( ) ;
// Refresh claim page, Confirm button should be hidden
await page . reload ( ) ;
await expect ( page . getByRole ( 'button' , { name : 'Confirm' } ) ) . toBeHidden ( ) ;
} ) ;