11import { test , expect } from "@playwright/test" ;
2- import {
3- setupJSErrorCollection ,
4- expectNoErrors ,
5- expectDefaultValue ,
6- } from "./utils" ;
2+ import { setupJSErrorCollection , expectNoErrors } from "./utils" ;
3+ import { checkEditorsForDefault } from "./check-default-editor-value" ;
74
85test . describe ( "CBOR Encoder Tool" , ( ) => {
96 test . beforeEach ( async ( { page } ) => {
@@ -13,67 +10,58 @@ test.describe("CBOR Encoder Tool", () => {
1310
1411 test ( "should load without errors and show default JSON" , async ( { page } ) => {
1512 await expect ( page . locator ( "main" ) ) . toBeVisible ( ) ;
13+ await checkEditorsForDefault (
14+ page ,
15+ "json-input" ,
16+ "cbor-output" ,
17+ "CBOR example" ,
18+ output => output . trim ( ) . length > 0
19+ ) ;
1620 await expectNoErrors ( page ) ;
17- await expectDefaultValue ( page ) ;
1821 } ) ;
1922
20- test ( "should encode JSON to non-empty hex output" , async ( { page } ) => {
21- const output = page . getByTestId ( "cbor-output" ) ;
22- await expect ( output ) . not . toHaveValue ( "" ) ;
23- } ) ;
24-
25- test ( "should produce different output for hex and base64 tabs" , async ( {
26- page,
27- } ) => {
28- const hexOutput = await page
29- . getByTestId ( "cbor-output" )
30- . inputValue ( )
31- . catch ( ( ) =>
32- page
33- . getByTestId ( "cbor-output" )
34- . evaluate ( el => ( el as HTMLTextAreaElement ) . value )
35- ) ;
36-
37- await page . getByRole ( "button" , { name : "Base64" } ) . click ( ) ;
38-
39- const base64Output = await page
40- . getByTestId ( "cbor-output" )
41- . inputValue ( )
42- . catch ( ( ) =>
43- page
44- . getByTestId ( "cbor-output" )
45- . evaluate ( el => ( el as HTMLTextAreaElement ) . value )
23+ test ( "should show non-empty hex output by default" , async ( { page } ) => {
24+ await expect ( page . locator ( "#cbor-output" ) ) . toBeVisible ( ) ;
25+ const outputValue = await page . evaluate ( ( ) => {
26+ const lines = Array . from (
27+ document . querySelectorAll ( "#cbor-output .cm-content .cm-line" )
4628 ) ;
47-
48- expect ( hexOutput ) . not . toBe ( "" ) ;
49- expect ( base64Output ) . not . toBe ( "" ) ;
50- expect ( hexOutput ) . not . toBe ( base64Output ) ;
29+ return lines . map ( l => l . textContent ?? "" ) . join ( " " ) ;
30+ } ) ;
31+ expect ( outputValue . trim ( ) ) . not . toBe ( "" ) ;
32+ expect ( outputValue ) . toMatch ( / [ 0 - 9 a - f ] { 2 } / ) ;
5133 } ) ;
5234
53- test ( "should round-trip encode then decode back to original JSON " , async ( {
35+ test ( "should switch between hex and base64 tabs with different output " , async ( {
5436 page,
5537 } ) => {
56- const jsonInput = page . getByTestId ( "cbor-json-input" ) ;
57- const originalJson = await jsonInput
58- . inputValue ( )
59- . catch ( ( ) => jsonInput . evaluate ( el => ( el as HTMLTextAreaElement ) . value ) ) ;
38+ await expect ( page . locator ( "#cbor-output" ) ) . toBeVisible ( ) ;
39+ const getOutput = ( ) =>
40+ page . evaluate ( ( ) => {
41+ const lines = Array . from (
42+ document . querySelectorAll ( "#cbor-output .cm-content .cm-line" )
43+ ) ;
44+ return lines . map ( l => l . textContent ?? "" ) . join ( " " ) ;
45+ } ) ;
6046
61- // Encode, then decode back
62- await page . getByRole ( "button" , { name : "Encode" } ) . click ( ) ;
63- await page . getByRole ( "button" , { name : "Decode" } ) . click ( ) ;
47+ const hexOutput = await getOutput ( ) ;
6448
65- const afterRoundTrip = await jsonInput
66- . inputValue ( )
67- . catch ( ( ) => jsonInput . evaluate ( el => ( el as HTMLTextAreaElement ) . value ) ) ;
49+ await page . getByRole ( "button" , { name : "Base64" } ) . click ( ) ;
50+ const base64Output = await getOutput ( ) ;
6851
69- // Values should represent the same JSON structure
70- expect ( JSON . parse ( afterRoundTrip ) ) . toEqual ( JSON . parse ( originalJson ) ) ;
52+ expect ( hexOutput . trim ( ) ) . not . toBe ( "" ) ;
53+ expect ( base64Output . trim ( ) ) . not . toBe ( "" ) ;
54+ expect ( hexOutput ) . not . toBe ( base64Output ) ;
7155 } ) ;
7256
7357 test ( "should show error for invalid JSON input" , async ( { page } ) => {
74- const jsonInput = page . getByTestId ( "cbor-json-input" ) ;
75- await jsonInput . fill ( "{not valid json}" ) ;
58+ const inputEditor = page . locator ( "#json-input .cm-content" ) ;
59+ await inputEditor . click ( ) ;
60+ await page . keyboard . press ( "Control+a" ) ;
61+ await page . keyboard . type ( "{not valid json}" ) ;
62+
7663 await page . getByRole ( "button" , { name : "Encode" } ) . click ( ) ;
7764 await expect ( page . locator ( '[role="alert"]' ) ) . toBeVisible ( ) ;
65+ await expectNoErrors ( page ) ;
7866 } ) ;
7967} ) ;
0 commit comments