@@ -19,6 +19,8 @@ const path = require('path');
19
19
const test = require ( 'ava' ) ;
20
20
const fs = require ( 'fs' ) ;
21
21
const tools = require ( '@google-cloud/nodejs-repo-tools' ) ;
22
+ const PNG = require ( 'pngjs' ) . PNG ;
23
+ const pixelmatch = require ( 'pixelmatch' ) ;
22
24
23
25
const cmd = 'node redact.js' ;
24
26
const cwd = path . join ( __dirname , `..` ) ;
@@ -28,6 +30,33 @@ const testResourcePath = 'system-test/resources';
28
30
29
31
test . before ( tools . checkCredentials ) ;
30
32
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
31
60
test ( `should redact a single sensitive data type from a string` , async t => {
32
61
const output = await tools . runAsync (
33
62
`${ cmd } string "My email is [email protected] " -t EMAIL_ADDRESS` ,
@@ -56,33 +85,33 @@ test(`should handle string with no sensitive data`, async t => {
56
85
test ( `should redact a single sensitive data type from an image` , async t => {
57
86
const testName = `redact-single-type` ;
58
87
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` ,
60
89
cwd
61
90
) ;
62
91
63
92
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 / ) ;
64
93
65
- const correct = fs . readFileSync (
66
- `${ testResourcePath } /${ testName } .correct.png`
94
+ const difference = await getImageDiffPercentage (
95
+ `${ testName } .actual.png` ,
96
+ `${ testResourcePath } /${ testName } .expected.png`
67
97
) ;
68
- const result = fs . readFileSync ( `${ testName } .result.png` ) ;
69
- t . deepEqual ( correct , result ) ;
98
+ t . true ( difference < 0.03 ) ;
70
99
} ) ;
71
100
72
101
test ( `should redact multiple sensitive data types from an image` , async t => {
73
102
const testName = `redact-multiple-types` ;
74
103
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` ,
76
105
cwd
77
106
) ;
78
107
79
108
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 / ) ;
80
109
81
- const correct = fs . readFileSync (
82
- `${ testResourcePath } /${ testName } .correct.png`
110
+ const difference = await getImageDiffPercentage (
111
+ `${ testName } .actual.png` ,
112
+ `${ testResourcePath } /${ testName } .expected.png`
83
113
) ;
84
- const result = fs . readFileSync ( `${ testName } .result.png` ) ;
85
- t . deepEqual ( correct , result ) ;
114
+ t . true ( difference < 0.03 ) ;
86
115
} ) ;
87
116
88
117
test ( `should report info type errors` , async t => {
@@ -95,7 +124,7 @@ test(`should report info type errors`, async t => {
95
124
96
125
test ( `should report image redaction handling errors` , async t => {
97
126
const output = await tools . runAsync (
98
- `${ cmd } image ${ testImage } nonexistent.result .png -t BAD_TYPE` ,
127
+ `${ cmd } image ${ testImage } output .png -t BAD_TYPE` ,
99
128
cwd
100
129
) ;
101
130
t . regex ( output , / E r r o r i n r e d a c t I m a g e / ) ;
0 commit comments