Translate

Monday, 17 April 2017

SinonJS - the spy

The example is more of a revealer to an error that can be quite vexing in sinon, node js parlance.

It is called "attempted to wrap undefined property as function" as if it were an attempt to murder.

The case in question is that of a call made to sinon's spy method

var theSpy = sinon.spy('get');
If used as such, this line will throw the error. The trick is to use the container that contains the 'get' method as the first parameter:


Not much of a sinon, but a spy nevertheless, much like Johnny English!


The spy method of sinon helps you to understand how many times a method call happened and it can also help you understand that if at all a call did get made to a method. This can be helpful in calls over the wire, say, http.

You need mocha, sinonjs and nodejs to run the example.

Below is the code.

test.js
var assert = require('assert');
var sinon = require('sinon');

var http = require('http');
var api = require('../myapi.js');
describe('testingBlogPost', function() {
var theSpy = sinon.spy(api,'get'); //  the first parameter will resolve the undefined property method

it('should get result to compare', function() {
var expected = 'Moved Permanently';
api.get(function(err, result) {
....
});
api.get(function(err, result) {
...
});
console.log(theSpy.callCount);
});
});


myapi.js

var http = require('http');
function myApi(){};

myApi.prototype = {
get: function(req,res) {
var req = http.request({
hostname: 'ravichandranjv.blogspot.in',
path: '/2017/04/how-to-setup-middle-game.html'
}, function(response) {
var data = '';
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
res.send(data);
});
});
req.end();
}
}
module.exports = new myApi();


And the result, because the get method was twice called


Of course, another reason for writing the test also helps in knowing what if bots were to call posts on my blog! :)


Happy spying!

Tuesday, 4 April 2017

How to setup the middle game

It was way back in 1985. I was the captain of my college chess team and we were playing the Delhi University's Inter-College Chess Championships.

Our college, Sri Venkateswara College, had been a finalist in the previous year (first time in the history, I recall somebody saying it during the sports meetup) and since the captain had attributed that it was largely due to my efforts, I was offered the captaincy by the college principal and the Sports chief! I can just about recall that I barely managed to accept the captaincy because I was hugely reticent then.

We were playing the winners - Kirori Mal College - and they were formidable.

I had played the different boards in the previous championships so I knew how the order was set and the strategies played.

I knew that there were the smaller fishes that I could always attack, later, as the tournament format, being the swiss rounds, progressed and eyed the larger ones for just a point or a two. So, I decided to play the weakest on the top board, against a later-to-become-Delhi State champion (or possibly the then Delhi state champion), and myself played the 3rd board, hoping to at least fetch a half point for the team.

I don't recall the outcome much but I do recall that I decided to play the 2nd board in the next round against - another formidable team - the St. Stephen's ...! It had another well-known player on the top board and the second board was played by a fair girl, who, too, was a No.2 in the Women's category of the State level!

I decided to chance my luck against her and played the 2nd board!

To put it in perspective, I remembered this game because this post is about how a good opening can lead to a good middle game and a solid end game.

She played the French defense.

I say that with reverence.

Ever since I had eyed her on the floor, tall, fair, slender and very good looking, I had developed a crush on her. And there I was, playing against a ...e6!

In all my preparations, that mostly included games of the Rusian Grandmasters and a Chess Mate subscription that I had won in an Open tournament, the French Defense was the one that I had avoided, not because I did not like it but because Fischer demonstrated some stuff that put me off it forever!

I should have known. A slender soft female would go for the French Defense. It is docile, meek and extremely defensive. Of course, I loathe the opening and not just because I lost the game even before it reached the middle game for me to get my combinations going, but because, at that age, it seemed like I had lost some right to woo her! I still remember that feeling - a squashed out Bertie-Wooster-type-after-meeting-Madeline kind.

Since then I rarely play e4 in games of importance.





So I decided to check out d4 when I started playing Chess again around the early 2000s, with opponents who were four former Delhi State Champions, at an outdoor park, where we gathered each day over many months to play Chess, and which led to my book on Chess!

The only weapon that I had or could muster, against their class and experience, was to play some unconventional Chess and I started to explore Bf4 after Nf3 and not C4, as is the normal.



One of the former champions, the co-author of the book, "Analytical Games of Chess", remarked on why I didn't play the usual Queen's gambit C4, and the reason behind the move. I said that I am very comfortable playing the move. That was not the whole reason.

The real reason is that Bf4 provides the means to attack the black queenside as well as be in a position to switch to a kingside attack, without being obvious.








Another key element, in this strategy, is to sustain the diagonal's importance and make Black pay for moves like Be7 (instead of Bd6) and so, it is equally important to play h3 as soon as Black plays Nf6.







And yet another advantage is that it guides Black to play Ne4 as Nh5 could make no difference as the Bishop could retreat to h2, without causing any double pawns at f4 or g3.

Like so.



Take no notice of the Computer saying it is +0.80. It is just bunkuming itself! A human would not have played g5 before castling, anyway.

The middle game becomes a cake walk.

As I said, take no notice of what the computer thinks of a position.

It says "Checkmate" at the end, too, when it got checkmated - as if it was announcing it to me! :)

Below is another very good game to demonstrate the power of Bf4, in which the white Bishop in h2 makes life miserable for the entire diagonal, all the way to b8 and, to protect the c7 pawn or free the pressure, black has to think of a different strategy to play the Knight via d7!

Check out the difference in the two games - one, played by a computer and the other, by a human - played with the same strategies but the attack varies, depending upon the moves by Black.


The key to playing Bf4 is to have a strategy that mobilizes the a, b and c pawns to build the pressure on black's queenside. More often than not, it works.

It is important to note that with Bf4 and no c4, the White Bishop, in the opening, must be played, always, to Be2 and not to Bd3 or to Bc4 if attacking black's queenside is to succeed. Always.