Setting the file. One moment.
Wix Wp Plugin V2 Client Test Fixture · Rp Source Wordpress · wix/skills · Skills Docs
ContentsBack to the top of the page (opens in a new tab)
lib/ wix-wp-plugin-v2-client.test-fixture.js
JavaScript · 149 lines · 7 KB
,
options
) {
10 calls. push ({ method: 'GET' , url, options });
11 return {
12 table: options.params.table,
13 columns: [
14 { name: 'id' , type: 'bigint(20) unsigned' , nullable: false , key: 'primary' },
15 { name: 'number' , type: 'varchar(64)' , nullable: false , key: 'unique' },
16 { name: 'active' , type: 'tinyint(1)' , nullable: false , key: null },
17 ],
18 };
19 },
20 async post ( url , options ) {
21 calls. push ({ method: 'POST' , url, options });
22 return {
23 rows: [{ id: '1' , number: 'CARD0001' , active: '1' }],
24 pagingMetadata: { cursors: { next: null }, hasNext: false , total: 1 },
25 redactionMetadata: { redactedValueCount: 0 , redactedColumns: [] },
26 };
27 },
28 };
29
30 const discovered = await discoverStructure ({ httpClient, siteBaseUrl: 'https://example.test' , table: 'pimwick_gift_card' });
31 if (discovered.table !== 'pimwick_gift_card' || discovered.columns. length !== 3 ) {
32 throw new Error ( 'fixture discovery did not return the expected table/columns shape' );
33 }
34 const getCall = calls. find (( call ) => call.method === 'GET' );
35 if ( ! getCall || getCall.options.params.table !== 'pimwick_gift_card' ) {
36 throw new Error ( 'fixture discovery did not request the expected table param' );
37 }
38 if ( ! getCall.url. endsWith ( '/wp-json/wix-wp-plugin/v2/structure' )) {
39 throw new Error ( 'fixture discovery did not hit the expected wix-wp-plugin-v2 route' );
40 }
41
42 const queried = await queryStructure ({
43 httpClient,
44 siteBaseUrl: 'https://example.test' ,
45 structureRequest: {
46 table: 'pimwick_gift_card' ,
47 select: [{ column: 'id' }, { column: 'number' }, { column: 'active' }],
48 orderBy: [{ column: 'id' , direction: 'asc' }],
49 },
50 limit: 10 ,
51 });
52 if ( ! Array. isArray (queried.rows) || queried.rows. length !== 1 || queried.rows[ 0 ].number !== 'CARD0001' ) {
53 throw new Error ( 'fixture query did not return the expected row shape' );
54 }
55 if (queried.pagingMetadata.hasNext !== false || queried.pagingMetadata.total !== 1 ) {
56 throw new Error ( 'fixture query did not return the expected pagingMetadata shape' );
57 }
58 const postCall = calls. find (( call ) => call.method === 'POST' );
59 if ( ! postCall || ! postCall.url. endsWith ( '/wp-json/wix-wp-plugin/v2/query/admin-key' )) {
60 throw new Error ( 'fixture query did not hit the expected wix-wp-plugin-v2 route' );
61 }
62 const postedBody = JSON . parse (postCall.options.body);
63 if (postedBody.structureRequest.table !== 'pimwick_gift_card' || postedBody.limit !== 10 ) {
64 throw new Error ( 'fixture query did not send the expected request body' );
65 }
66
67 // --- the safety metadata must survive the client, and its absence must fail ---
68
69 if ( ! queried.redactionMetadata || queried.redactionMetadata.redactedValueCount !== 0 ) {
70 throw new Error ( 'fixture query dropped redactionMetadata, which every response carries' );
71 }
72 if (queried.eavAccess !== null ) {
73 throw new Error ( 'a plain table must report eavAccess as null, not as a fabricated object' );
74 }
75
76 // A plugin too old to send redactionMetadata has no value guard at all. Accepting that
77 // response would report a guarded read that never happened, so the client must refuse it.
78 const legacyClient = {
79 async post () {
80 return { rows: [{ id: '1' }], pagingMetadata: { cursors: { next: null }, hasNext: false , total: 1 } };
81 },
82 };
83 let refusedLegacy = false ;
84 try {
85 await queryStructure ({
86 httpClient: legacyClient,
87 siteBaseUrl: 'https://example.test' ,
88 structureRequest: { table: 'pimwick_gift_card' , select: [{ column: 'id' }], orderBy: [{ column: 'id' , direction: 'asc' }] },
89 });
90 } catch (error) {
91 refusedLegacy = /redactionMetadata/ . test (error.message);
92 }
93 if ( ! refusedLegacy) {
94 throw new Error ( 'a response with no redactionMetadata must be refused, not defaulted to zero' );
95 }
96
97 // A key/value table: both routes must hand the caller enough to build a legal request,
98 // INCLUDING the second guarded value column on a multi-pair schema.
99 const eavClient = {
100 async get ( url , options ) {
101 return {
102 table: options.params.table,
103 columns: [
104 { name: 'meta_id' , type: 'bigint(20) unsigned' , nullable: false , key: 'primary' },
105 { name: 'meta_key' , type: 'varchar(255)' , nullable: true , key: 'index' },
106 { name: 'meta_value' , type: 'longtext' , nullable: true , key: null },
107 { name: 'name' , type: 'varchar(64)' , nullable: true , key: null },
108 { name: 'value' , type: 'longtext' , nullable: true , key: null },
109 ],
110 eavPair: {
111 keyColumn: 'meta_key' ,
112 valueColumn: 'meta_value' ,
113 additionalPairs: [{ keyColumn: 'name' , valueColumn: 'value' }],
114 },
115 };
116 },
117 async post () {
118 return {
119 rows: [{ meta_key: 'last4' , n: '12' }],
120 pagingMetadata: { cursors: { next: null }, hasNext: false , total: 1 },
121 redactionMetadata: { redactedValueCount: 1 , redactedColumns: [ 'meta_value' ] },
122 eavAccess: { keyColumn: 'meta_key' , valueColumn: 'meta_value' , tier: 'key-discovery' , pinnedKeys: [] },
123 };
124 },
125 };
126 const eavDiscovered = await discoverStructure ({ httpClient: eavClient, siteBaseUrl: 'https://example.test' , table: 'dual_meta' });
127 if ( ! eavDiscovered.eavPair || eavDiscovered.eavPair.keyColumn !== 'meta_key' ) {
128 throw new Error ( 'discoverStructure dropped eavPair -- the caller loses its only advance notice that a table is key-scoped' );
129 }
130 if ( guardedValueColumns (eavDiscovered.eavPair). join ( ',' ) !== 'meta_value,value' ) {
131 throw new Error ( 'guardedValueColumns must report EVERY guarded value column, not only the first pair \' s' );
132 }
133 const eavQueried = await queryStructure ({
134 httpClient: eavClient,
135 siteBaseUrl: 'https://example.test' ,
136 structureRequest: { table: 'dual_meta' , select: [{ column: 'meta_key' }], orderBy: [{ column: 'meta_key' , direction: 'asc' }] },
137 });
138 if ( ! eavQueried.eavAccess || eavQueried.eavAccess.tier !== 'key-discovery' ) {
139 throw new Error ( 'queryStructure dropped eavAccess -- the caller cannot tell the read was key-scoped' );
140 }
141 if (eavQueried.redactionMetadata.redactedValueCount !== 1
142 || eavQueried.redactionMetadata.redactedColumns[ 0 ] !== 'meta_value' ) {
143 throw new Error ( 'queryStructure dropped the redaction count -- a redacted value would look like real data' );
144 }
145
146 return true ;
147 }
148
149 module . exports = selfTest;