@@ -19,6 +19,8 @@ const path = require('path');
1919const test = require ( 'ava' ) ;
2020const fs = require ( 'fs' ) ;
2121const tools = require ( '@google-cloud/nodejs-repo-tools' ) ;
22+ const PNG = require ( 'pngjs' ) . PNG ;
23+ const pixelmatch = require ( 'pixelmatch' ) ;
2224
2325const cmd = 'node redact.js' ;
2426const cwd = path . join ( __dirname , `..` ) ;
@@ -28,6 +30,33 @@ const testResourcePath = 'system-test/resources';
2830
2931test . before ( tools . checkCredentials ) ;
3032
33+ function readImage ( filePath ) {
34+ return new Promise ( ( resolve , reject ) => {
35+ fs . createReadStream ( filePath )
36+ . pipe ( new PNG ( ) )
37+ . on ( 'error' , reject )
38+ . on ( 'parsed' , function ( ) {
39+ resolve ( this ) ;
40+ } ) ;
41+ } ) ;
42+ }
43+
44+ async function getImageDiffPercentage ( image1Path , image2Path ) {
45+ let image1 = await readImage ( image1Path ) ;
46+ let image2 = await readImage ( image2Path ) ;
47+ let diff = new PNG ( { width : image1 . width , height : image1 . height } ) ;
48+
49+ const diffPixels = pixelmatch (
50+ image1 . data ,
51+ image2 . data ,
52+ diff . data ,
53+ image1 . width ,
54+ image1 . height
55+ ) ;
56+ return diffPixels / ( diff . width * diff . height ) ;
57+ }
58+
59+ // redact_text
3160test ( `should redact a single sensitive data type from a string` , async t => {
3261 const output = await tools . runAsync (
3362 `${ cmd } string "My email is jenny@example.com" -t EMAIL_ADDRESS` ,
@@ -56,33 +85,33 @@ test(`should handle string with no sensitive data`, async t => {
5685test ( `should redact a single sensitive data type from an image` , async t => {
5786 const testName = `redact-single-type` ;
5887 const output = await tools . runAsync (
59- `${ cmd } image ${ testImage } ${ testName } .result .png -t PHONE_NUMBER` ,
88+ `${ cmd } image ${ testImage } ${ testName } .actual .png -t PHONE_NUMBER` ,
6089 cwd
6190 ) ;
6291
6392 t . regex ( output , / S a v e d i m a g e r e d a c t i o n r e s u l t s t o p a t h / ) ;
6493
65- const correct = fs . readFileSync (
66- `${ testResourcePath } /${ testName } .correct.png`
94+ const difference = await getImageDiffPercentage (
95+ `${ testName } .actual.png` ,
96+ `${ testResourcePath } /${ testName } .expected.png`
6797 ) ;
68- const result = fs . readFileSync ( `${ testName } .result.png` ) ;
69- t . deepEqual ( correct , result ) ;
98+ t . true ( difference < 0.03 ) ;
7099} ) ;
71100
72101test ( `should redact multiple sensitive data types from an image` , async t => {
73102 const testName = `redact-multiple-types` ;
74103 const output = await tools . runAsync (
75- `${ cmd } image ${ testImage } ${ testName } .result .png -t PHONE_NUMBER EMAIL_ADDRESS` ,
104+ `${ cmd } image ${ testImage } ${ testName } .actual .png -t PHONE_NUMBER EMAIL_ADDRESS` ,
76105 cwd
77106 ) ;
78107
79108 t . regex ( output , / S a v e d i m a g e r e d a c t i o n r e s u l t s t o p a t h / ) ;
80109
81- const correct = fs . readFileSync (
82- `${ testResourcePath } /${ testName } .correct.png`
110+ const difference = await getImageDiffPercentage (
111+ `${ testName } .actual.png` ,
112+ `${ testResourcePath } /${ testName } .expected.png`
83113 ) ;
84- const result = fs . readFileSync ( `${ testName } .result.png` ) ;
85- t . deepEqual ( correct , result ) ;
114+ t . true ( difference < 0.03 ) ;
86115} ) ;
87116
88117test ( `should report info type errors` , async t => {
@@ -95,7 +124,7 @@ test(`should report info type errors`, async t => {
95124
96125test ( `should report image redaction handling errors` , async t => {
97126 const output = await tools . runAsync (
98- `${ cmd } image ${ testImage } nonexistent.result .png -t BAD_TYPE` ,
127+ `${ cmd } image ${ testImage } output .png -t BAD_TYPE` ,
99128 cwd
100129 ) ;
101130 t . regex ( output , / E r r o r i n r e d a c t I m a g e / ) ;
0 commit comments