/**
 * @fileoverview Tests a _project.yaml file.
 *
 * @author Pete LePage <petele@google.com>
 */
'use strict';

const testHelpers = require('./helpers');

const JSONValidator = require('jsonschema').Validator;

JSONValidator.prototype.customFormats.wfUAString = function(input) {
  return input === 'UA-52746336-1';
};
JSONValidator.prototype.customFormats.doesFileExist = function(input) {
  return testHelpers.doesFileExist(input);
};
const SCHEMA_PROJECT = {
  id: '/Project',
  type: 'object',
  properties: {
    is_family_root: {type: 'boolean'},
    parent_project_metadata_path: {
      type: 'string',
      pattern: /^\/web\/_project.yaml$/,
    },
    name: {type: 'string', required: true},
    description: {type: 'string', required: true},
    home_url: {type: 'string', pattern: /^\/web\//i, required: true},
    color: {
      type: 'string',
      pattern: /^google-blue|orange$/,
      required: true,
    },
    buganizer_id: {type: 'number', pattern: /^180451$/, required: true},
    content_license: {
      type: 'string',
      pattern: /^cc3-apache2$/,
      required: true,
    },
    footer_path: {type: 'string', required: true},
    icon: {
      type: 'object',
      properties: {
        path: {type: 'string', required: true},
      },
      additionalProperties: false,
      required: true,
    },
    google_analytics_ids: {
      type: 'array',
      items: {type: 'string', format: 'wfUAString'},
      required: true,
    },
    tags: {type: 'array'},
    announcement: {
      type: 'object',
      properties: {
        description: {type: 'string', required: true},
        background: {type: 'string', required: false},
      },
      additionalProperties: false,
    },
    social_media: {
      type: 'object',
      properties: {
        image: {
          type: 'object',
          properties: {
            path: {type: 'string', format: 'doesFileExist', required: true},
            width: {type: 'number'},
            height: {type: 'number'},
          },
          required: true,
          additionalProperties: false,
        },
      },
      additionalProperties: false,
    },
  },
  additionalProperties: false,
};

/**
 * Tests and validates a _project.yaml file.
 *   Note: The returned promise always resolves, it will never reject.
 *
 * @param {string} filename The name of the file to be tested.
 * @param {Object} project The parsed contents of the project file.
 * @return {Promise} A promise with the result of the test.
 */
function test(filename, project) {
  return new Promise(function(resolve, reject) {
    const results = [];
    let validator = new JSONValidator();
    validator.validate(project, SCHEMA_PROJECT).errors.forEach((err) => {
      let msg = `${err.stack || err.message}`;
      msg = msg.replace('{}', '(' + err.instance + ')');
      const result = {
        level: 'ERROR',
        filename: filename,
        message: msg,
      };
      results.push(result);
    });
    if (results.length > 0) {
      reject(results);
      return;
    }
    resolve(true);
  });
}

exports.test = test;

@@ -513,153 +513,8 @@
 : %7B%0A
-    is_family_root: %7Btype: 'boolean'%7D,%0A    parent_project_metadata_path: %7B%0A      type: 'string',%0A      pattern: /%5E%5C/web%5C/_project.yaml$/,%0A    %7D,%0A
     
@@ -678,111 +678,8 @@
 e%7D,%0A
-    color: %7B%0A      type: 'string',%0A      pattern: /%5Egoogle-blue%7Corange$/,%0A      required: true,%0A    %7D,%0A
     
@@ -861,63 +861,20 @@
     
-footer_path: %7Btype: 'string', required: true%7D,%0A    icon
+announcement
 : %7B%0A
@@ -915,36 +915,43 @@
 ties: %7B%0A        
-path
+description
 : %7Btype: 'string
@@ -980,72 +980,289 @@
     
-%7D,%0A      additionalProperties: false,%0A      required: true,%0A    
+  background: %7Btype: 'string', required: false%7D,%0A      %7D,%0A      additionalProperties: false,%0A    %7D,%0A    color: %7B%0A      type: 'string',%0A      pattern: /%5Egoogle-blue%7Corange$/,%0A      required: true,%0A    %7D,%0A    footer_path: %7Btype: 'string', required: true%7D,%0A    gcs_id: %7Btype: 'string'
 %7D,%0A 
@@ -1399,47 +1399,12 @@
     
-tags: %7Btype: 'array'%7D,%0A    announcement
+icon
 : %7B%0A
@@ -1449,35 +1449,28 @@
 : %7B%0A        
-description
+path
 : %7Btype: 'st
@@ -1503,38 +1503,51 @@
     
-  background: %7Btype: 'string',
+%7D,%0A      additionalProperties: false,%0A     
  req
@@ -1549,34 +1549,30 @@
   required: 
-false%7D,%0A  
+true,%0A
     %7D,%0A     
@@ -1574,37 +1574,140 @@
     
-  additionalProperties: false
+is_family_root: %7Btype: 'boolean'%7D,%0A    parent_project_metadata_path: %7B%0A      type: 'string',%0A      pattern: /%5E%5C/web%5C/_project.yaml$/
 ,%0A  
@@ -2132,16 +2132,43 @@
 %0A    %7D,%0A
+    tags: %7Btype: 'array'%7D,%0A
   %7D,%0A  a

Add support for gcs_id to project.yaml (#5910)